package org.linlinjava.jz.hm.web.servant;

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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.ContractSubTemplateService;
import org.linlinjava.jz.db.service.hm.HmEmployRelationService;
import org.linlinjava.jz.db.service.hm.HmEmployTaskService;
import org.linlinjava.jz.db.util.HmResponseUtil;
import org.linlinjava.jz.db.util.hm.HmEmployClockEnum;
import org.linlinjava.jz.db.util.hm.HmNoticeEnum;
import org.linlinjava.jz.dto.HmNoticeDto;
import org.linlinjava.jz.hm.dto.ServantTaskDTO;
import org.linlinjava.jz.hm.util.DateUtil;
import org.linlinjava.jz.service.HmEmployClockService;
import org.linlinjava.jz.service.NoticeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fuxubing
 */
@Api(value = "保姆-任务信息", description = "保姆-任务信息")
@RestController
@RequestMapping("task")
@Valid
public class HmServantTaskController {
    @Resource
    private HmEmployRelationService hmEmployRelationService;
    @Resource
    private HmEmployTaskService hmEmployTaskService;
    @Resource
    private HmEmployClockService hmEmployClockService;
    @Resource
    private ContractSubTemplateService contractSubTemplateService;
    @Resource
    private NoticeService noticeService;

    @ApiOperation(value = "每日任务清单", notes = "每日任务清单")
    @GetMapping(value = "list")
    public Object list(@NotNull @RequestParam Long employRelationId, @NotBlank @RequestParam String date) {
        HmEmployRelation relation = hmEmployRelationService.queryById(employRelationId);
        if (relation == null) {
            return HmResponseUtil.fail_null();
        }

        HmEmployClock hmEmployClock = hmEmployClockService.queryByDate(employRelationId, DateUtil.strToLocalDate(date).atStartOfDay());
        //每日任务
        List<Map<String, Object>> dayTaskList = Lists.newArrayList();
        //每周任务
        List<Map<String, Object>> weekEmployTaskList = Lists.newArrayList();
        //每月任务
        List<Map<String, Object>> monthEmployTaskList = Lists.newArrayList();


        boolean isRestDay = false;
        boolean isCompleted = false;

        long inCompleteCount = 0;

        if (null != hmEmployClock) {
            isRestDay = hmEmployClock.getIsRestday() == 1;
            inCompleteCount = hmEmployTaskService.queryInCompleteCount(hmEmployClock.getId());

            isCompleted = hmEmployTaskService.isAllCompleted(hmEmployClock.getId(), relation.getIsNeedClock());

        } else {
            //判断是否是休息日
            if (relation.getRestDay().contains(date)) {
                isRestDay = true;
            }
            hmEmployClock = new HmEmployClock();
            hmEmployClock.setStatus(HmEmployClockEnum.STATUS_0.getType());
            hmEmployClock.setApprovalState(HmEmployClockEnum.APPROVAL_STATE_3);
        }

        //当前输入日期与合同开始日期比较
        boolean before = contractSubTemplateService.isBefore(relation.getOrderNo(), DateUtil.strToLocalDate(date));

        if (!before && relation.getIsNeedClock() == 1) {
            //每日任务
            dayTaskList = hmEmployTaskService.getEmployTaskList(employRelationId, date);
            //每周任务
            weekEmployTaskList = hmEmployTaskService.getWeekEmployTaskList(employRelationId, date);
            //每月任务
            monthEmployTaskList = hmEmployTaskService.getMonthEmployTaskList(employRelationId, date);
        }

        Map<String, Object> map = new HashMap<>(16);
        map.put("isNeedClock", relation.getIsNeedClock() == 1);
        map.put("dayTaskList", dayTaskList);
        map.put("weekTaskList", weekEmployTaskList);
        map.put("monthTask", monthEmployTaskList);
        map.put("isRestDay", isRestDay);
        map.put("hmEmployClock", hmEmployClock);
        map.put("isCompleted", isCompleted);
        map.put("isBefore", before);
        map.put("inCompleteCount", inCompleteCount);
        return HmResponseUtil.ok(map);
    }

    @ApiOperation(value = "保姆任务提交")
    @PostMapping(path = "submit")
    public Object submit(@Valid @RequestBody ServantTaskDTO servantTaskDTO) {
        HmEmployTask hmEmployTask = hmEmployTaskService.queryById(servantTaskDTO.getId());

        if (null == hmEmployTask) {
            return HmResponseUtil.fail_null();
        }
        if (servantTaskDTO.getState().equals(2)) {
            if (StringUtils.isBlank(servantTaskDTO.getIncompleteReason())) {
                return HmResponseUtil.fail("请选择未完成原因");
            }

            HmEmployRelation hmEmployRelation = hmEmployRelationService.queryById(hmEmployTask.getEmployRelationId());
            HmNoticeDto hmNoticeDto = new HmNoticeDto();
            hmNoticeDto.setAppUserId(Lists.newArrayList(hmEmployRelation.getUserButlerId(), hmEmployRelation.getUserEmployerId()));
            hmNoticeDto.setEmployRelationId(hmEmployRelation.getId());
            hmNoticeDto.setrId(hmEmployTask.getEmployClockId().toString());
            hmNoticeDto.setCreateUserId(hmEmployRelation.getUserServantId());
            hmNoticeDto.setContent("保姆" + hmEmployTask.getTaskStartTime().toLocalDate() + "的服务有任务无法完成，管家已知晓，正在跟进了解情况中！");
            hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_5.getType());
            hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_5.getNoticeUserType());
            hmNoticeDto.setRemark(DateUtil.DateFormatToyyyyMMdd(hmEmployTask.getTaskStartTime()));
            noticeService.create(hmNoticeDto);
        }

        hmEmployTask.setState(servantTaskDTO.getState());
        hmEmployTask.setIncompleteReason(servantTaskDTO.getIncompleteReason());
        hmEmployTask.setTaskCompleteTime(LocalDateTime.now());
        hmEmployTask.setUpdateTime(LocalDateTime.now());
        int count = hmEmployTaskService.update(hmEmployTask);

        if (count > 0) {
            return HmResponseUtil.ok();
        }
        return HmResponseUtil.fail();
    }

    @ApiOperation(value = "一键完成")
    @GetMapping(path = "batchSubmit")
    public Object batchSubmit(@NotNull @RequestParam Long employRelationId, @NotBlank @RequestParam String date) {

        HmEmployClock hmEmployClock = hmEmployClockService.queryByDate(employRelationId, DateUtil.strToLocalDate(date).atStartOfDay());

        if (null == hmEmployClock) {
            return HmResponseUtil.fail_null();
        }

        int count = hmEmployTaskService.updateByEmployClockId(hmEmployClock.getId());

        if (count > 0) {
            return HmResponseUtil.ok();
        }
        return HmResponseUtil.fail();
    }
}
