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

import com.alibaba.fastjson.JSONObject;
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.entity.JgUseRegistration;
import com.yeejoin.amos.boot.module.jg.api.mapper.CommonMapper;
import com.yeejoin.amos.boot.module.jg.biz.edit.process.biz.strategy.IBizDataChangeHandleStrategy;
import com.yeejoin.amos.boot.module.jg.biz.edit.process.equip.CommonEquipDataProcessService;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.JgUseRegistrationEqServiceImpl;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.JgUseRegistrationServiceImpl;
import com.yeejoin.amos.boot.module.ymt.api.entity.IdxBizJgUseInfo;
import com.yeejoin.amos.boot.module.ymt.api.enums.FlowStatusEnum;
import com.yeejoin.amos.boot.module.ymt.api.mapper.IdxBizJgRegisterInfoMapper;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
@Getter
@Slf4j
public class UseRegisterUpdateService {

    private final JgUseRegistrationServiceImpl jgUseRegistrationService;

    private final CommonEquipDataProcessService commonEquipDataProcessService;

    private final JgUseRegistrationEqServiceImpl useRegistrationEqService;

    public void updateBizInfo(String bizId, String applyNo, IBizDataChangeHandleStrategy.ModelType model) {
        if(model.equals(IBizDataChangeHandleStrategy.ModelType.singleEquip)){
            JgUseRegistration useRegistration = jgUseRegistrationService.getOne(new LambdaQueryWrapper<JgUseRegistration>().select(BaseEntity::getSequenceNbr, JgUseRegistration::getStatus).eq(JgUseRegistration::getApplyNo, applyNo));
            // 非管道类设备且非已完成状态时，在编辑后进行内部设备代码的更新
            if(!useRegistration.getStatus().equals(FlowStatusEnum.TO_BE_FINISHED.getName())){
                JSONObject jsonObject = jgUseRegistrationService.getHisData(useRegistration);
                IdxBizJgUseInfo bizJgUseInfo = commonEquipDataProcessService.getIdxBizJgUseInfoService().getOneData(bizId);
                String useInnerCode = bizJgUseInfo.getUseInnerCode();
                jsonObject.put("useInnerCode", useInnerCode);
                jgUseRegistrationService.updateHistory(jsonObject, bizId, useRegistration.getSequenceNbr() + "", null );
            }
        }
    }

}
