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

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;


import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.yanzhu.jh.project.domain.SurProjectAttendanceCfg;
import com.yanzhu.jh.project.domain.SurProjectAttendanceGroup;
import com.yanzhu.jh.project.domain.SurProjectAttendanceUser;
import com.yanzhu.jh.project.mapper.SurProjectAttendanceCfgMapper;
import com.yanzhu.jh.project.mapper.SurProjectAttendanceGroupMapper;
import com.yanzhu.jh.project.mapper.SurProjectAttendanceUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yanzhu.jh.project.mapper.SurProjectAttendanceDataMapper;
import com.yanzhu.jh.project.domain.SurProjectAttendanceData;
import com.yanzhu.jh.project.service.ISurProjectAttendanceDataService;
import com.ruoyi.common.utils.SecurityUtils;
/**
 * 劳务实名制管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-24
 */
@Service
public class SurProjectAttendanceDataServiceImpl implements ISurProjectAttendanceDataService 
{
    @Autowired
    private SurProjectAttendanceDataMapper surProjectAttendanceDataMapper;

    @Autowired
    private SurProjectAttendanceUserMapper surProjectAttendanceUserMapper;

    @Autowired
    private SurProjectAttendanceGroupMapper surProjectAttendanceGroupMapper;

    @Autowired
    private SurProjectAttendanceCfgMapper surProjectAttendanceCfgMapper;


    /**
     * 查询劳务实名制管理
     * 
     * @param id 劳务实名制管理主键
     * @return 劳务实名制管理
     */
    @Override
    public SurProjectAttendanceData selectSurProjectAttendanceDataById(Long id)
    {
        return surProjectAttendanceDataMapper.selectSurProjectAttendanceDataById(id);
    }

    /**
     * 查询劳务实名制管理列表
     * 
     * @param surProjectAttendanceData 劳务实名制管理
     * @return 劳务实名制管理
     */
    @Override
    public List<SurProjectAttendanceData> selectSurProjectAttendanceDataList(SurProjectAttendanceData surProjectAttendanceData)
    {
        return surProjectAttendanceDataMapper.selectSurProjectAttendanceDataList(surProjectAttendanceData);
    }

    /**
     * 查询考勤信息
     *
     * @param surProjectAttendanceData 劳务实名制管理
     * @return 劳务实名制管理集合
     */
    @Override
    public SurProjectAttendanceData findCurrentAttendanceData(SurProjectAttendanceData surProjectAttendanceData){
        return surProjectAttendanceDataMapper.findCurrentAttendanceData(surProjectAttendanceData);
    }

    @Override
    public List<SurProjectAttendanceData> selectSurProjectAttendanceDataListEx(SurProjectAttendanceData surProjectAttendanceData)
    {
        return surProjectAttendanceDataMapper.selectSurProjectAttendanceDataListEx(surProjectAttendanceData);
    }

    /**
     * 新增劳务实名制管理
     * 
     * @param surProjectAttendanceData 劳务实名制管理
     * @return 结果
     */
    @Override
    public int insertSurProjectAttendanceData(SurProjectAttendanceData surProjectAttendanceData)
    {
        if(StringUtils.isEmpty(surProjectAttendanceData.getCreateBy())){
            surProjectAttendanceData.setCreateBy("task");
            surProjectAttendanceData.setCreateTime(DateUtils.getNowDate());
        }
        return surProjectAttendanceDataMapper.insertSurProjectAttendanceData(surProjectAttendanceData);
    }

    /**
     * 修改劳务实名制管理
     * 
     * @param surProjectAttendanceData 劳务实名制管理
     * @return 结果
     */
    @Override
    public int updateSurProjectAttendanceData(SurProjectAttendanceData surProjectAttendanceData)
    {
        if(StringUtils.isEmpty(surProjectAttendanceData.getCreateBy())){
            surProjectAttendanceData.setUpdateBy("task");
        }
        surProjectAttendanceData.setUpdateTime(DateUtils.getNowDate());
        return surProjectAttendanceDataMapper.updateSurProjectAttendanceData(surProjectAttendanceData);
    }

    /**
     * 批量删除劳务实名制管理
     * 
     * @param ids 需要删除的劳务实名制管理主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectAttendanceDataByIds(Long[] ids)
    {
        return surProjectAttendanceDataMapper.deleteSurProjectAttendanceDataByIds(ids);
    }

    /**
     * 删除劳务实名制管理信息
     * 
     * @param id 劳务实名制管理主键
     * @return 结果
     */
    @Override
    public int deleteSurProjectAttendanceDataById(Long id)
    {
        return surProjectAttendanceDataMapper.deleteSurProjectAttendanceDataById(id);
    }

    /**
     * 批量删除劳务实名制管理
     *
     * @param list 需要删除的数据参数集合
     * @return 结果
     */
    @Override
    public int deleteSurProjectAttendanceDataByParams(List<String> list) {
        return surProjectAttendanceDataMapper.deleteSurProjectAttendanceDataByParams(list);
    }

    /**
     * 批量新增劳务实名制考勤管理
     *
     * @param SurProjectAttendanceDataList 劳务实名制考勤管理列表
     * @return 结果
     */
    @Override
    public int batchSurProjectAttendanceData(List<SurProjectAttendanceData> SurProjectAttendanceDataList) {
        return surProjectAttendanceDataMapper.batchSurProjectAttendanceData(SurProjectAttendanceDataList,DateUtils.dateTimeNow(DateUtils.YYYY));
    }

    @Override
    public void add(SurProjectAttendanceData sdata) {
        if(StringUtils.isEmpty(sdata.getWorkerId()) || StringUtils.isEmpty(sdata.getAttendanceTime())){
            return;
        }
        SurProjectAttendanceData where=new SurProjectAttendanceData();
        where.setVendorsCode(sdata.getVendorsCode());
        where.setCfgid(sdata.getCfgid());
        where.setWorkerId(sdata.getWorkerId());
        where.setAttendanceTime(sdata.getAttendanceTime());
        List<SurProjectAttendanceData> list=selectSurProjectAttendanceDataListEx(where);
        if(list.size()==0){
            //设置考勤其它参数
            //查询人员信息
            SurProjectAttendanceUser userWhere=new SurProjectAttendanceUser();
            userWhere.setWorkerId(sdata.getWorkerId());
            List<SurProjectAttendanceUser> uList= surProjectAttendanceUserMapper.selectSurProjectAttendanceUserList(userWhere);
            if(uList.size()==0){
                return;
            }
            SurProjectAttendanceUser user=uList.get(0);
            sdata.setWorkerName(user.getName());
            String photo=user.getPhoto();
            if(StrUtil.isEmpty(photo)){
                photo=user.getRecentPhoto();
            }
            sdata.setWorkerPhoto(photo);
            sdata.setWorkerGender(user.getGender());;
            sdata.setGroupName(user.getGroupName());
            sdata.setWorkTypeName(user.getWorkTypeName());
            sdata.setEthnic(user.getEthnic());
            sdata.setNativePlace(user.getNativePlace());
            sdata.setPhone(user.getPhone());
            sdata.setSpecWorkType(user.getSpecWorkType());
            //查询分组信息
            SurProjectAttendanceGroup groupWhere=new SurProjectAttendanceGroup();
            groupWhere.setCfgid(sdata.getCfgid());
            groupWhere.setCompanyId(sdata.getCompanyId());
            List<SurProjectAttendanceGroup> gList=surProjectAttendanceGroupMapper.selectSurProjectAttendanceGroupList(groupWhere);
            if(gList.size()==0){
                return;
            }
            SurProjectAttendanceGroup group=gList.get(0);
            sdata.setCompanyName(group.getCompanyName());
            sdata.setCompanyTypeId(group.getCompanyTypeId());
            //查询华筑的分组信息
            if("huazhu".equals(sdata.getVendorsCode())){
                sdata.setWorkerGender(sdata.getWorkerGender()==1?0l:1l);
                sdata.setCompanyTypeId(getHuazhuCompanyTypeId(group.getCompanyName()));//获取华筑的分包商类型
            }
            if("jgw".equals(sdata.getVendorsCode())){
                sdata.setWorkTypeName(group.getCompanyCode());
                sdata.setGroupName(group.getTeamName());
            }
            //查询项目部门信息
            SurProjectAttendanceCfg cfgWhere=new SurProjectAttendanceCfg();
            cfgWhere.setId(sdata.getCfgid());
            List<SurProjectAttendanceCfg> cfgList=surProjectAttendanceCfgMapper.selectSurProjectAttendanceCfgListForAllInfo(cfgWhere);
            if(cfgList.size()==0){
                return;
            }
            SurProjectAttendanceCfg cfg=cfgList.get(0);
            sdata.setProjectId(cfg.getProjectId());
            sdata.setDeptId(cfg.getSubDeptId());
            sdata.setProjectName(cfg.getProjectName());
            sdata.setDeptName(cfg.getDeptName());
            if(sdata.getCompanyTypeId()==null){
                sdata.setCompanyTypeId("0");
            }
            insertSurProjectAttendanceData(sdata);
        }else{
            sdata.setId(list.get(0).getId());
            SurProjectAttendanceData upData=list.get(0);
            upData.setAttendanceOutTime(sdata.getAttendanceTime());
            upData.setGroupName(sdata.getGroupName());
            upData.setWorkTypeName(sdata.getWorkTypeName());
            updateSurProjectAttendanceData(upData);
        }
    }

    private String getHuazhuCompanyTypeId(String groupName) {
        return surProjectAttendanceDataMapper.findHuaZhuCompanyType(groupName);
    }

    @Override
    public String getLastServerId(SurProjectAttendanceData where) {
        return ""+surProjectAttendanceDataMapper.getLastServerId(where);
    }

    @Override
    public List<SurProjectAttendanceData> groupByComany(SurProjectAttendanceData where) {
        return surProjectAttendanceDataMapper.groupByComany(where);
    }

    @Override
    public Long getHuazhuPage(Long id) {
        return surProjectAttendanceDataMapper.getHuazhuPage(id);
    }

    @Override
    public List<SurProjectAttendanceData> groupAllByComany(SurProjectAttendanceData where) {
        return surProjectAttendanceDataMapper.groupAllByComany(where);
    }

    @Override
    public List<Map<String,Object>> initOtherData(Map<String,Object> data) {
        List<Map<String,Object>> dataList = new ArrayList<>();
        List<Map<String,Object>> list = surProjectAttendanceDataMapper.initOtherData(data);
        if(StringUtils.isNotEmpty(list)){
            List<String> workerIds = list.stream().map(Map -> Map.get("workerId").toString()).collect(Collectors.toList());
            data.put("list",workerIds);
            List<SurProjectAttendanceUser> datas=surProjectAttendanceUserMapper.todayAttendanceOtherData(data);
            for (Map<String,Object> map:list) {
                for(SurProjectAttendanceUser sau:datas){
                    if(Objects.equals(map.get("workerId").toString(),sau.getWorkerId())){
                        map.put("inTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,sau.getInTime()));
                        map.put("outTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,sau.getOutTime()));
                        dataList.add(map);
                    }
                }
            }
        }
        return dataList;
    }

    @Override
    public List<Map<String,Object>> initHuaZhuData(Map<String,Object> data) {
        List<Map<String,Object>> dataList = new ArrayList<>();
        List<Map<String,Object>> list = surProjectAttendanceDataMapper.initHuaZhuData(data);
        if(StringUtils.isNotEmpty(list)){
            List<String> workerIds = list.stream().map(Map -> Map.get("workerId").toString()).collect(Collectors.toList());
            data.put("list",workerIds);
            List<SurProjectAttendanceUser> datas=surProjectAttendanceUserMapper.todayAttendanceOtherData(data);
            for (Map<String,Object> map:list) {
                for(SurProjectAttendanceUser sau:datas){
                    if(Objects.equals(map.get("workerId").toString(),sau.getWorkerId())){
                        map.put("inTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,sau.getInTime()));
                        map.put("outTime",DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,sau.getOutTime()));
                        dataList.add(map);
                    }
                }
            }
        }
        return dataList;
    }

    @Override
    public List<SurProjectAttendanceData> todayAttendance(SurProjectAttendanceData where) {
        return surProjectAttendanceDataMapper.todayAttendance(where);
    }

    @Override
    public List<SurProjectAttendanceData> groupTodayCompanyTypeId(SurProjectAttendanceData where) {
        return surProjectAttendanceDataMapper.groupTodayCompanyTypeId(where);
    }
}
