package com.fs.hisStore.service.listener;

import com.fs.common.utils.txocr.TxOcrClient;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.domain.FsStoreScrmOcr;
import com.fs.hisStore.mapper.FsStoreScrmMapper;
import com.fs.hisStore.mapper.FsStoreScrmOcrMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @description:
 * @author: Guos
 * @time: 2025/11/21 上午9:44
 */
@Slf4j
@Service
public class FsStoreScrmListener {

    @Resource
    private FsStoreScrmOcrMapper fsStoreScrmOcrMapper;

    @Autowired
    private FsStoreScrmMapper fsStoreMapper;


    @EventListener
    public void StoreScrmOcrListener(FsStoreScrm fsStore){
        log.info("StoreScrmOcrListener start");
        //先查ocr的表中是否存在，
        // 1.店铺存在，url不一致 调用识别并更新
        // 2.店铺存在，url一致，不重新调用识别
        // 3.店铺不存在，说明是新增，新增直接识别别插入
        FsStoreScrmOcr fsStoreScrmOcr = new FsStoreScrmOcr();
        Boolean oldBusinessLicenseFlag = false;
        Boolean oldDrugLicenseFlag = false;
        FsStoreScrmOcr OldFsStoreScrmOcr = null;
        if(null == fsStore.getStoreId()){
            log.info("新增店铺id为空！");
            //如果是新增id是自增的，这时候可以先用StoreSeq，或者account以及storeName这种具有唯一性的字段进行反查StoreId
            String storeSeq = fsStore.getStoreSeq();//这个是必须会生成的，所以我们这直接去查询
            FsStoreScrm fsStoreScrm = fsStoreMapper.selectFsStoreByStoreSeq(storeSeq);
            fsStoreScrmOcr.setStoreId(fsStoreScrm.getStoreId());
        }else{
            fsStoreScrmOcr.setStoreId(fsStore.getStoreId());
            OldFsStoreScrmOcr = fsStoreScrmOcrMapper.selectById(fsStore.getStoreId());
            if(!ObjectUtils.isEmpty(OldFsStoreScrmOcr)){
                fsStoreScrmOcr.setStoreId(OldFsStoreScrmOcr.getStoreId());
                String OldBusinessLicense = OldFsStoreScrmOcr.getBusinessLicense();
                String OldDrugLicense = OldFsStoreScrmOcr.getDrugLicense();
                oldDrugLicenseFlag = OldDrugLicense.equals(fsStore.getDrugLicense());
                oldBusinessLicenseFlag = OldBusinessLicense.equals(fsStore.getBusinessLicense());
            }
        }
        //不一致才会去更新
        if(!oldBusinessLicenseFlag){
            fsStoreScrmOcr.setBusinessLicense(fsStore.getBusinessLicense());
            String result = TxOcrClient.bizLicenseOCR(fsStoreScrmOcr.getBusinessLicense());
            if(!ObjectUtils.isEmpty(result)){
                fsStoreScrmOcr.setBusinessLicenseTxt(result);
            }
        }
        if(!oldDrugLicenseFlag){
            fsStoreScrmOcr.setDrugLicense(fsStore.getDrugLicense());
            String result = TxOcrClient.ExtractDocBasic(fsStore.getDrugLicense());
            if(!ObjectUtils.isEmpty(result)){
                fsStoreScrmOcr.setDrugLicenseTxt(result);
            }
        }
        fsStoreScrmOcr.setUpdateTime(LocalDateTime.now());
        if(ObjectUtils.isEmpty(OldFsStoreScrmOcr)){
            fsStoreScrmOcr.setCreateTime(LocalDateTime.now());
            fsStoreScrmOcrMapper.insert(fsStoreScrmOcr);
        }else{
            fsStoreScrmOcrMapper.updateById(fsStoreScrmOcr);
        }
        log.info("StoreScrmOcrListener end");
    }

}
