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

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.HmEmployTaskMapper;
import org.linlinjava.jz.db.dao.HmTaskQuestionLogMapper;
import org.linlinjava.jz.db.domain.HmEmployTask;
import org.linlinjava.jz.db.domain.HmEmployTaskExample;
import org.linlinjava.jz.db.domain.HmTaskQuestionLog;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @author fuxubing
 */
@Service
public class HmEmployTaskService {
    @Resource
    private HmEmployTaskMapper hmEmployTaskMapper;
    @Resource
    private HmEmployTaskSetService hmEmployTaskSetService;
    @Resource
    public HmTaskQuestionLogMapper hmTaskQuestionLogMapper;

    public List<Map<String, Object>> queryByClockId(Long clockId, Integer taskType, List<Integer> stateList) {
        return hmEmployTaskMapper.queryByClockId(clockId, taskType, stateList);
    }

    /**
     * 根据打卡id查当天的任务
     *
     * @param employClockId 是hm_employ_clock的id
     * @return
     */
    public List<HmEmployTask> getEmployTaskList(Long employClockId) {
        HmEmployTaskExample example = new HmEmployTaskExample();
        example.or().andEmployClockIdEqualTo(employClockId);

        return hmEmployTaskMapper.selectByExample(example);
    }

    /**
     * 查询打卡关联任务总数
     *
     * @param employClockId
     * @return
     */
    public long queryCount(Long employClockId) {
        HmEmployTaskExample example = new HmEmployTaskExample();
        example.or().andEmployClockIdEqualTo(employClockId);
        return hmEmployTaskMapper.countByExample(example);
    }

    /**
     * 查询打卡关联任务总完成数
     *
     * @param employClockId
     * @return
     */
    public long queryCompleteCount(Long employClockId) {
        HmEmployTaskExample example = new HmEmployTaskExample();
        example.or().andEmployClockIdEqualTo(employClockId).andStateIn(Lists.newArrayList(1, 2));
        return hmEmployTaskMapper.countByExample(example);
    }

    /**
     * 查询打卡关联任务无法完成数
     *
     * @param employClockId
     * @return
     */
    public long queryInCompleteCount(Long employClockId) {
        HmEmployTaskExample example = new HmEmployTaskExample();
        example.or().andEmployClockIdEqualTo(employClockId).andStateEqualTo(2);
        return hmEmployTaskMapper.countByExample(example);
    }

    /**
     * 获取每日任务
     *
     * @param employRelationId
     * @param date
     * @return
     */
    public List<Map<String, Object>> getEmployTaskList(Long employRelationId, String date) {
        List<Map<String, Object>> mapList = hmEmployTaskMapper.selectTaskList(employRelationId, 1, date);
        //判断是否当天有任务，没有的话将每日任务覆盖
        if (CollectionUtils.isEmpty(mapList)) {
            mapList = hmEmployTaskSetService.getTaskByTaskType(employRelationId, 1);
        }
        return mapList;
    }

    /**
     * 查询每周任务
     *
     * @param employRelationId
     * @param date
     * @return
     */
    public List<Map<String, Object>> getWeekEmployTaskList(Long employRelationId, String date) {
        //已设置的所有每周任务
        List<Map<String, Object>> allWeekTask = hmEmployTaskSetService.getTaskByTaskType(employRelationId, 2);
        //已完成所有每周任务
        List<Map<String, Object>> completeEmployTaskList = hmEmployTaskMapper.getCompleteEmployTaskList(employRelationId, 2, date);
        return replaceTaskList(allWeekTask, completeEmployTaskList);
    }

    /**
     * 查询每月任务
     *
     * @param employRelationId
     * @param date
     * @return
     */
    public List<Map<String, Object>> getMonthEmployTaskList(Long employRelationId, String date) {
        //已设置的所有每月任务
        List<Map<String, Object>> allMonthTask = hmEmployTaskSetService.getTaskByTaskType(employRelationId, 3);
        //已完成所有每月任务
        List<Map<String, Object>> monthCompleteEmployTaskList = hmEmployTaskMapper.getMonthCompleteEmployTaskList(employRelationId, 3, date);
        return replaceTaskList(allMonthTask, monthCompleteEmployTaskList);
    }

    private List<Map<String, Object>> replaceTaskList(List<Map<String, Object>> allList, List<Map<String, Object>> completeList) {
        allList.forEach(item -> {
            for (Map<String, Object> map : completeList) {
                if (item.get("item_id").equals(map.get("item_id"))) {
                    item.replace("id", map.get("id"));
                    item.replace("state", map.get("state"));
                    item.replace("incomplete_reason", map.get("incomplete_reason"));
                }
            }
        });
        return allList;
    }

    public int update(HmEmployTask hmEmployTask) {
        return hmEmployTaskMapper.updateByPrimaryKeySelective(hmEmployTask);
    }

    public HmEmployTask queryById(Long id) {
        return hmEmployTaskMapper.selectByPrimaryKey(id);
    }

    public int updateByEmployClockId(Long employClockId) {
        HmEmployTaskExample example = new HmEmployTaskExample();
        example.or().andEmployClockIdEqualTo(employClockId).andStateEqualTo(0);

        HmEmployTask hmEmployTask = new HmEmployTask();
        hmEmployTask.setState(1);
        hmEmployTask.setTaskCompleteTime(LocalDateTime.now());
        hmEmployTask.setUpdateTime(LocalDateTime.now());

        return hmEmployTaskMapper.updateByExampleSelective(hmEmployTask, example);
    }


    /**
     * 判断任务是否全部完成
     *
     * @param employClockId
     * @param isNeedClock
     * @return
     */
    public boolean isAllCompleted(Long employClockId, Integer isNeedClock) {
        boolean isCompleted = false;
        long count = queryCount(employClockId);

        long completeCount = queryCompleteCount(employClockId);

        if (count >= 0 && isNeedClock == 1) {
            if (completeCount == count) {
                isCompleted = true;
            }
        }

        return isCompleted;
    }


    /**
     * 判断是否需要质疑
     *
     * @param employClockId
     * @param isNeedClock
     * @return
     */
    public boolean isQuestion(Long employClockId, Integer isNeedClock) {
        boolean flag = false;
        long count = queryCount(employClockId);

        long completeCount = queryCompleteCount(employClockId);

        if (count >= 0 && isNeedClock == 1) {
            if (completeCount <= count) {
                flag = true;
            }
        }

        return flag;
    }


    /**
     * 批量生成每日任务
     *
     * @param hmEmployTask
     * @param itemIds
     * @return
     */
    public int batchInsertDayTask(HmEmployTask hmEmployTask, String[] itemIds) {
        return hmEmployTaskMapper.batchInsert(hmEmployTask, itemIds);
    }

    /**
     * 批量生成每周、每月任务
     *
     * @param orderNo
     * @param employRelationId
     * @param employClockId
     * @param mapList
     * @return
     */
    public int batchInsertWeekTask(String orderNo, Long employClockId, Long employRelationId, List<Map<String, Object>> mapList) {
        return hmEmployTaskMapper.batchInsertWeekTask(orderNo, employClockId, employRelationId, mapList);
    }

    /**
     * 新增质疑打卡记录
     *
     * @param employClockId
     * @param taskIds
     * @return
     */
    public int createQuestionLog(Long employClockId, List<Long> taskIds) {
        HmTaskQuestionLog hmTaskQuestionLog = new HmTaskQuestionLog();
        hmTaskQuestionLog.setEmployTaskId(StringUtils.join(taskIds, ","));
        hmTaskQuestionLog.setEmployClockId(employClockId);
        hmTaskQuestionLog.setCreateTime(LocalDateTime.now());
        return hmTaskQuestionLogMapper.insertSelective(hmTaskQuestionLog);
    }
}
