package com.jpsoft.shinestar.mq.listener;

import com.jpsoft.shinestar.modules.base.entity.*;
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 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;
import java.util.Map;

/**
 * @author 墨鱼_mo
 * @date 2021-1-22 16:04
 */
@Slf4j
@Component
@RabbitListener(queues = "insertPersonInfoSuperviseQueue", concurrency = "2")
public class InsertPersonInfoSuperviseListener {

    @Autowired
    private PersonInfoService personInfoService;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private PersonRoleInfoService personRoleInfoService;


    @RabbitHandler
    public void process(ReturnInfo returnInfo) {

        try{
            //查询登记人对应的公司
            log.warn(returnInfo.toString());
            List<CompanyInfo> companyInfoList = companyInfoService.findByRegionIdAndSceneId(returnInfo.getLiveRegionId(),"2");

            log.warn(String.valueOf(companyInfoList.size()));
            CompanyInfo companyInfo = companyInfoList.get(0);

            PersonInfo personInfo = personInfoService.findByPhone(returnInfo.getPhone());
            PersonApplicationInfo personApplicationInfo = new PersonApplicationInfo();
            if (personInfo != null){
                PersonRoleInfo personRoleInfo = personRoleInfoService.findByRoleNameAndSceneId("返乡人员","2");
                List<PersonPopedom> personPopedomList = personPopedomService.findByPersonIdAndRoleId(personInfo.getId(),personRoleInfo.getId());
                if (personPopedomList.size()>0){
                    PersonPopedom personPopedom = personPopedomList.get(0);
                    personInfo.setName(returnInfo.getName());
                    personInfo.setPhone(returnInfo.getPhone());
                    personInfo.setCompanyId(companyInfo.getId());
                    personInfo.setIdCard(returnInfo.getIdCard());
                    personInfoService.update(personInfo);
                    personPopedom.setCompanyId(companyInfo.getId());
                    personPopedomService.update(personPopedom);
                }else {
                    personApplicationInfo.setPersonId(personInfo.getId());
                    personApplicationInfo.setName(returnInfo.getName());
                    personApplicationInfo.setIdCard(returnInfo.getIdCard());
                    personApplicationInfo.setTelephone(returnInfo.getPhone());
                    personApplicationInfo.setPersonRoleId(personRoleInfoService.findByRoleNameAndSceneId("返乡人员","2").getId());
                    personApplicationInfo.setAddress(returnInfo.getLiveDetailAddress());
                    personApplicationInfo.setServiceCompanyId(companyInfo.getRelationCompanyId());
                    personApplicationInfo.setSceneId(companyInfo.getSceneId());

                    Long personId =  personInfoService.insertPersonAndPopedom(personApplicationInfo, null,companyInfo.getId());

                    if (personId != null){
                        PersonInfo personInfo1 = personInfoService.get(personId);
                        personInfo1.setOpenId(returnInfo.getOpenId());
                        personInfoService.update(personInfo1);
                    }
                }



            }else {
                personApplicationInfo.setName(returnInfo.getName());
                personApplicationInfo.setIdCard(returnInfo.getIdCard());
                personApplicationInfo.setTelephone(returnInfo.getPhone());
                personApplicationInfo.setPersonRoleId(personRoleInfoService.findByRoleNameAndSceneId("返乡人员","2").getId());
                personApplicationInfo.setAddress(returnInfo.getLiveDetailAddress());
                personApplicationInfo.setServiceCompanyId(companyInfo.getRelationCompanyId());
                personApplicationInfo.setSceneId(companyInfo.getSceneId());

                Long personId =  personInfoService.insertPersonAndPopedom(personApplicationInfo, null,companyInfo.getId());

                if (personId != null){
                    PersonInfo personInfo1 = personInfoService.get(personId);
                    personInfo1.setOpenId(returnInfo.getOpenId());
                    personInfoService.update(personInfo1);
                }
            }


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

    }
}
