package com.jpsoft.shinestar.modules.mobile.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.config.WxConfig;
import com.jpsoft.shinestar.modules.base.dto.ApprovalDTO;
import com.jpsoft.shinestar.modules.base.dto.BusinessOutDTO;
import com.jpsoft.shinestar.modules.base.dto.WorkOverDTO;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.business.dto.ArrangeShiftListDTO;
import com.jpsoft.shinestar.modules.business.entity.AdjustShift;
import com.jpsoft.shinestar.modules.business.entity.AdjustShiftPerson;
import com.jpsoft.shinestar.modules.business.entity.SupplementWork;
import com.jpsoft.shinestar.modules.business.entity.SupplementWorkPerson;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.*;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 墨鱼_mo
 * @date 2020-12-24 9:31
 */
@RestController
@RequestMapping("/mobile/workOver")
@Slf4j
public class WorkOverApiController {

    @Value("${jwt.secret}")
    private String jwtSecret;

    @Autowired
    private WxConfig wxConfig;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private WorkOverPersonService workOverPersonService;

    @Autowired
    private WorkOverService workOverService;

    @ApiOperation(value = "加班申请数据提交")
    @RequestMapping(value = "submitWorkOver", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "reason", value = "加班原因", required = true, paramType = "form"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "hours", value = "时长（小时）", required = true, paramType = "form"),
            @ApiImplicitParam(name = "approvalPersonId", value = "审核人", required = true, paramType = "form")
    })
    public MessageResult<Map> submitWorkOver(String reason, String startTime, String endTime, Integer hours, String approvalPersonId, @RequestAttribute String subject, String token) {

        MessageResult<Map> msgResult = new MessageResult<>();
        try{

            //申请人
            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());
            long needHours =  DateUtil.between(DateUtil.parse(startTime),DateUtil.parse(endTime), DateUnit.HOUR);

             if (hours<=0){
                 throw new Exception("加班时间有误");
             }



           //创建申请记录
            WorkOver workOver = new WorkOver();
            workOver.setId(UUID.randomUUID().toString());
            workOver.setPersonPopedomId(personPopedom.getId());
            workOver.setStartTime(DateUtil.parse(startTime));
            workOver.setEndTime(DateUtil.parse(endTime));
            workOver.setHours(hours);
            workOver.setStatus("0");
            workOver.setApprovalPersonPopedomId(approvalPersonId);
            workOver.setReason(reason);
            workOver.setApprovalNo(StringUtils.getApprovalNo());
            workOver.setCreateBy(personPopedom.getId());
            workOver.setCreateTime(new Date());
            workOverService.insert(workOver);

            new Thread(() -> {
                try {

                    //审核人
                    PersonPopedom approvalPersonPopedom = personPopedomService.get(approvalPersonId);
                    PersonInfo approvalPerson = personInfoService.get(approvalPersonPopedom.getPersonId());
                    String typeN = "加班申请";
                    String url = "master/extraWork/verifyInfo?id=";

                    String accessToken = JwtUtil2.createToken(jwtSecret, approvalPerson.getId() + "", DateTime.now().plusHours(30 * 24).toDate());

                    WechatMessageUtil.sendEvenMessage(approvalPerson.getOpenId(), "有一条新的" + typeN + "信息待审核", personInfo.getName(), workOver.getApprovalNo(), "前往处理", wxConfig.getPortalUrl() + url  + workOver.getId() + "&wx=1&exchange=1&role_id=" + approvalPersonId + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());

                } catch (Exception ex) {
                    log.error(ex.getMessage(), ex);
                }
            }).start();

            Map<String, Object> map = new HashMap<>();
            map.put("id", workOver.getId());
            msgResult.setData(map);
            msgResult.setCode(200);
            msgResult.setResult(true);
            msgResult.setMessage("提交成功");


        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            msgResult.setCode(400);
            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;


    }



    @ApiOperation(value = "加班申请数据列表")
    @RequestMapping(value = "workOverList", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> workOverList(@RequestAttribute String subject, String token, @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {

        MessageResult<Map> msgResult = new MessageResult<>();

        try{
            //操作人
            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());

            Map<String,Object> searchParam = new HashMap<>();
            searchParam.put("personPopedomId",personPopedom.getId());
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("a.create_time","desc"));

            Page<WorkOver> page = workOverService.pageSearch(searchParam,pageIndex,pageSize,true,sortList);

            List<WorkOver> workOverList = page.getResult();
            List<WorkOverDTO> list = new ArrayList<>();
            if (workOverList.size()>0){
                for (WorkOver workOver : workOverList){
                    WorkOverDTO workOverDTO = new WorkOverDTO();
                    workOverDTO.setId(workOver.getId());
                    workOverDTO.setPersonName(personInfo.getName());
                    workOverDTO.setDateTime(DateUtil.format(workOver.getCreateTime(),"yyyy-MM-dd HH:mm"));
                    workOverDTO.setStatus(workOver.getStatus());
                    workOverDTO.setStatusN(workOver.getStatusN());
                    list.add(workOverDTO);
                }
            }
            Map<String,Object> pageMap = new HashMap<>();
            pageMap.put("recordsTotal",page.getTotal());
            pageMap.put("recordsFiltered",page.getTotal());
            pageMap.put("totalPage",page.getPages());
            pageMap.put("pageNumber",page.getPageNum());
            pageMap.put("pageSize",page.getPageSize());
            pageMap.put("data", list);

            msgResult.setData(pageMap);
            msgResult.setCode(200);
            msgResult.setResult(true);

        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            msgResult.setCode(400);
            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;

    }

    @ApiOperation(value = "加班申请数据详情")
    @RequestMapping(value = "workOverDetail", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "form")

    })
    public MessageResult<WorkOverDTO> workOverDetail(@RequestAttribute String subject, String token,String id) {

        MessageResult<WorkOverDTO> msgResult = new MessageResult<>();

        try{
            //操作人
            PersonInfo personInfo0 = personInfoService.get(Long.valueOf(subject));
            PersonPopedom personPopedom0 = personPopedomService.get(personInfo0.getPopedom());

            WorkOver workOver = workOverService.get(id);
            if (workOver == null){
                throw new Exception("此加班申请记录不存在");
            }
            //申请人角色
            PersonPopedom personPopedom = personPopedomService.get(workOver.getPersonPopedomId());
            PersonInfo personInfo = personInfoService.get(personPopedom.getPersonId());
            //审核人
            PersonPopedom approvalPersonPopedom = personPopedomService.get(workOver.getApprovalPersonPopedomId());

            PersonInfo approvalPersonInfo = null;
            if (approvalPersonPopedom != null) {
                approvalPersonInfo = personInfoService.get(approvalPersonPopedom.getPersonId());
            }


            //申请人公司
        //    CompanyInfo companyInfo = companyInfoService.get(personPopedom.getCompanyId());


            WorkOverDTO workOverDTO = new WorkOverDTO();
            workOverDTO.setPersonName(personInfo.getName());
            workOverDTO.setFaceImageUrl(personInfo.getFaceImageUrl());
            workOverDTO.setStatus(workOver.getStatus());
            workOverDTO.setStatusN(workOver.getStatusN());
            workOverDTO.setWorkOverTime(DateUtil.format(workOver.getStartTime(),"yyyy-MM-dd HH:mm") + "~" + DateUtil.format(workOver.getEndTime(),"yyyy-MM-dd HH:mm"));
            workOverDTO.setHours(workOver.getHours());
            workOverDTO.setReason(workOver.getReason());
            workOverDTO.setDateTime(DateUtil.format(workOver.getCreateTime(),"yyyy-MM-dd HH:mm"));




            //审批人
            List<String> personPopedomList = null;
            if (org.apache.commons.lang3.StringUtils.isNotBlank(workOver.getApprovalPersonIdStr())) {
                personPopedomList = Arrays.asList(workOver.getApprovalPersonIdStr().split(","));
            }
            //审批时间
            List<String> approvalTimeList = null;
            if (org.apache.commons.lang3.StringUtils.isNotBlank(workOver.getApprovalTimeStr())) {
                approvalTimeList = Arrays.asList(workOver.getApprovalTimeStr().split(","));
            }

            //审批内容
            List<String> approvalContentList = null;
            if (org.apache.commons.lang3.StringUtils.isNotBlank(workOver.getApprovalContentStr())) {
                approvalContentList = Arrays.asList(workOver.getApprovalContentStr().split(","));
            }

            //审批状态
            List<String> statusList = null;
            if (org.apache.commons.lang3.StringUtils.isNotBlank(workOver.getApprovalStatusStr())) {
                statusList = Arrays.asList(workOver.getApprovalStatusStr().split(","));
            }


            List<ApprovalDTO> approvalDTOList = new ArrayList<>();


            if (personPopedomList != null && personPopedomList.size() != 0) {
                Integer n = personPopedomList.size();

                for (int i = 0; i < n; i++) {
                    ApprovalDTO approvalDTO = new ApprovalDTO();

                    //审核人
                    PersonPopedom personPopedom1 = personPopedomService.get(personPopedomList.get(i));

                    if (personPopedom1 != null) {


                        PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                        approvalDTO.setName(personInfo1.getName());
                        approvalDTO.setFaceImageUrl(personInfo1.getFaceImageUrl());
                        approvalDTO.setStatus(statusList.get(i));
                        if ("0".equals(statusList.get(i))) {
                            approvalDTO.setStatusN("待审核");
                        }
                        if ("1".equals(statusList.get(i))) {
                            approvalDTO.setStatusN("同意");
                            if (approvalTimeList != null) {
                                approvalDTO.setApprovalContent(approvalContentList.get(i));
                                approvalDTO.setTime(approvalTimeList.get(i));
                            }
                        }
                        if ("2".equals(statusList.get(i))) {
                            approvalDTO.setStatusN("拒绝");
                            if (approvalTimeList != null) {
                                approvalDTO.setApprovalContent(approvalContentList.get(i));
                                approvalDTO.setTime(approvalTimeList.get(i));
                            }
                        }
                        approvalDTOList.add(approvalDTO);

                    }
                }
            }

            ApprovalDTO approvalDTO0 = new ApprovalDTO();

            if (approvalPersonInfo != null) {
                approvalDTO0.setName(approvalPersonInfo.getName());
                approvalDTO0.setFaceImageUrl(approvalPersonInfo.getFaceImageUrl());
            }

            if (workOver.getApprovalTime() != null) {
                approvalDTO0.setTime(DateUtil.format(workOver.getApprovalTime(), "yyyy-MM-dd HH:mm"));
            }
            approvalDTO0.setStatus(workOver.getStatus());
            approvalDTO0.setStatusN(workOver.getStatusN());
            approvalDTO0.setApprovalContent(workOver.getApprovalContent());
            approvalDTOList.add(approvalDTO0);


            workOverDTO.setList(approvalDTOList);

            boolean approvalEnable = false;
            if (personPopedom0.getId().equals(workOver.getApprovalPersonPopedomId())) {
                approvalEnable = true;
            }

            workOverDTO.setApprovalEnable(approvalEnable);


            msgResult.setData(workOverDTO);
            msgResult.setCode(200);
            msgResult.setResult(true);




        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            msgResult.setCode(400);
            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;

    }


    @ApiOperation(value = "加班申请审核数据列表")
    @RequestMapping(value = "workOverApprovalList", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "未处理/已处理（0/1）", required = true, paramType = "form")
    })
    public MessageResult<Map> workOverApprovalList(@RequestAttribute String subject, String token,String status, @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex, @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {


        MessageResult<Map> msgResult = new MessageResult<>();

        try{

            //操作人
            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());


            Map<String,Object> pageMap = new HashMap<>();
            Page<WorkOver> page = new Page<>();
            if ("0".equals(status)){
                Map<String,Object> searchParams = new HashMap<>();
                searchParams.put("status","0");
                searchParams.put("approvalPersonPopedomId",personPopedom.getId());
                List<Sort> sortList = new ArrayList<>();
                sortList.add(new Sort("a.create_time","desc"));
                 page = workOverService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);


            }
            if("1".equals(status)){

                //操作人的公司
                CompanyInfo companyInfo = companyInfoService.get(personPopedom.getCompanyId());
                List<CompanyInfo> companyInfoList = companyInfoService.findAllCompanyByCode("%"+companyInfo.getId()+"%");
                Map<String,Object> searchParams = new HashMap<>();
                searchParams.put("notStatus","0");
                searchParams.put("inCompanyInfo",companyInfoList);
                List<Sort> sortList = new ArrayList<>();
                sortList.add(new Sort("a.approval_time","desc"));
                 page = workOverService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);
            }

            List<WorkOver> workOverList = page.getResult();
            List<WorkOverDTO> list = new ArrayList<>();
            if (workOverList.size()>0){
                for (WorkOver workOver : workOverList){
                    //申请人
                    PersonPopedom personPopedom1 = personPopedomService.get(workOver.getPersonPopedomId());
                    PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                    WorkOverDTO workOverDTO = new WorkOverDTO();
                    workOverDTO.setId(workOver.getId());
                    workOverDTO.setPersonName(personInfo1.getName());
                    workOverDTO.setDateTime(DateUtil.format(workOver.getCreateTime(),"yyyy-MM-dd HH:mm"));
                    workOverDTO.setWorkOverTime(DateUtil.format(workOver.getStartTime(),"yyyy-MM-dd HH:mm") + "~" + DateUtil.format(workOver.getEndTime(),"yyyy-MM-dd HH:mm"));
                    workOverDTO.setHours(workOver.getHours());
                    workOverDTO.setStatus(workOver.getStatus());
                    workOverDTO.setStatusN(workOver.getStatusN());
                    list.add(workOverDTO);
                }
            }


            pageMap.put("recordsTotal",page.getTotal());
            pageMap.put("recordsFiltered",page.getTotal());
            pageMap.put("totalPage",page.getPages());
            pageMap.put("pageNumber",page.getPageNum());
            pageMap.put("pageSize",page.getPageSize());
            pageMap.put("data", list);



            msgResult.setData(pageMap);
            msgResult.setCode(200);
            msgResult.setResult(true);


        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            msgResult.setCode(400);
            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return  msgResult;

    }



    @ApiOperation(value = "加班申请审核")
    @RequestMapping(value = "approvalWorkOver", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "ids", value = "ids", required = true, paramType = "form"),
            @ApiImplicitParam(name = "status", value = "同意/拒绝（1/2）", required = true, paramType = "form"),
            @ApiImplicitParam(name = "approvalContent", value = "审核内容", required = true, paramType = "form")
    })
    public MessageResult<Map> approvalWorkOver(@RequestAttribute String subject, String token,String ids,String status, String approvalContent) {

        MessageResult<Map> msgResult = new MessageResult<>();
        try{

            //操作人
            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());
            if (org.apache.commons.lang3.StringUtils.isBlank(ids)){
                throw new Exception("请选择要审核的记录");
            }

            if (org.apache.commons.lang3.StringUtils.isNotBlank(approvalContent)){
                approvalContent = StrUtil.replace(approvalContent,",","，");
            }
            workOverService.approvalWorkOver(personInfo,ids,status,approvalContent);

            msgResult.setCode(200);
            msgResult.setResult(true);

        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            msgResult.setCode(400);
            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;

    }


    @ApiOperation(value = "批量申请加班提交")
    @PostMapping(value = "workOverPathSubmit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "reason", value = "加班原因", required = true, paramType = "form"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "hours", value = "时长（小时）", required = true, paramType = "form"),
            @ApiImplicitParam(name = "personIds", value = "加班人(不传就是个人申请加班)", required = false, paramType = "form"),
            @ApiImplicitParam(name = "type", value = "1:个人申请，2：批量申请，3：上级安排", required = true, paramType = "form"),
            @ApiImplicitParam(name = "formId", value = "页面路径", required = true, paramType = "form"),
            @ApiImplicitParam(name = "draft", value = "状态（0：草稿，1：提交）", required = true, paramType = "form")
    })
    public MessageResult<Map> workOverPathSubmit(String ids,String draft,String reason, String startTime, String endTime, Integer hours,String personIds,String type,String formId,String processTemplateId,String token, @RequestAttribute String subject) {


        MessageResult<Map> messageResult = new MessageResult<>();
        try {

            PersonInfo person = personInfoService.get(Long.parseLong(subject));
            PersonPopedom personPopedom = personPopedomService.get(person.getPopedom());

            String id = workOverService.workOverPathSubmit(personPopedom, hours,startTime,endTime,reason,personIds,formId,type,ids,draft,processTemplateId);

            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            messageResult.setData(map);
            messageResult.setCode(200);
            messageResult.setResult(true);


        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);

        }

        return messageResult;
    }

    @ApiOperation(value = "根据id查询批量加班详情")
    @PostMapping(value = "workOverBatchDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "form")
    })
    public MessageResult<Map> workOverBatchDetail(String id, String token, @RequestAttribute String subject) {


        MessageResult<Map> messageResult = new MessageResult<>();
        try {

            PersonInfo person = personInfoService.get(Long.parseLong(subject));
            PersonPopedom personPopedom = personPopedomService.get(person.getPopedom());



            //主业务
            WorkOver workOver = workOverService.get(id);

            String name = "";
            String nameId = "";
            //子业务表
            List<WorkOverPerson> workOverPersonList = workOverPersonService.findByWorkOverId(workOver.getId());
            if (workOverPersonList.size()>0){
                for (WorkOverPerson workOverPerson : workOverPersonList){
                    PersonPopedom personPopedom1 = personPopedomService.get(workOverPerson.getPersonPopedomId());
                    if (personPopedom1 != null){
                        PersonInfo personInfo = personInfoService.get(personPopedom1.getPersonId());
                        if (org.apache.commons.lang3.StringUtils.isBlank(name)){
                            name = personInfo.getName();
                            nameId = String.valueOf(personInfo.getId());
                        }else {
                            name = name + "," + personInfo.getName();
                            nameId = nameId + "," + personInfo.getId();
                        }
                    }
                }
            }
            Map<String, Object> map = new HashMap<>();
            map.put("name", name);
            map.put("reason",workOver.getReason());
            map.put("status", workOver.getStatus());
            map.put("statusName", TypeNameUtil.getExamineTypeName(workOver.getStatus().toString()));
            map.put("createTime",DateUtil.format(workOver.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            map.put("startTime",DateUtil.format(workOver.getStartTime(),"yyyy-MM-dd HH:mm:ss"));
            map.put("endTime",DateUtil.format(workOver.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
            map.put("hours",workOver.getHours());
            map.put("nameId",nameId);

            String durationStr = null;
            if (workOverPersonList.size() ==1){
                WorkOverPerson workOverPerson = workOverPersonList.get(0);
                durationStr = workOverPerson.getDurationStr();
            }
            map.put("durationStr",durationStr);

            messageResult.setData(map);
            messageResult.setCode(200);
            messageResult.setResult(true);

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);

        }
        return messageResult;
    }

    @ApiOperation(value = "批量加班申请记录")
    @PostMapping(value = "workOverBatchList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "type", value = "类型（2：批量申请，3：上级安排）", required = true, paramType = "form")

    })
    public MessageResult<Map> workOverBatchList(String token, String type,@RequestAttribute String subject, @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {


        MessageResult<Map> messageResult = new MessageResult<>();
        try {

            PersonInfo person = personInfoService.get(Long.parseLong(subject));
            PersonPopedom personPopedom = personPopedomService.get(person.getPopedom());

            List<ArrangeShiftListDTO> list = new ArrayList<>();
            Map<String, Object> searchParams = new HashMap<>();
            searchParams.put("personPopedomId", personPopedom.getId());
            searchParams.put("type",type);
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("create_time", "desc"));
            Page<WorkOver> page = workOverService.pageSearch(searchParams, pageIndex, pageSize, true, sortList);

            List<WorkOver> workOverList = page.getResult();
            if (workOverList.size()>0){
                for (WorkOver workOver : workOverList){
                    List<WorkOverPerson> workOverPersonList = workOverPersonService.findByWorkOverId(workOver.getId());
                    WorkOverPerson workOverPerson = workOverPersonList.get(0);
                    PersonPopedom personPopedom1 = personPopedomService.get(workOverPerson.getPersonPopedomId());
                    PersonInfo personInfo = personInfoService.get(personPopedom1.getPersonId());
                    ArrangeShiftListDTO arrangeShiftListDTO = new ArrangeShiftListDTO();
                    arrangeShiftListDTO.setId(workOver.getId());
                    arrangeShiftListDTO.setName(personInfo.getName()+"等"+workOverPersonList.size()+"人");
                  //  arrangeShiftListDTO.setAdjustDate(workOver.getAdjustDate());
                    arrangeShiftListDTO.setSubmitDate(DateUtil.format(workOver.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
                    arrangeShiftListDTO.setStatus(workOver.getStatus());
                    arrangeShiftListDTO.setStatusName(TypeNameUtil.getExamineTypeName(workOver.getStatus()));
                    arrangeShiftListDTO.setHours(workOver.getHours());
                    list.add(arrangeShiftListDTO);

                }
            }

            Map<String, Object> pageMap = new HashMap<>();
            pageMap.put("recordsTotal", page.getTotal());
            pageMap.put("recordsFiltered", page.getTotal());
            pageMap.put("totalPage", page.getPages());
            pageMap.put("pageNumber", page.getPageNum());
            pageMap.put("pageSize", page.getPageSize());
            pageMap.put("data", list);

            messageResult.setData(pageMap);
            messageResult.setCode(200);
            messageResult.setResult(true);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);

        }

        return messageResult;
    }


}
