package com.bonus.system.holiday.service;

import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringHelper;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.basic.dao.SysUserMapper;
import com.bonus.system.basic.domain.SysNotice;
import com.bonus.system.basic.service.SysNoticeService;
import com.bonus.system.evection.dao.EvectionDao;
import com.bonus.system.evection.entity.EvectionBean;
import com.bonus.system.holiday.dao.RequestReportDao;
import com.bonus.system.holiday.dao.WorkReportDao;
import com.bonus.system.holiday.entity.HolidayBean;
import com.bonus.system.holiday.entity.WorkReportBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

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

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

    @Resource
    private SysUserMapper userMapper;

    /**
     * 消息推送库表
     */
    @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) {
        //查询登陆人的角色
        if (StringHelper.isNotEmpty(bean.getLeaveTypes())){
            if ("临时外出".equals(bean.getLeaveTypes())){
                bean.setLeaveTypes("临时外出");
            } else if ("出差".equals(bean.getLeaveTypes())){
                bean.setLeaveTypes("出差");
            }
        }
        List<WorkReportBean> workReportList = dao.getWorkReportList(bean);
        for (WorkReportBean workReportBean : workReportList) {
            EvectionBean evectionBean = evectionDao.getPostName(workReportBean.getUserId());
            if (evectionBean != null){
                workReportBean.setPostName(evectionBean.getPostName());
            }else {
                workReportBean.setPostName("");
            }

            StringBuilder name = new StringBuilder();
            if (StringHelper.isNotEmpty(workReportBean.getHostUserId())) {
                String[] checkIds = workReportBean.getHostUserId().split(",");
                for (String checkId : checkIds) {
                    if(checkId.equals("0")){
                        name.append("/").append("");
                    }else{
                        SysUser sysUser = userMapper.selectUserById(Long.valueOf(checkId));
                        String userName = sysUser.getUserName();
                        name.append(userName).append(",");
                    }
                }
                // 如果 name 不为空，移除最后一个逗号
                if (name.length() > 0) {
                    name.setLength(name.length() - 1);
                }
            }
            workReportBean.setHostUserId(name.toString());
        }
        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;
        }
    }

}
