package com.ruoyi.project.production.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.production.controller.vo.SaiQueryVO;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISysDictTypeService;
import com.ruoyi.project.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.production.mapper.TSaiMapper;
import com.ruoyi.project.production.domain.TSai;
import com.ruoyi.project.production.service.ITSaiService;

/**
 * SAI信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-08
 */
@Service
public class TSaiServiceImpl implements ITSaiService
{
    @Autowired
    private TSaiMapper tSaiMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserServiceImpl sysUserService;

    /**
     * 查询SAI信息
     *
     * @param saiId SAI信息ID
     * @return SAI信息
     */
    @Override
    public TSai selectTSaiById(Long saiId)
    {
        return tSaiMapper.selectTSaiById(saiId);
    }

    /**
     * 查询SAI信息列表
     *
     * @param tSai SAI信息
     * @return SAI信息
     */
    @Override
    public List<TSai> selectTSaiListByYear(SaiQueryVO tSai)
    {
        List<TSai> tSais = tSaiMapper.selectTSaiListByYear(tSai);
        List<SysDept> sysDepts = sysDeptMapper.selectDeptList(new SysDept());
        for (TSai sai : tSais) {
            for (SysDept sysDept : sysDepts) {
                if (sysDept.getDeptId().toString().equals(sai.getPlantId())) {
                    sai.setPlantName(sysDept.getDeptName());
                }
                if (sysDept.getDeptId().toString().equals(sai.getUserDept())) {
                    sai.setUserDeptName(sysDept.getDeptName());
                }
            }
            String applicant = sai.getApplicant();
            if (applicant != null) {
                sai.setApplicantName(sysUserService.selectUserById(Long.parseLong(applicant)).getNickName());
            }
        }
        return tSais;
    }

    /**
     * 查询SAI信息列表
     *
     * @param tSai SAI信息
     * @return SAI信息
     */
    @Override
    public List<TSai> selectTSaiList(TSai tSai)
    {
        return tSaiMapper.selectTSaiList(tSai);
    }

    /**
     * 新增SAI信息
     *
     * @param tSai SAI信息
     * @return 结果
     */
    @Override
    public int insertTSai(TSai tSai)
    {
        tSai.setCreateTime(DateUtils.getNowDate());
        return tSaiMapper.insertTSai(tSai);
    }

    /**
     * 修改SAI信息
     *
     * @param tSai SAI信息
     * @return 结果
     */
    @Override
    public int updateTSai(TSai tSai)
    {
        tSai.setUpdateTime(DateUtils.getNowDate());
        return tSaiMapper.updateTSai(tSai);
    }

    /**
     * 批量删除SAI信息
     *
     * @param saiIds 需要删除的SAI信息ID
     * @return 结果
     */
    @Override
    public int deleteTSaiByIds(Long[] saiIds)
    {
        return tSaiMapper.deleteTSaiByIds(saiIds);
    }

    /**
     * 删除SAI信息信息
     *
     * @param saiId SAI信息ID
     * @return 结果
     */
    @Override
    public int deleteTSaiById(Long saiId)
    {
        return tSaiMapper.deleteTSaiById(saiId);
    }
}
