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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @description: 非特殊都直接从公共验证中获取
 * @author: Guos
 * @time: 2025/11/28 下午3:24
 */
@Component
public class CommonCheck extends AbstractHandler {

    @Lazy
    @Autowired
    private FsStoreScrmServiceImpl fsStoreScrmService;

    @Autowired
    private FsStoreScrmOcrMapper fsStoreScrmOcrMapper;

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

    /**
     * 校验
     *
     * @param cateName 分类名称
     * @param storeId  店铺id
     * @param medicalDeviceCode 器械编码
     */
    public ContainsResult check(String cateName, Long storeId, String medicalDeviceCode) {
        FsStoreScrm fsStoreScrm = fsStoreScrmService.selectFsStoreByStoreId(storeId);
        //没有搜索到就调用一次识别
        ContainsResult result = new ContainsResult();
        result.setKeywords(cateName);
        if(cateName.contains("非处方") && fsStoreScrm.getBusinessScope().contains(cateName)){
            result.setFlag(true);
            result.setMessage("包含所属！");
            result.setBusinessStr(fsStoreScrm.getBusinessScope());
            return result;
        }
        //通过店铺id获取上传的药品经营许可证。
        String drugLicenseUrl = fsStoreScrm.getDrugLicense();
        FsStoreScrmOcr fsStoreScrmOcr = fsStoreScrmOcrMapper.selectByIdAndUrl(storeId, drugLicenseUrl, null);
        if(ObjectUtil.isEmpty(fsStoreScrmOcr)){
            String ocrResult = TxOcrClient.ExtractDocBasic(drugLicenseUrl);
            if(null != ocrResult){
                boolean equalsR = ocrResult.contains(cateName);
                result.setFlag(equalsR);
                result.setMessage(equalsR?"识别成功，包含所属！":"对比失败，不包含你所选类目");
                result.setBusinessStr(ocrResult);
            }else{
                result.setFlag(false);
                result.setMessage("请上传正确药品经营许可证！");
            }
        }else{
            result.setBusinessStr(fsStoreScrmOcr.getDrugLicenseTxt());
            boolean equalsR = fsStoreScrmOcr.getDrugLicenseTxt().contains(cateName);
            result.setFlag(equalsR);
            result.setMessage(equalsR?"识别成功，包含所属！":"对比失败，不包含你所选类目");
            return result;
        }
        //调用对比，看经营范围是否包含这个类别
        return result;
    }

}
