package com.fs.hisStore.service.categoryVal;

import cn.hutool.core.util.ObjectUtil;
import com.fs.common.utils.txocr.ContainsResult;
import com.fs.common.utils.txocr.TxOcrClient;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.domain.FsStoreScrmOcr;
import com.fs.hisStore.mapper.FsStoreScrmOcrMapper;
import com.fs.hisStore.service.impl.FsStoreScrmServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @description: 1类器械验证 1类需要验证的时间是经营许可证是否有“第一类医疗器械销售”
 * @author: Guos
 * @time: 2025/11/28 下午2:21
 */
@Slf4j
@Component
public class IMedicalDeviceCheck extends AbstractHandler{

    @Autowired
    private FsStoreScrmOcrMapper fsStoreScrmOcrMapper;

    @Lazy
    @Autowired
    private FsStoreScrmServiceImpl fsStoreScrmService;

    @Override
    public void afterPropertiesSet() throws Exception {
        ProductCategoryCheckFactory.register("202", this);
    }

    private static final String I_MEDICAL_DEVICE = "第一类医疗器械销售";

    /**
     * 校验
     * @param cateName
     * @param storeId
     * @param medicalDeviceCode 器械编码
     */
    public ContainsResult check(String cateName, Long storeId, String medicalDeviceCode){
        log.info("第一类医疗器械销售检查");
        FsStoreScrmOcr fsStoreScrmOcr = fsStoreScrmOcrMapper.selectById(storeId);
        if (ObjectUtil.isEmpty(fsStoreScrmOcr)) {
            FsStoreScrm fsStoreScrm = fsStoreScrmService.selectFsStoreByStoreId(storeId);
            //没有就直接去查询，然后调用识别
            String businessLicense = fsStoreScrm.getBusinessLicense();
            return TxOcrClient.isContains(businessLicense, I_MEDICAL_DEVICE);
        }else{
            String businessLicenseTxt = fsStoreScrmOcr.getBusinessLicenseTxt();
            boolean containsFlag = businessLicenseTxt.contains(I_MEDICAL_DEVICE);
            ContainsResult result = new ContainsResult();
            result.setKeywords(cateName);
            result.setFlag(containsFlag);
            result.setMessage(containsFlag?"识别成功，包含所属！":"对比失败，不包含你所选类目");
            result.setBusinessStr(businessLicenseTxt);
            return result;
        }
    }

}
