package com.fs.hisStore.service.categoryVal;

import cn.hutool.core.util.ObjectUtil;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.txocr.ContainsResult;
import com.fs.common.utils.txocr.TxOcrClient;
import com.fs.hisStore.domain.FsStoreScrm;
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;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @description:
 * @author: Guos
 * @time: 2025/11/28 下午2:21
 */
@Slf4j
@Component
public class IIMedicalDeviceCheck extends AbstractHandler{

    @Lazy
    @Autowired
    private FsStoreScrmServiceImpl fsStoreScrmService;

    private static final List<String> medicalDeviceCodes = Arrays.asList("07", "14", "6840", "18", "19", "22");




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

    /**
     * 校验
     * @param cateName
     * @param storeId
     * @param medicalDeviceCode 器械编码
     */
    public ContainsResult check(String cateName, Long storeId, String medicalDeviceCode){
        log.info("2类器械验证");
        FsStoreScrm fsStoreScrm = fsStoreScrmService.selectFsStoreByStoreId(storeId);
        ContainsResult result = new ContainsResult();
        if(ObjectUtil.isEmpty(fsStoreScrm)){
            result.setFlag(false);
            result.setMessage("店铺不存在！");
            return result;
        }
        if(StringUtils.isEmpty(fsStoreScrm.getMedicalDevice2())){
            result.setFlag(false);
            result.setMessage("没有上传2类证书不允许选择该分类！");
            return result;
        }
        if(medicalDeviceCodes.contains(medicalDeviceCode)){
            result.setFlag(true);
            result.setMessage("符合所选商品分类标准");
            return result;
        }
        String medicalDevice2 = fsStoreScrm.getMedicalDevice2();
        String ocrStr = TxOcrClient.ExtractDocBasic(medicalDevice2);
        boolean contains = ocrStr.contains(medicalDeviceCode);
        result.setFlag(contains);
        result.setMessage(contains ? "器械编号符合所选商品分类2类器械标准" : "器械编号不符合所选商品分类2类器械标准");
        return result;
    }
}
