package com.yeejoin.amos.boot.module.jg.biz.edit.process.biz.changeRegistrationReform;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yeejoin.amos.boot.biz.common.entity.BaseEntity;
import com.yeejoin.amos.boot.module.jg.api.dto.FieldChangeMeta;
import com.yeejoin.amos.boot.module.jg.api.entity.JgChangeRegistrationReform;
import com.yeejoin.amos.boot.module.jg.api.enums.BusinessTypeEnum;
import com.yeejoin.amos.boot.module.jg.biz.edit.core.RouterEventPublisher;
import com.yeejoin.amos.boot.module.jg.biz.edit.event.BaseBizDataChangeEvent;
import com.yeejoin.amos.boot.module.jg.biz.edit.process.biz.DefaultBizDataChangeHandler;
import com.yeejoin.amos.boot.module.ymt.api.enums.FlowStatusEnum;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 改造变更登记编辑处理器
 */
@Component
public class ChangeRegistrationReformDataChangeHandler extends DefaultBizDataChangeHandler<BaseBizDataChangeEvent> {

    private final ChangeRegistrationReformEditUpdateService changeRegistrationReformEditUpdateService;

    protected ChangeRegistrationReformDataChangeHandler(RouterEventPublisher routerEventPublisher, ApplicationContext applicationContext, ChangeRegistrationReformEditUpdateService changeRegistrationReformEditUpdateService) {
        super(routerEventPublisher, applicationContext);
        this.changeRegistrationReformEditUpdateService = changeRegistrationReformEditUpdateService;
    }

    @Override
    public String canHandleBizType() {
        return BusinessTypeEnum.JG_RENOVATION_REGISTRATION.name();
    }

    @Override
    public List<FieldChangeMeta> updBizData(String bizId, String applyNo, ModelType model, Map<String, Object> changeData) {
        return Collections.emptyList();
    }

    @Override
    public Boolean beforeCheck(String bizId, String applyNo, ModelType model, Map<String, Object> changeData) {
        return Boolean.TRUE;
    }

    @Override
    public Set<String> getEqs(String applyNo) {
        return changeRegistrationReformEditUpdateService.getEqsByApplyNo(applyNo);
    }


    @Override
    public Boolean bizIsFinished(String applyNo) {
        JgChangeRegistrationReform jgChangeRegistrationReform = changeRegistrationReformEditUpdateService.getMainService().getOne(new LambdaQueryWrapper<JgChangeRegistrationReform>().eq(JgChangeRegistrationReform::getApplyNo, applyNo).select(BaseEntity::getSequenceNbr, JgChangeRegistrationReform::getAuditStatus));
        return jgChangeRegistrationReform.getAuditStatus().equals(FlowStatusEnum.TO_BE_FINISHED.getName());
    }

    @Override
    public Set<String> getProjectContraptionIds(String applyNo) {
        Set<String> projectContraptionIds = new HashSet<>();
        JgChangeRegistrationReform registrationReform = changeRegistrationReformEditUpdateService.getOneByApplyNo(applyNo);
        projectContraptionIds.add(registrationReform.getProjectContraptionId());
        projectContraptionIds.remove(null);
        projectContraptionIds.remove("");
        return projectContraptionIds;
    }
}
