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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.util.StringUtil;
import com.jpsoft.shinestar.modules.base.dto.ApprovalDTO;
import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonPopedom;
import com.jpsoft.shinestar.modules.base.entity.PersonRoleInfo;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.base.service.PersonRoleInfoService;
import com.jpsoft.shinestar.modules.business.dto.YearRequestForLeaveDetailDTO;
import com.jpsoft.shinestar.modules.business.dto.YearRequestForLeaveInfoDTO;
import com.jpsoft.shinestar.modules.business.entity.YearRequestForLeaveInfo;
import com.jpsoft.shinestar.modules.business.service.YearRequestForLeaveInfoService;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import com.jpsoft.shinestar.modules.common.utils.TypeNameUtil;
import com.jpsoft.shinestar.modules.sys.entity.DataDictionary;
import com.jpsoft.shinestar.modules.sys.service.DataDictionaryService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/mobile/yearRequestForLeaveInfoApi")
@Slf4j
public class YearRequestForLeaveInfoApiController {


    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonRoleInfoService personRoleInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private DataDictionaryService dataDictionaryService;

    @Autowired
    private YearRequestForLeaveInfoService yearRequestForLeaveInfoService;

    @Autowired
    private CompanyInfoService companyInfoService;


    @ApiOperation(value = "请假记录分页")
    @PostMapping(value = "pageList")
    public MessageResult<Map> pageList(
            String token,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("a.create_time", "desc"));

            Map<String, Object> searchParams = new HashMap<>();

            if (StringUtil.isNotEmpty(subject)) {
                searchParams.put("personId", subject);
            }

            searchParams.put("pageIndex", pageIndex);
            searchParams.put("pageSize", pageSize);

            Page<YearRequestForLeaveInfo> page = yearRequestForLeaveInfoService.pageMobileSearch(searchParams, pageIndex, pageSize, true, sortList);

            Page<YearRequestForLeaveInfoDTO> pageDto = new Page<>();


            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
            for (YearRequestForLeaveInfo yearRequestForLeaveInfo : page) {

                YearRequestForLeaveInfoDTO yearRequestForLeaveInfoDTO = new YearRequestForLeaveInfoDTO();

                yearRequestForLeaveInfoDTO.setId(yearRequestForLeaveInfo.getId());

                Date createTime = yearRequestForLeaveInfo.getCreateTime();

                String createTimeStr = "";

                if (createTime != null) {
                    createTimeStr = sdf.format(createTime);
                }

                yearRequestForLeaveInfoDTO.setCreateTime(createTimeStr);

                PersonInfo personInfo = personInfoService.get(yearRequestForLeaveInfo.getPersonId());


                String title = personInfo.getName() + "的年假申请";

                yearRequestForLeaveInfoDTO.setTitle(title);

                yearRequestForLeaveInfoDTO.setStatus(yearRequestForLeaveInfo.getStatus());

                yearRequestForLeaveInfoDTO.setStatusN(TypeNameUtil.getExamineTypeName(yearRequestForLeaveInfo.getStatus().toString()));
                yearRequestForLeaveInfoDTO.setStartTime(DateUtil.format(yearRequestForLeaveInfo.getStartTime(),"yyyy-MM-dd"));
                yearRequestForLeaveInfoDTO.setStartTimeQuantum(yearRequestForLeaveInfo.getStartTimeQuantum());
                yearRequestForLeaveInfoDTO.setEndTime(DateUtil.format(yearRequestForLeaveInfo.getEndTime(),"yyyy-MM-dd"));
                yearRequestForLeaveInfoDTO.setEndTimeQuantum(yearRequestForLeaveInfo.getEndTimeQuantum());
                if (yearRequestForLeaveInfoDTO.getStatus() == 0){
                    yearRequestForLeaveInfoDTO.setMergeStatus("0");
                    yearRequestForLeaveInfoDTO.setMergeStatusN("年假待审核");
                }
                if (yearRequestForLeaveInfoDTO.getStatus() == 3){
                    yearRequestForLeaveInfoDTO.setMergeStatus("3");
                    yearRequestForLeaveInfoDTO.setMergeStatusN("年假已拒绝");
                }

                pageDto.add(yearRequestForLeaveInfoDTO);
            }

            pageDto.setPageSize(page.getPageSize());
            pageDto.setTotal(page.getTotal());
            pageDto.setPageNum(page.getPageNum());

            messageResult.setResult(true);
            messageResult.setData(PojoUtils.pageWrapper(pageDto));

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            messageResult.setResult(false);
        }

        return messageResult;
    }


    @ApiOperation(value = "请年假申请数据提交")
    @RequestMapping(value = "submitYearRequest", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", required = true, paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form"),
            @ApiImplicitParam(name = "startTime", value = "请假开始时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "endTime", value = "请假结束时间", required = true, paramType = "form"),
            @ApiImplicitParam(name = "startTimeQuantum", value = "开始时间段（0：上午，1：下午）", required = true, paramType = "form"),
            @ApiImplicitParam(name = "endTimeQuantum", value = "结束时间段（0：上午，1：下午）", required = true, paramType = "form"),
            @ApiImplicitParam(name = "imageUrl", value = "上传照片", required = true, paramType = "form"),
            @ApiImplicitParam(name = "reason", value = "请假理由", required = true, paramType = "form"),
            @ApiImplicitParam(name = "days", value = "天数", required = true, paramType = "form"),
            @ApiImplicitParam(name = "reason", value = "请假理由", required = true, paramType = "form"),
            @ApiImplicitParam(name = "formId", value = "页面路径", required = true, paramType = "form"),
            @ApiImplicitParam(name = "activityId", value = "步骤id", required = true, paramType = "form"),
            @ApiImplicitParam(name = "draft", value = "状态（0：草稿，1：提交）", required = true, paramType = "form")
    })
    public MessageResult<Map> submitYearRequest(String id,String draft,
                                             String startTime, String endTime,
                                             String startTimeQuantum,String endTimeQuantum,
                                             String imageUrl,String reason,String days,
                                             String formId,String activityId,
                                             String token, @RequestAttribute String subject) {
        MessageResult<Map> msgResult = new MessageResult<>();

        try {
            //查询操作人
            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));
            if (personInfo == null) {
                throw new Exception("申请人不存在");
            }

            //申请人角色
            PersonInfo person = personInfoService.get(Long.parseLong(subject));
            PersonPopedom personPopedom = personPopedomService.get(person.getPopedom());

            String ide = yearRequestForLeaveInfoService.leaveSubmit(personPopedom,startTime,endTime, startTimeQuantum, endTimeQuantum, imageUrl,reason,days,formId,activityId,draft,id);


            Map<String, Object> map = new HashMap<>();
            map.put("id", ide);
            msgResult.setData(map);
            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 = "approvedList")
    public MessageResult<Object> approvedList(String token, @RequestAttribute String subject) {
        MessageResult<Object> messageResult = new MessageResult<>();

        PersonInfo person = personInfoService.get(Long.parseLong(subject));

        List<Map<String, Object>> list = new ArrayList<>();

        if (person != null) {
            //查询企业管理员
            PersonPopedom personPopedom0 = personPopedomService.get(person.getPopedom());
            PersonRoleInfo personRoleInfo = personRoleInfoService.get(personPopedom0.getPersonRoleId());
            if (org.apache.commons.lang3.StringUtils.isNotBlank(personRoleInfo.getParentId())) {


                List<PersonPopedom> personPopedomList = personPopedomService.findByCompanyIdAndRoleIds(personPopedom0.getCompanyId(), personRoleInfo.getParentId());
                for (PersonPopedom personPopedom : personPopedomList) {
                    PersonInfo personInfo = personInfoService.get(personPopedom.getPersonId());

                    if (personInfo != null && !personInfo.getDelFlag()) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", personInfo.getId());
                        map.put("name", personInfo.getName());
                        list.add(map);
                    }
                }
            }

        }

        messageResult.setResult(true);
        messageResult.setData(list);

        return messageResult;
    }


    @ApiOperation(value = "根据id查询请假详情")
    @PostMapping(value = "yearRequestLeaveDetail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "form")
    })
    public MessageResult<Map> yearRequestLeaveDetail(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());



            //主业务
            YearRequestForLeaveInfo yearRequestForLeaveInfo = yearRequestForLeaveInfoService.get(id);

            //申请人
            //   PersonPopedom requestForLeavePopedom = personPopedomService.get(requestForLeaveInfo.getPersonPopedomId());
            PersonInfo yearRequestForLeavePerson = personInfoService.get(yearRequestForLeaveInfo.getPersonId());


            Map<String, Object> map = new HashMap<>();
            map.put("name", yearRequestForLeavePerson.getName());
            map.put("faceImageUrl",yearRequestForLeavePerson.getFaceImageUrl());
            map.put("imageUrl", yearRequestForLeaveInfo.getImageUrl());
            map.put("status", yearRequestForLeaveInfo.getStatus());
            map.put("statusName", TypeNameUtil.getExamineTypeName(yearRequestForLeaveInfo.getStatus().toString()));
            map.put("startTime", DateUtil.format(yearRequestForLeaveInfo.getStartTime(),"yyyy-MM-dd"));
            map.put("startTimeQuantum",yearRequestForLeaveInfo.getStartTimeQuantum());
            map.put("endTime",DateUtil.format(yearRequestForLeaveInfo.getEndTime(),"yyyy-MM-dd"));
            map.put("endTimeQuantum",yearRequestForLeaveInfo.getEndTimeQuantum());
            map.put("days",yearRequestForLeaveInfo.getDays());
            map.put("reason",yearRequestForLeaveInfo.getReason());
            map.put("createTime",DateUtil.format(yearRequestForLeaveInfo.getCreateTime(),"yyyy-MM-dd HH:mm"));


            if (yearRequestForLeaveInfo.getStatus() == 0){
                map.put("mergeStatus","0");
                map.put("mergeStatusN","年假待审核");
            }
            if (yearRequestForLeaveInfo.getStatus() == 3){
                map.put("mergeStatus","3");
                map.put("mergeStatusN","年假已拒绝");
            }




            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 = "detail")
    public MessageResult<Object> detail(String id, String token, @RequestAttribute String subject) {
        MessageResult<Object> messageResult = new MessageResult<>();

        try {

            PersonInfo personInfo0 = personInfoService.get(Long.valueOf(subject));
            PersonPopedom personPopedom0 = personPopedomService.get(personInfo0.getPopedom());


            YearRequestForLeaveInfo yearRequestForLeaveInfo = yearRequestForLeaveInfoService.get(id);

            if (yearRequestForLeaveInfo != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("y年M月d日");

                PersonInfo person = personInfoService.get(yearRequestForLeaveInfo.getPersonId());

                YearRequestForLeaveDetailDTO yearRequestForLeaveDetailDTO = new YearRequestForLeaveDetailDTO();


                yearRequestForLeaveDetailDTO.setName(person.getName());
                yearRequestForLeaveDetailDTO.setPicUrl(person.getFaceImageUrl());

                yearRequestForLeaveDetailDTO.setStatus(yearRequestForLeaveInfo.getStatus());
                yearRequestForLeaveDetailDTO.setStatusN(yearRequestForLeaveInfo.getStatusN());
                yearRequestForLeaveDetailDTO.setIdCard(person.getIdCard());

                String startTime = sdf.format(yearRequestForLeaveInfo.getStartTime());
                String endTime = sdf.format(yearRequestForLeaveInfo.getEndTime());

                yearRequestForLeaveDetailDTO.setStartTime(startTime);
                yearRequestForLeaveDetailDTO.setEndTime(endTime);
                yearRequestForLeaveDetailDTO.setStartTimeQuantum(yearRequestForLeaveInfo.getStartTimeQuantum());
                yearRequestForLeaveDetailDTO.setEndTimeQuantum(yearRequestForLeaveInfo.getEndTimeQuantum());
                yearRequestForLeaveDetailDTO.setReason(yearRequestForLeaveInfo.getReason());
                yearRequestForLeaveDetailDTO.setImageUrl(yearRequestForLeaveInfo.getImageUrl());
                yearRequestForLeaveDetailDTO.setCreateTime(yearRequestForLeaveInfo.getCreateTime());


                //审核人
                PersonPopedom personPopedom = personPopedomService.get(yearRequestForLeaveInfo.getApprovedPersonPopedomId());

                PersonInfo approved = null;
                if (personPopedom == null) {
                    approved = personInfoService.get(yearRequestForLeaveInfo.getApprovedId());
                } else {
                    approved = personInfoService.get(personPopedom.getPersonId());
                }
                // PersonInfo approved = personInfoService.get(personPopedom.getPersonId());

                yearRequestForLeaveDetailDTO.setApprovedName(approved.getName());
                yearRequestForLeaveDetailDTO.setApprovalOpinions(yearRequestForLeaveInfo.getApprovalOpinions());
                yearRequestForLeaveDetailDTO.setApprovalExplain(yearRequestForLeaveInfo.getApprovalExplain());

                if (yearRequestForLeaveDetailDTO.getApprovedTime() != null) {
                    String approvedTime = sdf.format(yearRequestForLeaveInfo.getApprovedTime());
                    yearRequestForLeaveDetailDTO.setApprovedTime(approvedTime);
                }


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

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

                //审批状态
                List<String> statusList = null;
                if (org.apache.commons.lang3.StringUtils.isNotBlank(yearRequestForLeaveInfo.getApprovalStatusStr())) {
                    statusList = Arrays.asList(yearRequestForLeaveInfo.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 (approved != null) {
                    approvalDTO0.setName(approved.getName());
                    approvalDTO0.setFaceImageUrl(approved.getFaceImageUrl());
                }

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


                yearRequestForLeaveDetailDTO.setList(approvalDTOList);
                //   requestForLeaveDetailDto.setRemark(businessOut.getRemark());
                //  requestForLeaveDetailDto.setapp(businessOut.getApprovalPersonPopedomId());
                boolean approvalEnable = false;
                if (personPopedom0.getId().equals(yearRequestForLeaveInfo.getApprovedPersonPopedomId())) {
                    approvalEnable = true;
                }

                yearRequestForLeaveDetailDTO.setApprovalEnable(approvalEnable);


                messageResult.setResult(true);
                messageResult.setData(yearRequestForLeaveDetailDTO);
            } else {
                messageResult.setResult(false);
                messageResult.setMessage("年假记录不存在！");
            }

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

        return messageResult;
    }


    @ApiOperation(value = "年假审批列表")
    @PostMapping(value = "approvedPageList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "是否处理（0：待处理，1：已处理）", dataType = "int", paramType = "query"),
    })
    public MessageResult<Map> approvedPageList(
            @RequestParam(value = "type", defaultValue = "0") int type,
            String token,
            @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestAttribute String subject) {
        MessageResult<Map> messageResult = new MessageResult<>();

        try {

            //  List<Sort> sortList = new ArrayList<>();
            //  sortList.add(new Sort("a.create_time", "desc"));

            //  Map<String, Object> searchParams = new HashMap<>();


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

            Page<YearRequestForLeaveInfo> page = new Page<>();
            if (type == 0){
                Map<String,Object> searchParams = new HashMap<>();
                searchParams.put("type","0");
                searchParams.put("approvalPersonPopedomId",personPopedom.getId());
                List<Sort> sortList = new ArrayList<>();
                sortList.add(new Sort("a.create_time","desc"));
                page = yearRequestForLeaveInfoService.pageMobileSearch(searchParams,pageIndex,pageSize,true,sortList);


            }else {
                //操作人的公司
                CompanyInfo companyInfo = companyInfoService.get(personPopedom.getCompanyId());
                List<CompanyInfo> companyInfoList = companyInfoService.findAllCompanyByCode("%"+companyInfo.getId()+"%");
                Map<String,Object> searchParams = new HashMap<>();
                searchParams.put("type","1");
                searchParams.put("inCompanyInfo",companyInfoList);
                List<Sort> sortList = new ArrayList<>();
                sortList.add(new Sort("a.approved_time","desc"));
                page = yearRequestForLeaveInfoService.pageMobileSearch(searchParams,pageIndex,pageSize,true,sortList);
            }

            /*List<PersonRoleInfo> personRoleInfoList = personRoleInfoService.findParentId(personPopedom.getPersonRoleId());

            List<PersonPopedom> totalPersonPopedomList = new ArrayList<>();


            for (PersonRoleInfo personRoleInfo : personRoleInfoList) {
                List<PersonPopedom> personPopedomList = personPopedomService.findByCompanyIdAndRoleIds(personPopedom.getCompanyId(), personRoleInfo.getId());
                totalPersonPopedomList.addAll(personPopedomList);
            }


            List<Long> approvedIdList = new ArrayList<>();

            approvedIdList.add(person.getId());

            List<PersonInfo> personList = new ArrayList<>();

            for (PersonPopedom item : totalPersonPopedomList) {
                PersonInfo temp = personInfoService.get(item.getPersonId());
                if (temp != null) {
                    approvedIdList.add(item.getPersonId());
                    personList.add(temp);
                }
            }*/

            //    searchParams.put("approvedIdList", approvedIdList);


            //    searchParams.put("status", status);


            //  Page<RequestForLeaveInfo> page = requestForLeaveInfoService.pageMobileSearch(searchParams, pageIndex, pageSize, true, sortList);


            Page<YearRequestForLeaveInfoDTO> pageDto = new Page<>();

            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
            for (YearRequestForLeaveInfo yearRequestForLeaveInfo : page) {

                YearRequestForLeaveInfoDTO yearRequestForLeaveInfoDTO = new YearRequestForLeaveInfoDTO();

                yearRequestForLeaveInfoDTO.setId(yearRequestForLeaveInfo.getId());

                Date createTime = yearRequestForLeaveInfo.getCreateTime();

                String createTimeStr = "";

                if (createTime != null) {
                    createTimeStr = sdf.format(createTime);
                }

                yearRequestForLeaveInfoDTO.setCreateTime(createTimeStr);

                PersonInfo personInfo = personInfoService.get(yearRequestForLeaveInfo.getPersonId());

                if (personInfo != null) {
                    String title = personInfo.getName() + "的年假申请";
                    yearRequestForLeaveInfoDTO.setPicUrl(personInfo.getFaceImageUrl());
                    yearRequestForLeaveInfoDTO.setTitle(title);
                    yearRequestForLeaveInfoDTO.setName(personInfo.getName());
                }

                yearRequestForLeaveInfoDTO.setStatus(yearRequestForLeaveInfo.getStatus());

                yearRequestForLeaveInfoDTO.setStatusN(yearRequestForLeaveInfo.getStatusN());

                pageDto.add(yearRequestForLeaveInfoDTO);
            }

            pageDto.setPageSize(page.getPageSize());
            pageDto.setTotal(page.getTotal());
            pageDto.setPageNum(page.getPageNum());

            messageResult.setResult(true);
            messageResult.setData(PojoUtils.pageWrapper(pageDto));

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

        return messageResult;
    }


    @ApiOperation(value = "年假审批")
    @PostMapping(value = "approved")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "请假记录ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "approvalOpinions", value = "审批意见", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "approvalExplain", value = "意见说明", dataType = "String", paramType = "query"),
    })
    public MessageResult<Object> approved(String id, String approvalOpinions, String approvalExplain, int status, String token, @RequestAttribute String subject) {
        MessageResult<Object> messageResult = new MessageResult<>();

        try {
            //  String[] sz= id.split(",");
            //审核人
            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());


            if (org.apache.commons.lang3.StringUtils.isNotBlank(approvalOpinions)) {
                approvalOpinions = StrUtil.replace(approvalOpinions, ",", "，");
            }
            if (org.apache.commons.lang3.StringUtils.isNotBlank(approvalExplain)) {
                approvalExplain = StrUtil.replace(approvalExplain, ",", "，");
            }

            yearRequestForLeaveInfoService.approvalRequestForLeave(personInfo, id, approvalOpinions, approvalExplain, status);


            messageResult.setResult(true);

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

        return messageResult;
    }

}
