package com.jpsoft.shinestar.mq.listener;

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonPopedom;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.business.entity.RequestForLeaveInfo;
import com.jpsoft.shinestar.modules.business.service.RequestForLeaveInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 墨鱼_mo
 * @date 2021-5-22 11:28
 */
@Slf4j
@Component
@RabbitListener(queues = "requestForLeaveQueue",concurrency = "2")
public class RequestForLeaveListener {

    @Autowired
    private RequestForLeaveInfoService requestForLeaveInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private PersonInfoService personInfoService;

    @RabbitHandler
    public void process(String id) {
        try {
            RequestForLeaveInfo requestForLeaveInfo = requestForLeaveInfoService.get(id);
            PersonPopedom personPopedom = personPopedomService.get(requestForLeaveInfo.getPersonPopedomId());
            PersonInfo personInfo = personInfoService.get(personPopedom.getPersonId());
            if (requestForLeaveInfo != null) {
                /*String startTimeStr = DateUtil.format(requestForLeaveInfo.getStartTime(), "yyyy-MM-dd");
                String endTimeStr = DateUtil.format(requestForLeaveInfo.getEndTime(), "yyyy-MM-dd");
                if ("0".equals(requestForLeaveInfo.getStartTimeQuantum())) {
                    startTimeStr = startTimeStr + " " + "06:00:00";
                }
                if ("1".equals(requestForLeaveInfo.getStartTimeQuantum())) {
                    startTimeStr = startTimeStr + " " + "12:00:00";
                }
                if ("0".equals(requestForLeaveInfo.getEndTimeQuantum())) {
                    endTimeStr = endTimeStr + " " + "12:00:00";
                }
                if ("1".equals(requestForLeaveInfo.getEndTimeQuantum())) {
                    endTimeStr = endTimeStr + " " + "18:00:00";
                }*/

               /* Date start = DateUtil.parse(startTimeStr);
                Date end = DateUtil.parse(endTimeStr);*/

               Date start = requestForLeaveInfo.getStartTime();

               Date end = requestForLeaveInfo.getEndTime();

               String approvalPersonIdStr = requestForLeaveInfo.getApprovalPersonIdStr();

                if(StringUtils.isNotEmpty(approvalPersonIdStr)){
                    String[] approvalPersonIds = approvalPersonIdStr.split(",");

                    if (approvalPersonIds.length>0){
                        personInfo.setUpdateBy(approvalPersonIds[approvalPersonIds.length-1]);
                    }
                }

                if (start.compareTo(new Date()) <= 0 && end.compareTo(new Date()) > 0) {
                    personInfo.setWorkStatus("4");
                    personInfo.setUpdateTime(new Date());
                    personInfoService.update(personInfo);
                }
                if (end.compareTo(new Date()) <= 0) {
                    personInfo.setWorkStatus("1");
                    personInfo.setUpdateTime(new Date());
                    personInfoService.update(personInfo);
                }

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