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

import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.jz.db.domain.HmEmployClock;
import org.linlinjava.jz.db.domain.HmEmployRelation;
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.util.DateUtil;
import org.linlinjava.jz.service.HmEmployClockService;
import org.linlinjava.jz.service.NoticeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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("clock")
@Valid
public class EmployerClockController {

    @Resource
    private HmEmployClockService hmEmployClockService;
    @Resource
    private HmEmployTaskService hmEmployTaskService;
    @Resource
    private NoticeService noticeService;
    @Resource
    private HmEmployRelationService hmEmployRelationService;

    @ApiOperation(value = "出勤记录", notes = "出勤记录")
    @GetMapping(value = "list")
    public Object list(@NotNull @RequestParam Long employClockId) {
        HmEmployClock hmEmployClock = hmEmployClockService.queryById(employClockId);
        if (null == hmEmployClock) {
            return HmResponseUtil.fail_null();
        }

        List<Map<String, Object>> dayTask = hmEmployTaskService.queryByClockId(employClockId, 1, null);

        List<Map<String, Object>> weekTask = hmEmployTaskService.queryByClockId(employClockId, 2, null);

        List<Map<String, Object>> monthTask = hmEmployTaskService.queryByClockId(employClockId, 3, null);

        Map<String, Object> map = new HashMap<>(16);
        map.put("hmEmployClock", hmEmployClock);
        map.put("dayTask", dayTask);
        map.put("weekTask", weekTask);
        map.put("monthTask", monthTask);

        return HmResponseUtil.ok(map);
    }

    @ApiOperation(value = "质疑打卡记录-下拉框列表展示", notes = "质疑打卡-下拉框列表展示")
    @GetMapping(value = "record")
    public Object record(@NotNull @RequestParam Long employClockId) {
        HmEmployClock hmEmployClock = hmEmployClockService.queryById(employClockId);
        if (null == hmEmployClock) {
            return HmResponseUtil.fail_null();
        }

        List<Map<String, Object>> mapList = hmEmployTaskService.queryByClockId(employClockId, null, Lists.newArrayList(1, 2));

        return HmResponseUtil.ok(mapList);
    }

    @ApiOperation(value = "打卡质疑提交", notes = "打卡质疑提交")
    @GetMapping(value = "submit")
    public Object submit(@NotNull @RequestParam Long employClockId, @RequestParam(required = false) List<Long> taskIds) {
        HmEmployClock hmEmployClock = hmEmployClockService.queryById(employClockId);
        if (null == hmEmployClock) {
            return HmResponseUtil.fail_null();
        }

        hmEmployTaskService.createQuestionLog(employClockId, taskIds);

        HmEmployRelation hmEmployRelation = hmEmployRelationService.queryById(hmEmployClock.getEmployRelationId());

        HmNoticeDto hmNoticeDto = new HmNoticeDto();

        hmNoticeDto.setAppUserId(Lists.newArrayList(hmEmployRelation.getUserButlerId()));
//        hmNoticeDto.setContent("您对于" + hmEmployClock.getWorkData().toLocalDate() + "的保姆服务的质疑已通知管家，管家了解情况中！");
        hmNoticeDto.setEmployRelationId(hmEmployRelation.getId());
        hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_7.getType());
        hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_7.getNoticeUserType());
        hmNoticeDto.setrId(hmEmployClock.getId().toString());
        hmNoticeDto.setCreateUserId(hmEmployRelation.getUserEmployerId());
        hmNoticeDto.setRemark(DateUtil.DateFormatToyyyyMMdd(hmEmployClock.getWorkData()));

        noticeService.create(hmNoticeDto);

        return HmResponseUtil.ok();
    }

    @ApiOperation(value = "保姆评价记录", notes = "保姆评价记录")
    @GetMapping(value = "comment")
    public Object comment(@NotNull @RequestParam Long employRelationId, @NotBlank @RequestParam String date) {
        HmEmployRelation relation = hmEmployRelationService.queryById(employRelationId);
        if (relation == null) {
            return HmResponseUtil.fail_null();
        }

        List<Map<String, Object>> mapList = hmEmployClockService.queryByMonth(employRelationId, date);

        return HmResponseUtil.ok(mapList);
    }

    @ApiOperation(value = "请假审批")
    @GetMapping(path = "approval")
    public Object approval(@NotNull @RequestParam Long employClockId, @NotNull @RequestParam Integer approvalState, @RequestParam String approvalReason) {
        HmEmployClock hmEmployClock = hmEmployClockService.queryById(employClockId);
        if (null == hmEmployClock) {
            return HmResponseUtil.fail_null();
        }

        HmEmployRelation hmEmployRelation = hmEmployRelationService.queryById(hmEmployClock.getEmployRelationId());

        HmNoticeDto hmNoticeDto = new HmNoticeDto();
        hmNoticeDto.setAppUserId(Lists.newArrayList(hmEmployRelation.getUserServantId()));
        hmNoticeDto.setEmployRelationId(hmEmployRelation.getId());
        hmNoticeDto.setrId(hmEmployClock.getId().toString());
        hmNoticeDto.setCreateUserId(hmEmployRelation.getUserEmployerId());

        if (HmEmployClockEnum.APPROVAL_STATE_1 == approvalState) {
            hmEmployClock.setStatus(HmEmployClockEnum.STATUS_4.getType());
            hmNoticeDto.setContent("雇主请假审批通过");
            hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_2.getType());
            hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_2.getNoticeUserType());
        } else {
            hmEmployClock.setStatus(HmEmployClockEnum.STATUS_0.getType());
            hmNoticeDto.setContent("雇主请假审批不通过");
            hmNoticeDto.setRtype(HmNoticeEnum.NOTICE_NUM_3.getType());
            hmNoticeDto.setNoticeUserType(HmNoticeEnum.NOTICE_NUM_3.getNoticeUserType());
        }

        noticeService.create(hmNoticeDto);

        hmEmployClock.setApprovalState(approvalState);
        hmEmployClock.setApprovalReason(approvalReason);
        hmEmployClock.setUpdateTime(LocalDateTime.now());

        hmEmployClockService.update(hmEmployClock);
        return HmResponseUtil.ok();
    }

}
