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

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import com.yanzhu.jh.project.domain.SurProjectDeptWroks;
import com.yanzhu.jh.project.mapper.SurProjectDeptWroksMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectWorkAttendanceMapper;
import com.yanzhu.jh.project.domain.SurProjectWorkAttendance;
import com.yanzhu.jh.project.service.ISurProjectWorkAttendanceService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目出勤Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-09
 */
@Service
public class SurProjectWorkAttendanceServiceImpl implements ISurProjectWorkAttendanceService 
{
    @Autowired
    private SurProjectWorkAttendanceMapper surProjectWorkAttendanceMapper;

    @Autowired
    private SurProjectDeptWroksMapper surProjectDeptWroksMapper;

    /**
     * 查询项目出勤
     * 
     * @param id 项目出勤主键
     * @return 项目出勤
     */
    @Override
    public SurProjectWorkAttendance selectSurProjectWorkAttendanceById(Long id)
    {
        return surProjectWorkAttendanceMapper.selectSurProjectWorkAttendanceById(id);
    }

    /**
     * 查询项目出勤列表
     * 
     * @param surProjectWorkAttendance 项目出勤
     * @return 项目出勤
     */
    @Override
    public List<SurProjectWorkAttendance> selectSurProjectWorkAttendanceList(SurProjectWorkAttendance surProjectWorkAttendance)
    {
        return surProjectWorkAttendanceMapper.selectSurProjectWorkAttendanceList(surProjectWorkAttendance);
    }

    /**
     * 查询项目出勤列表
     *
     * @param surProjectWorkAttendance 项目出勤
     * @return 项目出勤集合
     */
    @Override
    public List<SurProjectWorkAttendance> selectBgscreenWorkAttendanceList(SurProjectWorkAttendance surProjectWorkAttendance) {
        return surProjectWorkAttendanceMapper.selectBgscreenWorkAttendanceList(surProjectWorkAttendance);
    }

    /**
     * 新增项目出勤
     * 
     * @param surProjectWorkAttendance 项目出勤
     * @return 结果
     */
    @Override
    public int insertSurProjectWorkAttendance(SurProjectWorkAttendance surProjectWorkAttendance)
    {
        surProjectWorkAttendance.setCreateBy(SecurityUtils.getUsername());
        surProjectWorkAttendance.setCreateTime(DateUtils.getNowDate());
        return surProjectWorkAttendanceMapper.insertSurProjectWorkAttendance(surProjectWorkAttendance);
    }

    /**
     * 修改项目出勤
     * 
     * @param surProjectWorkAttendance 项目出勤
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSurProjectWorkAttendance(SurProjectWorkAttendance surProjectWorkAttendance)
    {
        int res = -1;
        surProjectWorkAttendance.setWorkDate(new Date());
        surProjectWorkAttendance.setIsDel(Convert.toLong(PublicStateEnum.OK.getCode()));
        //数据效验
        SurProjectDeptWroks surProjectDeptWroks = new SurProjectDeptWroks();
        surProjectDeptWroks.setProjectId(surProjectWorkAttendance.getProjectId());
        surProjectDeptWroks.setNowDept(Convert.toStr(surProjectWorkAttendance.getDeptId()));
        List<SurProjectDeptWroks> surProjectDeptWroksList =surProjectDeptWroksMapper.selectSurProjectDeptWroksListv2(surProjectDeptWroks);
        if(surProjectDeptWroksList!=null && surProjectDeptWroksList.size()>0){
            if(surProjectWorkAttendance.getServicePersonnel()>surProjectDeptWroksList.get(0).getServicePersonnel()){
                return res;
            }
            if(surProjectWorkAttendance.getSupervisorPersonnel()>surProjectDeptWroksList.get(0).getSupervisorPersonnel()){
                return res;
            }
            if(surProjectWorkAttendance.getContractorPersonnel()>surProjectDeptWroksList.get(0).getContractorPersonnel()){
                return res;
            }
        }
        if(surProjectWorkAttendance.getId()!=null){
            surProjectWorkAttendance.setUpdateBy(SecurityUtils.getUsername());
            surProjectWorkAttendance.setUpdateTime(DateUtils.getNowDate());
            return surProjectWorkAttendanceMapper.updateSurProjectWorkAttendance(surProjectWorkAttendance);
        }else{
            surProjectWorkAttendance.setCreateBy(SecurityUtils.getUsername());
            surProjectWorkAttendance.setCreateTime(DateUtils.getNowDate());
            return surProjectWorkAttendanceMapper.insertSurProjectWorkAttendance(surProjectWorkAttendance);
        }
    }

    /**
     * 批量删除项目出勤
     * 
     * @param ids 需要删除的项目出勤主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectWorkAttendanceByIds(Long[] ids)
    {
        return surProjectWorkAttendanceMapper.deleteSurProjectWorkAttendanceByIds(ids);
    }

    /**
     * 删除项目出勤信息
     * 
     * @param id 项目出勤主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectWorkAttendanceById(Long id)
    {
        return surProjectWorkAttendanceMapper.deleteSurProjectWorkAttendanceById(id);
    }

    @Override
    public List<SurProjectWorkAttendance> selectByDate(SurProjectWorkAttendance surProjectWorkAttendance) {
        return surProjectWorkAttendanceMapper.selectByDate(surProjectWorkAttendance);
    }
}
