package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.modules.base.entity.BusinessOut;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonPopedom;
import com.jpsoft.shinestar.modules.base.service.BusinessOutService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.pacs.dto.PacsResult;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.List;

/**
 * @author 墨鱼_mo
 * @date 2020-11-21 14:06
 */
@Slf4j
@Component
@RabbitListener(queues = "businessOutQueue",concurrency = "10")
public class BusinessOutListener {

    @Autowired
    private BusinessOutService businessOutService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private PersonInfoService personInfoService;

    @RabbitHandler
    public void process(String id) {
        log.warn("收到外出或出差编号:" + id);

        try {
            BusinessOut businessOut = businessOutService.get(id);

            if (businessOut != null && "1".equals(businessOut.getStatus())){
                PersonPopedom personPopedom = personPopedomService.get(businessOut.getPersonPopedomId());
                PersonInfo personInfo = personInfoService.get(personPopedom.getPersonId());

                Date now = new Date();

                //当前时间是出差期间
                if (businessOut.getStartTime().compareTo(now) <=0 && businessOut.getEndTime().compareTo(now) >0){
                    personInfo.setWorkStatus("3");
                    personInfo.setUpdateTime(new Date());
                    personInfoService.update(personInfo);
                }

                //当前时间已结束出差
                if (businessOut.getEndTime().compareTo(now) <= 0){
                    personInfo.setWorkStatus("1");
                    personInfo.setUpdateTime(new Date());
                    personInfoService.update(personInfo);
                }
            }

            log.warn("员工延时更新工作状态完毕");
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }
    }
}
