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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.business.dto.ExamineShiftListDTO;
import com.jpsoft.shinestar.modules.business.dto.OwnerShiftDTO;
import com.jpsoft.shinestar.modules.business.entity.*;
import com.jpsoft.shinestar.modules.business.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.TypeNameUtil;
import com.jpsoft.shinestar.modules.workflow.entity.Procinst;
import com.jpsoft.shinestar.modules.workflow.entity.ProcinstActUser;
import com.jpsoft.shinestar.modules.workflow.entity.ProcinstActivity;
import com.jpsoft.shinestar.modules.workflow.service.ProcinstActUserService;
import com.jpsoft.shinestar.modules.workflow.service.ProcinstActivityService;
import com.jpsoft.shinestar.modules.workflow.service.ProcinstService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 墨鱼_mo
 * @date 2021-5-6 9:39
 */
@Slf4j
@RestController
@RequestMapping("/mobile/examineApi")
@Api(description = "审核接口")
public class ExamineApiController {

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private CarryThingsOutFactoryService carryThingsOutFactoryService;

    @Autowired
    private SupplementWorkService supplementWorkService;

    @Autowired
    private WorkOverService workOverService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private CustomerDiningRecordService customerDiningRecordService;

    @Autowired
    private RequestForLeaveInfoService requestForLeaveInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private BusinessOutService businessOutService;

    @Autowired
    private AdjustShiftPersonService adjustShiftPersonService;

    @Autowired
    private ProcinstService procinstService;

    @Autowired
    private ProcinstActivityService procinstActivityService;

    @Autowired
    private AdjustShiftService adjustShiftService;

    @Autowired
    private ProcinstActUserService procinstActUserService;

    @Autowired
    private YearRequestForLeaveInfoService yearRequestForLeaveInfoService;



    @ApiOperation(value = "根据登录人查询待审核的调班记录")
    @PostMapping(value = "waitExamineShiftList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "procinstType", value = "类型（1：调班，2：请假，3：出差，4：补卡，5：批量补卡，6：批量加班，7：加班，8：携物出厂，9：接待就餐）",required = false,paramType = "form")
    })
    public MessageResult<Map>  waitExamineShiftList(String procinstType, String token, @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());

            Map<String,Object> searchParams = new HashMap<>();
            //类型
            if (StringUtils.isNotBlank(procinstType)){
                searchParams.put("procinstType",procinstType);
            }
            //状态（0：非当前操作步骤，1：当前操作步骤）
            searchParams.put("status","1");
            //审核人
            searchParams.put("userId",person.getId());
            //审核状态0：待审核，1：同意，2：退回，3：不同意
            searchParams.put("action","0");
            //审核人类型（1：审核，2：抄送）
            searchParams.put("type","1");

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("a.create_time","desc"));
            Page<ProcinstActUser> page = procinstActUserService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

            List<ExamineShiftListDTO> list = new ArrayList<>();

            List<ProcinstActUser> procinstActUserList = page.getResult();
            if (procinstActUserList.size()>0){
                for (ProcinstActUser procinstActUser : procinstActUserList){
                    ProcinstActivity procinstActivity = procinstActivityService.get(procinstActUser.getProcinstActivityId());
                    Procinst procinst = procinstService.get(procinstActivity.getProcinstId());
                    if (StringUtils.isBlank(procinstType)){
                        ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                        examineShiftListDTO.setId(procinst.getBusinessKey());
                        examineShiftListDTO.setProcinstType(procinst.getType());
                        examineShiftListDTO.setProcinstTypeName(TypeNameUtil.getOperationNameStr(procinst.getType()));
                        examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                        examineShiftListDTO.setCreateTime(DateUtil.format(procinst.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));

                        if ("1".equals(procinst.getType())){
                            AdjustShift adjustShift = adjustShiftService.get(procinst.getBusinessKey());

                            if(adjustShift!=null) {
                                List<AdjustShiftPerson> adjustShiftPersonList = adjustShiftPersonService.findByAdjustShiftId(adjustShift.getId());

                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(adjustShiftPersonList.get(0).getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("2".equals(procinst.getType())){
                            RequestForLeaveInfo requestForLeaveInfo = requestForLeaveInfoService.get(procinst.getBusinessKey());

                            if(requestForLeaveInfo!=null) {
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(requestForLeaveInfo.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("3".equals(procinst.getType())){
                            BusinessOut businessOut = businessOutService.get(procinst.getBusinessKey());

                            if(businessOut!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(businessOut.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("4".equals(procinst.getType())){
                            SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                            if(supplementWork!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(supplementWork.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("5".equals(procinst.getType())){
                            SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                            if(supplementWork!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(supplementWork.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())){
                            WorkOver workOver = workOverService.get(procinst.getBusinessKey());

                            if(workOver!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(workOver.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("8".equals(procinst.getType())){
                            CarryThingsOutFactory carryThingsOutFactory = carryThingsOutFactoryService.get(procinst.getBusinessKey());

                            if(carryThingsOutFactory!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(carryThingsOutFactory.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("9".equals(procinst.getType())){
                            CustomerDiningRecord customerDiningRecord = customerDiningRecordService.get(procinst.getBusinessKey());

                            if(customerDiningRecord!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(customerDiningRecord.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        if ("11".equals(procinst.getType())){
                            YearRequestForLeaveInfo yearRequestForLeaveInfo = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());

                            if(yearRequestForLeaveInfo!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(yearRequestForLeaveInfo.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                            }
                        }
                        list.add(examineShiftListDTO);

                    }

                    //调班
                    if ("1".equals(procinstType)){
                        AdjustShift adjustShift = adjustShiftService.get(procinst.getBusinessKey());

                        if(adjustShift!=null) {
                            List<AdjustShiftPerson> adjustShiftPersonList = adjustShiftPersonService.findByAdjustShiftId(adjustShift.getId());

                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(adjustShiftPersonList.get(0).getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());

                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(adjustShift.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            Date adjustTime = DateUtil.parse(adjustShift.getAdjustDate(), "yyyy-MM-dd");

                            Week week = DateUtil.dayOfWeekEnum(adjustTime);
                            examineShiftListDTO.setAdjustDate(DateUtil.format(adjustTime, "yyyy-MM-dd") + " " + week.toChinese());
                            examineShiftListDTO.setCreateTime(DateUtil.format(adjustShift.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            list.add(examineShiftListDTO);
                        }
                    }
                    //请假
                    if ("2".equals(procinstType)){
                        RequestForLeaveInfo requestForLeaveInfo = requestForLeaveInfoService.get(procinst.getBusinessKey());

                        if(requestForLeaveInfo!=null) {
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(requestForLeaveInfo.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(requestForLeaveInfo.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(requestForLeaveInfo.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setTypeN(TypeNameUtil.getLeaveTypeName(requestForLeaveInfo.getLeaveType()));
                            examineShiftListDTO.setStartTime(DateUtil.format(requestForLeaveInfo.getStartTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setStartTimeQuantum(requestForLeaveInfo.getStartTimeQuantum());
                            examineShiftListDTO.setEndTime(DateUtil.format(requestForLeaveInfo.getEndTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setEndTimeQuantum(requestForLeaveInfo.getEndTimeQuantum());

                            list.add(examineShiftListDTO);
                        }
                    }

                    //出差
                    if ("3".equals(procinstType)){
                        BusinessOut businessOut = businessOutService.get(procinst.getBusinessKey());

                        if(businessOut!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(businessOut.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(businessOut.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(businessOut.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setDestination(businessOut.getDestination());
                            examineShiftListDTO.setStartTime(DateUtil.format(businessOut.getStartTime(), "yyyy-MM-dd"));

                            list.add(examineShiftListDTO);
                        }

                    }
                    //补卡
                    if ("4".equals(procinstType)){
                        SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                        if(supplementWork!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(supplementWork.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(supplementWork.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(supplementWork.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setSupplementDate(supplementWork.getAttendanceDate());
                            list.add(examineShiftListDTO);
                        }
                    }
                    //批量补卡
                    if ("5".equals(procinstType)){
                        SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                        if(supplementWork!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(supplementWork.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(supplementWork.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(supplementWork.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setSupplementDate(supplementWork.getAttendanceDate());
                            list.add(examineShiftListDTO);
                        }
                    }
                    //批量加班
                    if ("6".equals(procinstType) || "7".equals(procinstType)){
                        WorkOver workOver = workOverService.get(procinst.getBusinessKey());

                        if(workOver!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(workOver.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(workOver.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(workOver.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setHours(workOver.getHours());
                            examineShiftListDTO.setStartTime(DateUtil.format(workOver.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                            examineShiftListDTO.setEndTime(DateUtil.format(workOver.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
                            list.add(examineShiftListDTO);
                        }
                    }

                    //批量加班
                    if ("8".equals(procinstType)){
                        CarryThingsOutFactory carryThingsOutFactory = carryThingsOutFactoryService.get(procinst.getBusinessKey());

                        if(carryThingsOutFactory!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(carryThingsOutFactory.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(carryThingsOutFactory.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(carryThingsOutFactory.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setAdjustDate(DateUtil.format(carryThingsOutFactory.getLeaveFactoryTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setAction(carryThingsOutFactory.getAuditStatus());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(carryThingsOutFactory.getAuditStatus()));
                            list.add(examineShiftListDTO);
                        }
                    }

                    //接待就餐
                    if ("9".equals(procinstType)){
                        CustomerDiningRecord customerDiningRecord = customerDiningRecordService.get(procinst.getBusinessKey());

                        if(customerDiningRecord!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(customerDiningRecord.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                            CompanyInfo companyInfo = companyInfoService.findByIdIgnoreDel(customerDiningRecord.getCompanyId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(customerDiningRecord.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());
                            examineShiftListDTO.setCompanyName(companyInfo.getName());
                            examineShiftListDTO.setDiningName(customerDiningRecord.getDiningName());
                            examineShiftListDTO.setCreateTime(DateUtil.format(customerDiningRecord.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setAction(customerDiningRecord.getStatus());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(customerDiningRecord.getStatus()));
                            list.add(examineShiftListDTO);
                        }
                    }

                    //年假
                    if ("11".equals(procinstType)){
                        YearRequestForLeaveInfo yearRequestForLeaveInfo = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());

                        if(yearRequestForLeaveInfo!=null) {
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(yearRequestForLeaveInfo.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(yearRequestForLeaveInfo.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(yearRequestForLeaveInfo.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setStartTime(DateUtil.format(yearRequestForLeaveInfo.getStartTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setStartTimeQuantum(yearRequestForLeaveInfo.getStartTimeQuantum());
                            examineShiftListDTO.setEndTime(DateUtil.format(yearRequestForLeaveInfo.getEndTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setEndTimeQuantum(yearRequestForLeaveInfo.getEndTimeQuantum());

                            list.add(examineShiftListDTO);
                        }
                    }

                }

            }


            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;
    }


    @ApiOperation(value = "根据登录人查询已审核的调班记录")
    @PostMapping(value = "alreadyExamineShiftList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "procinstType", value = "类型（1：调班，2：请假）",required = false,paramType = "form")
    })
    public MessageResult<Map> alreadyExamineShiftList(String procinstType,String name,String token, @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());

            Map<String,Object> searchParams = new HashMap<>();
            //类型
            if (StringUtils.isNotBlank(procinstType)){
                searchParams.put("procinstType",procinstType);
            }
            if (StrUtil.isNotBlank(name)){
                searchParams.put("name","%"+name+"%");
            }

            //状态（0：非当前操作步骤，1：当前操作步骤）
         //   searchParams.put("status","1");
            //审核人
            searchParams.put("userId",person.getId());
            //非待审核状态
            searchParams.put("noAction","0");
            //审核人类型（1：审核，2：抄送）
            searchParams.put("type","1");

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("a.finish_time","desc"));
            Page<ProcinstActUser> page = procinstActUserService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

            List<ExamineShiftListDTO> list = new ArrayList<>();

            List<ProcinstActUser> procinstActUserList = page.getResult();
            if (procinstActUserList.size()>0){
                for (ProcinstActUser procinstActUser : procinstActUserList){
                    ProcinstActivity procinstActivity = procinstActivityService.get(procinstActUser.getProcinstActivityId());
                    Procinst procinst = procinstService.get(procinstActivity.getProcinstId());

                    if (StringUtils.isBlank(procinstType)){
                        ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                        examineShiftListDTO.setId(procinst.getBusinessKey());
                        examineShiftListDTO.setProcinstType(procinst.getType());
                        examineShiftListDTO.setProcinstTypeName(TypeNameUtil.getOperationNameStr(procinst.getType()));
                        examineShiftListDTO.setCreateTime(DateUtil.format(procinst.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));

                        if ("1".equals(procinst.getType())){
                            AdjustShift adjustShift = adjustShiftService.get(procinst.getBusinessKey());

                            if (adjustShift!=null) {
                                List<AdjustShiftPerson> adjustShiftPersonList = adjustShiftPersonService.findByAdjustShiftId(adjustShift.getId());

                                PersonPopedom personPopedom1 = personPopedomService.get(adjustShiftPersonList.get(0).getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(adjustShift.getStatus().toString());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(adjustShift.getStatus()));
                            }
                        }
                        if ("2".equals(procinst.getType())){
                            RequestForLeaveInfo requestForLeaveInfo = requestForLeaveInfoService.get(procinst.getBusinessKey());

                            if(requestForLeaveInfo!=null) {
                                PersonInfo personInfo1 = personInfoService.get(requestForLeaveInfo.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(requestForLeaveInfo.getStatus().toString());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(requestForLeaveInfo.getStatus().toString()));
                            }
                        }
                        if ("3".equals(procinst.getType())){
                            BusinessOut businessOut = businessOutService.get(procinst.getBusinessKey());

                            if(businessOut!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.get(businessOut.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(businessOut.getStatus());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(businessOut.getStatus()));
                            }
                        }
                        if ("4".equals(procinst.getType())){
                            SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                            if(supplementWork!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.get(supplementWork.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(supplementWork.getStatus());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(supplementWork.getStatus()));
                            }
                        }
                        if ("5".equals(procinst.getType())){
                            SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                            if(supplementWork!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.get(supplementWork.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(supplementWork.getStatus());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(supplementWork.getStatus()));
                            }
                        }
                        if ("6".equals(procinst.getType()) || "7".equals(procinst.getType())){
                            WorkOver workOver = workOverService.get(procinst.getBusinessKey());

                            if(workOver!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.get(workOver.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(workOver.getStatus());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(workOver.getStatus()));
                            }
                        }
                        if ("8".equals(procinst.getType())){
                            CarryThingsOutFactory carryThingsOutFactory = carryThingsOutFactoryService.get(procinst.getBusinessKey());

                            if(carryThingsOutFactory!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.get(carryThingsOutFactory.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(carryThingsOutFactory.getAuditStatus());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(carryThingsOutFactory.getAuditStatus()));
                            }
                        }
                        if ("9".equals(procinst.getType())){
                            CustomerDiningRecord customerDiningRecord = customerDiningRecordService.get(procinst.getBusinessKey());

                            if(customerDiningRecord!=null) {
                                PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(customerDiningRecord.getPersonPopedomId());
                                PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(customerDiningRecord.getStatus());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(customerDiningRecord.getStatus()));
                            }
                        }
                        if ("11".equals(procinst.getType())){
                            YearRequestForLeaveInfo yearRequestForLeaveInfo = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());

                            if(yearRequestForLeaveInfo!=null) {
                                PersonInfo personInfo1 = personInfoService.get(yearRequestForLeaveInfo.getPersonId());
                                examineShiftListDTO.setName(personInfo1.getName());
                                examineShiftListDTO.setAction(yearRequestForLeaveInfo.getStatus().toString());
                                examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(yearRequestForLeaveInfo.getStatus().toString()));
                            }
                        }

                        list.add(examineShiftListDTO);
                    }

                    //调班
                    if ("1".equals(procinstType)){
                        AdjustShift adjustShift = adjustShiftService.get(procinst.getBusinessKey());

                        if(adjustShift!=null) {
                            List<AdjustShiftPerson> adjustShiftPersonList = adjustShiftPersonService.findByAdjustShiftId(adjustShift.getId());

                            PersonPopedom personPopedom1 = personPopedomService.get(adjustShiftPersonList.get(0).getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());

                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(adjustShift.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            Date adjustTime = DateUtil.parse(adjustShift.getAdjustDate(), "yyyy-MM-dd");
                            Week week = DateUtil.dayOfWeekEnum(adjustTime);
                            examineShiftListDTO.setAdjustDate(DateUtil.format(adjustTime, "yyyy-MM-dd") + " " + week.toChinese());
                            examineShiftListDTO.setCreateTime(DateUtil.format(adjustShift.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setAction(procinstActivity.getAction());
                            examineShiftListDTO.setActionName(TypeNameUtil.getActionName("1", procinstActivity.getAction()));
                            list.add(examineShiftListDTO);
                        }
                    }

                    if ("2".equals(procinstType)){
                        RequestForLeaveInfo requestForLeaveInfo = requestForLeaveInfoService.get(procinst.getBusinessKey());

                        if(requestForLeaveInfo!=null) {
                            // PersonInfo personInfo1 = personInfoService.get(requestForLeaveInfo.getPersonId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(requestForLeaveInfo.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(requestForLeaveInfo.getId());
                            examineShiftListDTO.setProcinstActUserId(requestForLeaveInfo.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(requestForLeaveInfo.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setTypeN(TypeNameUtil.getLeaveTypeName(requestForLeaveInfo.getLeaveType()));
                            examineShiftListDTO.setStartTime(DateUtil.format(requestForLeaveInfo.getStartTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setStartTimeQuantum(requestForLeaveInfo.getStartTimeQuantum());
                            examineShiftListDTO.setEndTime(DateUtil.format(requestForLeaveInfo.getEndTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setEndTimeQuantum(requestForLeaveInfo.getEndTimeQuantum());
                            examineShiftListDTO.setAction(requestForLeaveInfo.getStatus().toString());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(requestForLeaveInfo.getStatus().toString()));

                            list.add(examineShiftListDTO);
                        }
                    }

                    //出差
                    if ("3".equals(procinstType)){
                        BusinessOut businessOut = businessOutService.get(procinst.getBusinessKey());

                        if(businessOut!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.get(businessOut.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(businessOut.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(businessOut.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setDestination(businessOut.getDestination());
                            examineShiftListDTO.setStartTime(DateUtil.format(businessOut.getStartTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setAction(businessOut.getStatus());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(businessOut.getStatus()));

                            list.add(examineShiftListDTO);
                        }
                    }

                    //补卡
                    if ("4".equals(procinstType)){
                        SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                        if(supplementWork!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.get(supplementWork.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(supplementWork.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(supplementWork.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setSupplementDate(supplementWork.getAttendanceDate());
                            examineShiftListDTO.setAction(supplementWork.getStatus().toString());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(supplementWork.getStatus().toString()));
                            list.add(examineShiftListDTO);
                        }
                    }

                    //批量补卡
                    if ("5".equals(procinstType)){
                        SupplementWork supplementWork = supplementWorkService.get(procinst.getBusinessKey());

                        if(supplementWork!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.get(supplementWork.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(supplementWork.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(supplementWork.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setSupplementDate(supplementWork.getAttendanceDate());
                            examineShiftListDTO.setAction(supplementWork.getStatus().toString());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(supplementWork.getStatus().toString()));
                            list.add(examineShiftListDTO);
                        }
                    }

                    //批量加班
                    if ("6".equals(procinstType)|| "7".equals(procinstType)){
                        WorkOver workOver = workOverService.get(procinst.getBusinessKey());

                        if (workOver!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.get(workOver.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(workOver.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(workOver.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setHours(workOver.getHours());
                            examineShiftListDTO.setAction(workOver.getStatus().toString());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(workOver.getStatus().toString()));
                            examineShiftListDTO.setStartTime(DateUtil.format(workOver.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                            examineShiftListDTO.setEndTime(DateUtil.format(workOver.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
                            list.add(examineShiftListDTO);
                        }
                    }

                    //批量加班
                    if ("8".equals(procinstType)){
                        CarryThingsOutFactory carryThingsOutFactory = carryThingsOutFactoryService.get(procinst.getBusinessKey());

                        if(carryThingsOutFactory!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.get(carryThingsOutFactory.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.get(personPopedom1.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(carryThingsOutFactory.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(carryThingsOutFactory.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setAdjustDate(DateUtil.format(carryThingsOutFactory.getLeaveFactoryTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setAction(carryThingsOutFactory.getAuditStatus());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(carryThingsOutFactory.getAuditStatus()));
                            list.add(examineShiftListDTO);
                        }
                    }

                    //接待就餐
                    if ("9".equals(procinstType)){
                        CustomerDiningRecord customerDiningRecord = customerDiningRecordService.get(procinst.getBusinessKey());

                        if(customerDiningRecord!=null) {
                            PersonPopedom personPopedom1 = personPopedomService.findByIdIgnoreDel(customerDiningRecord.getPersonPopedomId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(personPopedom1.getPersonId());
                            CompanyInfo companyInfo = companyInfoService.findByIdIgnoreDel(customerDiningRecord.getCompanyId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(customerDiningRecord.getId());
                            examineShiftListDTO.setProcinstActUserId(procinstActUser.getId());
                            examineShiftListDTO.setName(personInfo1.getName());
                            examineShiftListDTO.setCompanyName(companyInfo.getName());
                            examineShiftListDTO.setDiningName(customerDiningRecord.getDiningName());
                            examineShiftListDTO.setCreateTime(DateUtil.format(customerDiningRecord.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setAction(customerDiningRecord.getStatus());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(customerDiningRecord.getStatus()));
                            list.add(examineShiftListDTO);
                        }
                    }


                    //年假
                    if ("11".equals(procinstType)){
                        YearRequestForLeaveInfo yearRequestForLeaveInfo = yearRequestForLeaveInfoService.get(procinst.getBusinessKey());

                        if(yearRequestForLeaveInfo!=null) {
                            // PersonInfo personInfo1 = personInfoService.get(requestForLeaveInfo.getPersonId());
                            PersonInfo personInfo1 = personInfoService.findByIdIgnoreDel(yearRequestForLeaveInfo.getPersonId());
                            ExamineShiftListDTO examineShiftListDTO = new ExamineShiftListDTO();
                            examineShiftListDTO.setId(yearRequestForLeaveInfo.getId());
                            examineShiftListDTO.setProcinstActUserId(yearRequestForLeaveInfo.getId());
                            examineShiftListDTO.setName(personInfo1.getName());

                            examineShiftListDTO.setCreateTime(DateUtil.format(yearRequestForLeaveInfo.getCreateTime(), "yyyy-MM-dd HH:mm"));
                            examineShiftListDTO.setStartTime(DateUtil.format(yearRequestForLeaveInfo.getStartTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setStartTimeQuantum(yearRequestForLeaveInfo.getStartTimeQuantum());
                            examineShiftListDTO.setEndTime(DateUtil.format(yearRequestForLeaveInfo.getEndTime(), "yyyy-MM-dd"));
                            examineShiftListDTO.setEndTimeQuantum(yearRequestForLeaveInfo.getEndTimeQuantum());
                            examineShiftListDTO.setAction(yearRequestForLeaveInfo.getStatus().toString());
                            examineShiftListDTO.setActionName(TypeNameUtil.getExamineTypeName(yearRequestForLeaveInfo.getStatus().toString()));

                            list.add(examineShiftListDTO);
                        }
                    }
                }

            }


            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;
    }


    @ApiOperation(value = "审核")
    @PostMapping(value = "examineSubmit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "procinstActUserIds", value = "ids",required = true,paramType = "form"),
            @ApiImplicitParam(name = "status", value = "审核状态（1：同意，2：退回，3：拒绝）",required = true,paramType = "form"),
            @ApiImplicitParam(name = "content", value = "审核意见",required = true,paramType = "form")
    })
    public MessageResult<Map> examineSubmit(String procinstActUserIds,String status,String content,String token, @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

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

            log.warn("examineSubmit(procinstActUserIds={},status={},content={})",procinstActUserIds,status,content);

            procinstActUserService.examineSubmit(procinstActUserIds,personPopedom,status,content);

            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;
    }
}
