package org.linlinjava.jz.hm.service.impl;

import com.google.common.collect.Lists;
import org.linlinjava.jz.core.util.DateTimeUtil;
import org.linlinjava.jz.db.domain.HmEmployClock;
import org.linlinjava.jz.db.domain.HmEmployRelation;
import org.linlinjava.jz.db.domain.HmEmployTask;
import org.linlinjava.jz.db.service.hm.HmEmployTaskService;
import org.linlinjava.jz.db.service.hm.HmEmployTaskSetService;
import org.linlinjava.jz.db.util.hm.HmEmployClockEnum;
import org.linlinjava.jz.db.util.hm.HolidayUtil;
import org.linlinjava.jz.hm.service.HmEmployTaskJobService;
import org.linlinjava.jz.service.HmEmployClockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author fuxubing
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HmEmployTaskJobServiceImpl implements HmEmployTaskJobService {
    @Resource
    private HmEmployClockService hmEmployClockService;
    @Resource
    private HmEmployTaskSetService hmEmployTaskSetService;
    @Resource
    private HmEmployTaskService hmEmployTaskService;


    /**
     * 生成日任务
     *
     * @param orderNo
     * @param hmEmployRelationId
     * @param hmClockId
     */
    public void createDayTask(String orderNo, Long hmEmployRelationId, Long hmClockId) {

        List<Map<String, Object>> dayTaskMap = hmEmployTaskSetService.getDiffTypeTask(orderNo, 1);

        for (Map<String, Object> map : dayTaskMap) {
            HmEmployTask hmEmployTask = new HmEmployTask();
            hmEmployTask.setOrderNo(orderNo);
            hmEmployTask.setEmployClockId(hmClockId);
            hmEmployTask.setEmployRelationId(hmEmployRelationId);
            hmEmployTask.setCategoryId(Long.parseLong(map.get("categoryId").toString()));
            hmEmployTask.setTaskStartTime(LocalDateTime.now());
            hmEmployTask.setTaskType(1);
            hmEmployTask.setState(0);
            hmEmployTaskService.batchInsertDayTask(hmEmployTask, map.get("itemIds").toString().split(","));
        }
    }


    /**
     * 生成周任务
     *
     * @param orderNo
     * @param hmEmployRelationId
     * @param hmClockId
     * @param date
     */
    public void createWeekTask(String orderNo, Long hmEmployRelationId, Long hmClockId, String date) {
        //存放未完成的任务
        List<Map<String, Object>> mapList = Lists.newArrayList();

        List<Map<String, Object>> weekEmployTaskList = hmEmployTaskService.getWeekEmployTaskList(hmEmployRelationId, date);
        weekEmployTaskList.forEach(item -> {
            //任务未完成
            if ("0".equals(item.get("state").toString())) {
                mapList.add(item);
            }
        });

        if (!CollectionUtils.isEmpty(mapList)) {
            hmEmployTaskService.batchInsertWeekTask(orderNo, hmClockId, hmEmployRelationId, mapList);
        }
    }


    /**
     * 生成月任务
     *
     * @param orderNo
     * @param hmEmployRelationId
     * @param hmClockId
     * @param date
     */
    public void createMonthTask(String orderNo, Long hmEmployRelationId, Long hmClockId, String date) {
        //存放未完成的任务
        List<Map<String, Object>> mapList = Lists.newArrayList();

        List<Map<String, Object>> monthEmployTaskList = hmEmployTaskService.getMonthEmployTaskList(hmEmployRelationId, date);
        monthEmployTaskList.forEach(item -> {
            //任务未完成
            if ("0".equals(item.get("state").toString())) {
                mapList.add(item);
            }
        });

        if (!CollectionUtils.isEmpty(mapList)) {
            hmEmployTaskService.batchInsertWeekTask(orderNo, hmClockId, hmEmployRelationId, mapList);
        }
    }


    public long insertHmClock(HmEmployRelation hmEmployRelation, String date) {

        HmEmployClock hmEmployClock = hmEmployClockService.queryByDate(hmEmployRelation.getId(), DateTimeUtil.strToLocalDate(date).atStartOfDay());

        if (null == hmEmployClock) {
            hmEmployClock = new HmEmployClock();
            hmEmployClock.setWorkData(LocalDate.now().atStartOfDay());
            hmEmployClock.setIsRestday(judgeRestDay(hmEmployRelation.getRestDay(), date));
            hmEmployClock.setIsHoliday(judgeHoliday(date));
            hmEmployClock.setStatus(HmEmployClockEnum.STATUS_0.getType());
            hmEmployClock.setApprovalState(HmEmployClockEnum.APPROVAL_STATE_3);
            hmEmployClock.setOrderNo(hmEmployRelation.getOrderNo());
            hmEmployClock.setEmployRelationId(hmEmployRelation.getId());
            hmEmployClock.setCreateTime(LocalDateTime.now());
            hmEmployClock.setUpdateTime(LocalDateTime.now());
            hmEmployClockService.insert(hmEmployClock);
        }
        return hmEmployClock.getId();
    }

    /**
     * 判断休息日(1休息日 0非休息日)
     *
     * @return
     */
    private Integer judgeRestDay(String restDay, String date) {
        return restDay.contains(date) ? 1 : 0;
    }

    /**
     * 判断是否为法定节假日(1节假日 0非节假日)
     *
     * @return
     */
    private Integer judgeHoliday(String date) {
        return null != HolidayUtil.getHoliday(date) ? 1 : 0;
    }

    @Override
    public void createTask(HmEmployRelation hmEmployRelation) {
        String date = DateTimeUtil.localDateFormat(LocalDateTime.now(), "yyyy-MM-dd");
        //生成打卡信息
        long hmClockId = insertHmClock(hmEmployRelation, date);
        //工作日生成任务休息日不生成
        if (0 == judgeRestDay(hmEmployRelation.getRestDay(), date)) {
            String orderNo = hmEmployRelation.getOrderNo();
            Long hmEmployRelationId = hmEmployRelation.getId();
            //生成日任务
            createDayTask(orderNo, hmEmployRelationId, hmClockId);
            //生成周任务
            createWeekTask(orderNo, hmEmployRelationId, hmClockId, date);
            //生成月任务
            createMonthTask(orderNo, hmEmployRelationId, hmClockId, date);
        }
    }
}
