package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
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;

@Slf4j
@Component
@RabbitListener(queues = "updateWorkStatusQueue",concurrency = "10")
public class UpdateWorkStatusListener {
    @Autowired
    private PersonInfoService personInfoService;

    @RabbitHandler
    public void process(String id) {
        try {
            log.warn("收到修改工作状态记录：" + id);
            String[]sz=id.split("_");
            String type=sz[0];//A 修改为请假， b还原
            String personId=sz[1];//id
            String status=sz[2];//之前的状态
            String approvePersonId = "";

            if (sz.length>3){
                approvePersonId = sz[3];
            }

          //RequestForLeaveInfo target = requestForLeaveInfoService.get(id);
            PersonInfo info =personInfoService.get(Long.valueOf(personId));

            if (StringUtils.isNotEmpty(approvePersonId)){
                info.setUpdateBy(type + "_" + approvePersonId);
            }

            Date now = new Date();

            if("A".equals(type)){
                info.setWorkStatus("4");
            }

            if("B".equals(type)){
                //如果是请假， 则修改回来， 否则不管
                if("4".equals(info.getWorkStatus())) {
                    info.setWorkStatus(status);
                }//连续请假可能造成的影响
                if("4".equals(info.getWorkStatus())) {
                    info.setWorkStatus("1");
                }
            }

            info.setUpdateTime(now);
            personInfoService.update(info);
        }
        catch (Exception ex){
            log.error(ex.getMessage(),ex);
        }
    }

}
