package com.bonus.system.holiday.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson2.JSONObject;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.FastJsonHelper;
import com.bonus.common.core.utils.ImportExcelUtils;
import com.bonus.common.core.utils.StringHelper;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.common.security.utils.ValidatorsUtils;
import com.bonus.system.api.domain.MapVo;
import com.bonus.system.api.domain.SysRole;
import com.bonus.system.basic.domain.SysNotice;
import com.bonus.system.basic.service.SysNoticeService;
import com.bonus.system.dept.dao.ProDeptRoleDao;
import com.bonus.system.evection.dao.EvectionDao;
import com.bonus.system.evection.entity.EvectionBean;
import com.bonus.system.holiday.dao.HolidayDao;
import com.bonus.system.holiday.dao.WorkReportDao;
import com.bonus.system.holiday.entity.HolidayBean;
import com.bonus.system.holiday.entity.HolidayImportBean;
import com.bonus.system.holiday.entity.HolidayKeyBean;
import com.bonus.system.holiday.entity.WorkReportBean;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目部角色-业务层
 *
 * @author fly
 */
@Service("WorkReportService")
public class WorkReportServiceImpl implements WorkReportService {

    @Resource(name = "WorkReportDao")
    private WorkReportDao dao;

    @Resource(name = "EvectionDao")
    private EvectionDao evectionDao;

    /**
     * 消息推送库表
     */
    @Resource
    private SysNoticeService noticeService;

    @Override
    public int insertHoliday(HolidayBean o) {
        return 0;
    }

    @Override
    public List<WorkReportBean> selectDataByMonth(String month) {
        return dao.selectDataByMonth(month);
    }

    @Override
    public int insertWorkReportData(WorkReportBean bean) {
        try {
            return dao.insertWorkReportData(bean);
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public List<WorkReportBean> getWorkReportList(WorkReportBean bean) {
        List<WorkReportBean> workReportList = new ArrayList<>();
        Long userId = SecurityUtils.getUserId();
        int roleCount = 0;
        //先查询用户的角色
        List<SysRole> sysRoleList = dao.getRoleListByUserId(userId);
        if (sysRoleList.size() > 0) {
            for (SysRole sysRole : sysRoleList) {
                if (sysRole.getRoleName().contains("人资专员")) {
                    roleCount = 1;
                    break;
                }
            }
            if (roleCount == 1) {
                bean.setOrgIds(null);
            } else {
                //查询该人员是哪些部门的考勤员和部门负责人
                List<Long> orgIdList = dao.selectOrgIdByRoleAndUserId(userId);
                if (orgIdList.size() > 0) {
                    bean.setOrgIds(orgIdList.toArray(new Long[orgIdList.size()]));
                } else {
                    return workReportList;
                }
            }
            workReportList = dao.getWorkReportList(bean);
            for (WorkReportBean workReportBean : workReportList) {
                if (StringHelper.isNotEmpty(workReportBean.getCheckId())) {
                    //用逗号分割数据
                    String[] checkIds = workReportBean.getCheckId().split(",");
                    workReportBean.setIsCheck("0");
                    for (String checkId : checkIds) {
                        if (checkId.equals(String.valueOf(userId))) {
                            workReportBean.setIsCheck("1");
                        }
                    }
                }
            }

        }
        return workReportList;
    }

    @Override
    public List<WorkReportBean> getDetailsList(WorkReportBean bean) {
        List<WorkReportBean> list = new ArrayList<>();
        if (StringHelper.isNotEmpty(bean.getType())) {
            if ("请假".equals(bean.getType())) {
                bean.setHolidayType(new String[]{"病假", "年休假", "探亲假", "事假", "产假", "婚假", "丧假", "育儿假", "陪护假","流产假","哺乳假","节育假","父母护理假","其他"});
            }
            list = dao.getDetailsList(bean);
            if (list.size() > 0) {
                for (WorkReportBean workReportBean : list) {
                    EvectionBean evectionBean = evectionDao.getPostName(workReportBean.getUserId());
                    if (evectionBean != null){
                        workReportBean.setPostName(evectionBean.getPostName());
                    }else {
                        workReportBean.setPostName("");
                    }
                }
            }
        }
        return list;
    }

    @Override
    public int setCheck(WorkReportBean bean) {
        String userName = SecurityUtils.getLoginUser().getSysUser().getUserName();
        //现根据id查询该条数据
        WorkReportBean workReportBean = dao.selectEvectionById(bean.getId());
        if (workReportBean != null) {
            String status = workReportBean.getStatus();
            String time = DateUtils.getTime();
            Long[] userIds = new Long[0];
            if ("0".equals(status)) {
                //考勤员核对
                bean.setAttCheckRemark("部门考勤员  " + userName + "  " + bean.getCheckRemark() + "  " + time);
                bean.setAttCheckStatus("1");
                bean.setAttCheckTime(time);
                bean.setStatus("1");
                if (StringHelper.isNotEmpty(workReportBean.getChargeCheckId())) {
                    //用逗号分割数据
                    String[] chargeCheckIds = workReportBean.getChargeCheckId().split(",");
                    userIds = Arrays.stream(chargeCheckIds).map(Long::valueOf).toArray(Long[]::new);
                }
            } else if ("1".equals(status)) {
                //负责人核对
                bean.setChargeCheckRemark("部门负责人  " + userName + "  " + bean.getCheckRemark() + "  " + time);
                bean.setChargeCheckStatus("1");
                bean.setChargeCheckTime(time);
                bean.setStatus("2");
                if (StringHelper.isNotEmpty(workReportBean.getWageCheckId())) {
                    //用逗号分割数据
                    String[] wageCheckIds = workReportBean.getWageCheckId().split(",");
                    userIds = Arrays.stream(wageCheckIds).map(Long::valueOf).toArray(Long[]::new);
                }
            } else if ("2".equals(status)) {
                //人资核对
                bean.setWageCheckRemark("人资专员  " + userName + "  " + bean.getCheckRemark() + "  " + time);
                bean.setWageCheckStatus("1");
                bean.setWageCheckTime(time);
                bean.setStatus("3");
            }
            int res = dao.updateEvection(bean);
            if (res > 0) {
                if (userIds != null && userIds.length > 0) {
                    SysNotice o = new SysNotice();
                    o.setUserIds(userIds);
                    o.setUserId(1L);
                    o.setTitle("工作安排统计报表");
                    o.setContent("工作安排统计报表");
                    o.setType("72");
                    o.setValue(workReportBean.getMonth());
                    ////推送消息
                    noticeService.insertNotice(o);
                }
            } else {
                return 0;
            }
            return 1;
        } else {
            return 0;
        }
    }

}
