package com.docus.server.service.impl;

import com.docus.core.util.Func;
import com.docus.core.util.jackson.JsonUtil;
import com.docus.infrastructure.redis.service.IdService;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.convert.OcrSpecialRuleTestConvert;
import com.docus.server.dto.segmentation.ocrspecialruletest.AddOcrSpecialRuleTestDTO;
import com.docus.server.dto.segmentation.ocrspecialruletest.DeleteOcrSpecialRuleTestDTO;
import com.docus.server.dto.segmentation.ocrspecialruletest.EditOcrSpecialRuleTestDTO;
import com.docus.server.entity.segmentation.OcrSpecialResult;
import com.docus.server.entity.segmentation.OcrSpecialRule;
import com.docus.server.entity.segmentation.OcrSpecialRuleJson;
import com.docus.server.entity.segmentation.OcrSpecialRuleTest;
import com.docus.server.infrastructure.dao.IOcrSpecialRuleTestDao;
import com.docus.server.service.IOcrSpecialRuleTestService;
import com.docus.server.vo.segmentation.ocrspecialruletest.OcrSpecialRuleTestVO;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 特殊策略表-调试 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-08-29
 */
@Service
public class OcrSpecialRuleTestServiceImpl implements IOcrSpecialRuleTestService {
    @Resource
    private IOcrSpecialRuleTestDao iOcrSpecialRuleTestDao;
    @Resource
    private IdService idService;


    @Override
    public OcrSpecialResult handleSpecialRule(List<String> ocrTextList) {

        // 查询所以的特殊规则列表
        List<OcrSpecialRuleTest> all = iOcrSpecialRuleTestDao.findAll();
        //判断是否符合某个条件的开始条件
        for (OcrSpecialRuleTest ocrSpecialRuleTest : all) {

            String assortId = ocrSpecialRuleTest.getAssortId();
            String startJson = ocrSpecialRuleTest.getStartJson();
            String assortName = ocrSpecialRuleTest.getAssortName();

            List<OcrSpecialRuleJson> ocrSpecialRuleJsons = Func.parseJsonArray(startJson, OcrSpecialRuleJson.class);
            for (OcrSpecialRuleJson ocrSpecialRuleJson : ocrSpecialRuleJsons) {
                if (Func.isEmpty(ocrSpecialRuleJson.getKeyWord())){
                    continue;
                }
                double score = ocrSpecialRuleJson.getScore();
                String[] keyWordList = ocrSpecialRuleJson.getKeyWord().split("#");
                for (String ocrText : ocrTextList) {
                    for (int i = 0; i < keyWordList.length; i++) {
                        String keyWord = keyWordList[i];
                        if (ocrText.contains(keyWord)){
                            double rate = keyWord.length() / (double)ocrText.length() * 100;
                            //判断是否命中
                            if (rate>=score) {
                                OcrSpecialResult ocrSpecialResult = new OcrSpecialResult();
                                ocrSpecialResult.setAssortId(assortId);
                                ocrSpecialResult.setAssortName(assortName);
                                ocrSpecialResult.setRate(rate);
                                ocrSpecialResult.setHitKey(keyWord);
                               return ocrSpecialResult;
                            }
                        }
                    }
                }
            }
        }
        //默认返回空对象
        return null;
    }

    @Override
    public boolean handleSpecialRuleEnd(OcrSpecialResult result, List<String> ocrTextList) {
        //获取结束条件列表
        List<OcrSpecialRuleJson> ocrSpecialRuleJsons = Func.parseJsonArray(result.getEndJson(), OcrSpecialRuleJson.class);
        //判断得分占比
        for (OcrSpecialRuleJson ocrSpecialRuleJson : ocrSpecialRuleJsons) {
            double score = ocrSpecialRuleJson.getScore();
            String[] keyWordList = ocrSpecialRuleJson.getKeyWord().split("#");
            for (String ocrText : ocrTextList) {
                for (int i = 0; i < keyWordList.length; i++) {
                    String keyWord = keyWordList[i];
                    if (ocrText.contains(keyWord)){
                        double rate = keyWord.length() / (double)ocrText.length() * 100;
                        //判断是否命中
                        if (rate>=score) {
                            //返回结束
                            return true;
                        }
                    }
                }
            }
        }
        //默认返回没结束
        return false;
    }

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @Override
    public OcrSpecialRuleTestVO findById(Long id) {
        return OcrSpecialRuleTestConvert.INSTANCE.convertVO(iOcrSpecialRuleTestDao.findById(id));
    }

    /**
     * 查询所有
     *
     * @return 实体
     */
    @Override
    public List<OcrSpecialRuleTestVO> findAll() {
        return OcrSpecialRuleTestConvert.INSTANCE.convertVO(iOcrSpecialRuleTestDao.findAll());
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<OcrSpecialRuleTestVO> search(SearchDTO searchDTO) {
        return OcrSpecialRuleTestConvert.INSTANCE.convertVO(iOcrSpecialRuleTestDao.search(searchDTO));

        //PageResult<OcrSpecialRuleTestVO> result = OcrSpecialRuleTestConvert.INSTANCE.convertVO(iOcrSpecialRuleTestDao.search(searchDTO));

        //if (CollectionUtils.isEmpty(result.getList())) {
        //return new PageResult<>();
        //}

        //Map<String, SchSystemParams> map = iSchSystemParamsService.find(ListUtils.distinctSelect(result.getList(), SchCollectorVO::getCollectorId));

        //result.getList().forEach(p -> {
        // String collectorId = String.valueOf(p.getCollectorId());
        // if (map.containsKey(collectorId)) {
        //     p.setCollectorName(map.get(collectorId).getParamName());
        // }
        //});

        //return result;
    }

    /**
     * 新增
     *
     * @param addOcrSpecialRuleTestDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddOcrSpecialRuleTestDTO addOcrSpecialRuleTestDTO) {
        OcrSpecialRuleTest ocrSpecialRuleTest = OcrSpecialRuleTestConvert.INSTANCE.convertDO(addOcrSpecialRuleTestDTO);
        ocrSpecialRuleTest.setId(idService.getDateSeq());
        return iOcrSpecialRuleTestDao.add(ocrSpecialRuleTest);

        // String name = addOcrSpecialRuleTestDTO.getName();

        //OcrSpecialRuleTest ocrSpecialRuleTest = iOcrSpecialRuleTestDao.findOneBy("name", name);

        //if (Func.notNull(ocrSpecialRuleTest)) {
        // throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "特殊策略表-调试已经存在");
        // }

        // OcrSpecialRuleTest ocrSpecialRuleTest =OcrSpecialRuleTestConvert.INSTANCE.convertDO(addOcrSpecialRuleTestDTO);
        // ocrSpecialRuleTest.setId(idService.getDateSeq());

        // return iOcrSpecialRuleTestDao.add(schCollector);

    }

    /**
     * 编辑
     *
     * @param editOcrSpecialRuleTestDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(EditOcrSpecialRuleTestDTO editOcrSpecialRuleTestDTO) {
        OcrSpecialRuleTest ocrSpecialRuleTest = OcrSpecialRuleTestConvert.INSTANCE.convertDO(editOcrSpecialRuleTestDTO);
        return iOcrSpecialRuleTestDao.edit(ocrSpecialRuleTest);

        // Long id = editOcrSpecialRuleTestDTO.getId();

        //  OcrSpecialRuleTest ocrSpecialRuleTest = iOcrSpecialRuleTestDao.findById(id);

        // if (Func.isNull(ocrSpecialRuleTest)) {
        //     throw new ApiException(ExceptionCode.ParamIllegal.getCode(), "特殊策略表-调试，无法更新！");
        // }

        // ocrSpecialRuleTest.setUpdateTime(LocalDateTime.now());
        // return iOcrSpecialRuleTestDao.edit(ocrSpecialRuleTest);
    }

    /**
     * 批量删除
     *
     * @param deleteOcrSpecialRuleTestDTO 删除参数
     * @return 成功或失败
     */
    @Override
    public int delete(DeleteOcrSpecialRuleTestDTO deleteOcrSpecialRuleTestDTO) {
        return iOcrSpecialRuleTestDao.delete(deleteOcrSpecialRuleTestDTO.getIds());
    }
}

