package com.zcloud.primeport.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jjb.saas.message.client.message.facede.MessageFacade;
import com.jjb.saas.message.client.message.request.MessageSendCmd;
import com.jjb.saas.message.client.message.request.MessageTargetCmd;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListAddEvent;
import com.zcloud.gbscommon.todolistmq.event.TodoListCompleteEvent;
import com.zcloud.gbscommon.utils.UuidUtil;
import com.zcloud.primeport.domain.gateway.PersonApplyGateway;
import com.zcloud.primeport.domain.model.PersonApplyE;
import com.zcloud.primeport.domain.model.XgfApplyPersonE;
import com.zcloud.primeport.persistence.dataobject.PersonApplyDO;
import com.zcloud.primeport.persistence.dataobject.PersonMessageDO;
import com.zcloud.primeport.persistence.dataobject.XgfApplyPersonDO;
import com.zcloud.primeport.persistence.repository.PersonApplyRepository;
import com.zcloud.primeport.persistence.repository.PersonMessageRepository;
import com.zcloud.primeport.persistence.repository.XgfApplyPersonRepository;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * web-infrastructure
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:22
 */
@Service
@AllArgsConstructor
public class PersonApplyGatewayImpl implements PersonApplyGateway {
    private final PersonApplyRepository personApplyRepository;
    private final XgfApplyPersonRepository xgfApplyPersonRepository;
    private final PersonMessageRepository personMessageRepository;
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    @DubboReference
    private MessageFacade messageFacade;
    @Override
    public Boolean add(PersonApplyE personApplyE) {
        PersonApplyDO d = new PersonApplyDO();
        BeanUtils.copyProperties(personApplyE, d);
        personApplyRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(PersonApplyE personApplyE) {
        PersonApplyDO d = new PersonApplyDO();
        BeanUtils.copyProperties(personApplyE, d);
        personApplyRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedPersonApplyById(Long id) {
        return personApplyRepository.removeById(id);
    }

    @Override
    public Boolean deletedPersonApplyByIds(Long[] ids) {
        return personApplyRepository.removeByIds(Collections.singletonList(ids));
    }

    @Override
    public void saveBatch(ArrayList<PersonApplyE> personApplyES) {
        ArrayList<PersonApplyDO> d = new ArrayList<>();
        personApplyES.forEach(item -> {
            PersonApplyDO doItem = new PersonApplyDO();
            BeanUtils.copyProperties(item, doItem);
            d.add(doItem);
        });
        personApplyRepository.saveBatch(d);
    }

    @Override
    public void xgfPersonAudit(XgfApplyPersonE build) {
        XgfApplyPersonDO d = new XgfApplyPersonDO();
        BeanUtils.copyProperties(build, d);
        xgfApplyPersonRepository.updateById(d);
        UpdateWrapper<PersonApplyDO> objectQueryWrapper = new UpdateWrapper<>();
        objectQueryWrapper.set("mkmj_permission", build.getAuditFlag().equals(2) ? 1 : 2);
        objectQueryWrapper.eq("xgf_apply_person_id", d.getId());
        personApplyRepository.update(objectQueryWrapper);
        if (build.getAuditFlag().equals(2)) {
            // 审核通过后添加人员信息
            XgfApplyPersonDO byId = xgfApplyPersonRepository.getById(d.getId());
            QueryWrapper<PersonApplyDO> personApplyDOQueryWrapper = new QueryWrapper<>();
            personApplyDOQueryWrapper.eq("xgf_apply_person_id", d.getId());
            List<PersonApplyDO> list = personApplyRepository.list(personApplyDOQueryWrapper);
            UpdateWrapper<PersonApplyDO> personApplyDOUpdateWrapper = new UpdateWrapper<>();
            personApplyDOUpdateWrapper.set("mkmj_permission", 1);
            personApplyDOUpdateWrapper.eq("xgf_apply_person_id", d.getId());
            ArrayList<PersonMessageDO> personMessageDOS = new ArrayList<>();
            list.forEach(item -> {
                PersonMessageDO messageDO = new PersonMessageDO();
                messageDO.setPersonBelongType(byId.getPersonBelongType());
                messageDO.setPersonApplyId(d.getId());
                messageDO.setSyncStatus(2);
                messageDO.setGateLevelAuthArea(byId.getGateLevelAuthArea());
                messageDO.setUserFaceUrl(item.getUserFaceUrl());
                messageDO.setUserCard(item.getUserCard());
                messageDO.setVisitStartTime(byId.getVisitStartTime());
                messageDO.setVisitEndTime(byId.getVisitEndTime());
                personMessageDOS.add(messageDO);
                //口门人员审核通过
                MessageSendCmd messageSendCmd = new MessageSendCmd();
                messageSendCmd.setBusinessId(UuidUtil.get32UUID());
                MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
                messageTargetCmd.setUserId(d.getId());
                messageSendCmd.setTargetCmd(messageTargetCmd);
                messageSendCmd.setNeedTokenEnum(false);
                Map<String, Object> sendParams = new HashMap<String, Object>();
                messageSendCmd.setSourceCode("MS000119");
                sendParams.put("HazardDeferralApplicationNumber", 1);
                sendParams.put("ReformHazardNumber", 1);
                messageSendCmd.setParams(sendParams);
                messageFacade.send(messageSendCmd);
            });
            personMessageRepository.saveBatch(personMessageDOS);
        }

        TodoListCompleteEvent todoListCompleteEvent = new TodoListCompleteEvent();
        todoListCompleteEvent.setForeignSubsidiaryKey(build.getId());
        todoListEventPusherUtil.sendMessageCompleteEvent(todoListCompleteEvent);
    }

    @Override
    public List<PersonApplyE> personListByxgfApplyPersonId(Long id) {
        QueryWrapper<PersonApplyDO> xgfApplyPersonDOQueryWrapper = new QueryWrapper<>();
        xgfApplyPersonDOQueryWrapper.eq("xgf_apply_person_id", id);
        List<PersonApplyDO> xgfApplyPersonDOS = personApplyRepository.list(xgfApplyPersonDOQueryWrapper);
        ArrayList<PersonApplyE> xgfApplyPersonES = new ArrayList<>();
        xgfApplyPersonDOS.forEach(item -> {
            PersonApplyE xgfApplyPersonE = new PersonApplyE();
            BeanUtils.copyProperties(item, xgfApplyPersonE);
            xgfApplyPersonES.add(xgfApplyPersonE);
        });
        return xgfApplyPersonES;
    }
}

