package com.yanzhu.jh.project.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.enums.ApplyCfgTypeEnum;
import com.ruoyi.common.enums.ShiFouEnum;
import com.ruoyi.common.enums.VendorsCodeEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysApplyConfig;
import com.ruoyi.system.service.ISysApplyConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectAttendanceCfgMapper;
import com.yanzhu.jh.project.domain.SurProjectAttendanceCfg;
import com.yanzhu.jh.project.service.ISurProjectAttendanceCfgService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 项目考勤配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-24
 */
@Service
public class SurProjectAttendanceCfgServiceImpl implements ISurProjectAttendanceCfgService 
{

    @Autowired
    private ISysApplyConfigService sysApplyConfigService;

    @Autowired
    private SurProjectAttendanceCfgMapper surProjectAttendanceCfgMapper;

    /**
     * 查询项目考勤配置
     * 
     * @param id 项目考勤配置主键
     * @return 项目考勤配置
     */
    @Override
    public SurProjectAttendanceCfg selectSurProjectAttendanceCfgById(Long id)
    {
        return surProjectAttendanceCfgMapper.selectSurProjectAttendanceCfgById(id);
    }

    /**
     * 查询项目考勤配置列表
     * 
     * @param surProjectAttendanceCfg 项目考勤配置
     * @return 项目考勤配置
     */
    @Override
    public List<SurProjectAttendanceCfg> selectSurProjectAttendanceCfgList(SurProjectAttendanceCfg surProjectAttendanceCfg)
    {
        return surProjectAttendanceCfgMapper.selectSurProjectAttendanceCfgList(surProjectAttendanceCfg);
    }

    /**
     * 新增项目考勤配置
     * 
     * @param surProjectAttendanceCfg 项目考勤配置
     * @return 结果
     */
    @Override
    public int insertSurProjectAttendanceCfg(SurProjectAttendanceCfg surProjectAttendanceCfg)
    {
        surProjectAttendanceCfg.setCreateBy(SecurityUtils.getUsername());
        surProjectAttendanceCfg.setCreateTime(DateUtils.getNowDate());
        return surProjectAttendanceCfgMapper.insertSurProjectAttendanceCfg(surProjectAttendanceCfg);
    }

    /**
     * 修改项目考勤配置
     * 
     * @param surProjectAttendanceCfg 项目考勤配置
     * @return 结果
     */
    @Override
    public int updateSurProjectAttendanceCfg(SurProjectAttendanceCfg surProjectAttendanceCfg)
    {
        surProjectAttendanceCfg.setUpdateBy(SecurityUtils.getUsername());
        surProjectAttendanceCfg.setUpdateTime(DateUtils.getNowDate());
        if(VendorsCodeEnum.YANZHU.getCode().equals(surProjectAttendanceCfg.getVendorsCode())){
            SysApplyConfig sysApplyConfig = new SysApplyConfig();
            sysApplyConfig.setCfgType(ApplyCfgTypeEnum.LABOUR.getCode());
            sysApplyConfig.setCfgId(surProjectAttendanceCfg.getId());
            List<SysApplyConfig> list = sysApplyConfigService.selectSysApplyConfigList(sysApplyConfig);
            if(CollectionUtils.isNotEmpty(list)){
                sysApplyConfig = list.get(0);
                sysApplyConfig.setIsDel(surProjectAttendanceCfg.getEnabled().equals(ShiFouEnum.FOU.getLongCode())?ShiFouEnum.SHI.getCode():ShiFouEnum.FOU.getCode());
                sysApplyConfigService.updateSysApplyConfig(sysApplyConfig);
            }
        }
        return surProjectAttendanceCfgMapper.updateSurProjectAttendanceCfg(surProjectAttendanceCfg);
    }

    /**
     * 批量删除项目考勤配置
     * 
     * @param ids 需要删除的项目考勤配置主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectAttendanceCfgByIds(Long[] ids)
    {
        List<Long> cfgIds = new ArrayList<>();
        for(Long id:ids){
            SurProjectAttendanceCfg surProjectAttendanceCfg = surProjectAttendanceCfgMapper.selectSurProjectAttendanceCfgById(id);
            if(VendorsCodeEnum.YANZHU.getCode().equals(surProjectAttendanceCfg.getVendorsCode())){
                SysApplyConfig sysApplyConfig = new SysApplyConfig();
                sysApplyConfig.setCfgType(ApplyCfgTypeEnum.LABOUR.getCode());
                sysApplyConfig.setCfgId(id);
                List<SysApplyConfig> list = sysApplyConfigService.selectSysApplyConfigList(sysApplyConfig);
                if(CollectionUtils.isNotEmpty(list)){
                    cfgIds.add(list.get(0).getId());
                }
            }
        }
        if(CollectionUtils.isNotEmpty(cfgIds)){
            sysApplyConfigService.deleteSysApplyConfigByIds(cfgIds.stream().toArray(Long[]::new));
        }
        return surProjectAttendanceCfgMapper.deleteSurProjectAttendanceCfgByIds(ids);
    }

    /**
     * 删除项目考勤配置信息
     * 
     * @param id 项目考勤配置主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectAttendanceCfgById(Long id)
    {
        SurProjectAttendanceCfg surProjectAttendanceCfg = surProjectAttendanceCfgMapper.selectSurProjectAttendanceCfgById(id);
        if(VendorsCodeEnum.YANZHU.getCode().equals(surProjectAttendanceCfg.getVendorsCode())){
            SysApplyConfig sysApplyConfig = new SysApplyConfig();
            sysApplyConfig.setCfgType(ApplyCfgTypeEnum.LABOUR.getCode());
            sysApplyConfig.setCfgId(id);
            List<SysApplyConfig> list = sysApplyConfigService.selectSysApplyConfigList(sysApplyConfig);
            if(CollectionUtils.isNotEmpty(list)){
                sysApplyConfigService.deleteSysApplyConfigById(list.get(0).getId());
            }
        }
        return surProjectAttendanceCfgMapper.deleteSurProjectAttendanceCfgById(id);
    }
}
