package com.docus.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.OcrVersionConvert;
import com.docus.server.dto.segmentation.ocrversion.AddOcrVersionDTO;
import com.docus.server.dto.segmentation.ocrversion.DeleteOcrVersionDTO;
import com.docus.server.dto.segmentation.ocrversion.EditOcrVersionDTO;
import com.docus.server.entity.segmentation.*;
import com.docus.server.infrastructure.dao.*;
import com.docus.server.service.IOcrRuleService;
import com.docus.server.service.IOcrSpecialRuleService;
import com.docus.server.service.IOcrSpecialRuleTestService;
import com.docus.server.service.IOcrVersionService;
import com.docus.server.vo.segmentation.ocrversion.OcrVersionVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 正式数据版本号管理 服务实现类
 *
 * @author AutoGenerator
 * @since 2023-08-29
 */
@Service
public class OcrVersionServiceImpl implements IOcrVersionService {
    @Resource
    private IOcrVersionDao iOcrVersionDao;
    @Resource
    private IdService idService;
    @Resource
    private IOcrRuleDao iOcrRuleDao;
    @Resource
    private IOcrRuleTestDao iOcrRuleTestDao;
    @Resource
    private IOcrSpecialRuleDao iOcrSpecialRuleDao;
    @Resource
    private IOcrSpecialRuleTestDao iOcrSpecialRuleTestDao;
    @Resource
    private IOcrCutConfigDao iOcrCutConfigDao;
    @Resource
    private IOcrCutConfigTestDao iOcrCutConfigTestDao;
    @Resource
    private IOcrUrlConfigDao iOcrUrlConfigDao;
    @Resource
    private IOcrUrlConfigTestDao iOcrUrlConfigTestDao;

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

    @Override
    public OcrVersion finEnableVersion() {
        return iOcrVersionDao.findBy("isEnable","1").get(0);
    }

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

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

        //PageResult<OcrVersionVO> result = OcrVersionConvert.INSTANCE.convertVO(iOcrVersionDao.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 addOcrVersionDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(AddOcrVersionDTO addOcrVersionDTO) {
        int version = 1;
        OcrVersion maxVersion = iOcrVersionDao.findMaxVersion();
        if (maxVersion!=null){
            version = maxVersion.getVersion()+1;
        }
        final Integer finalVersion = version;

        OcrVersion ocrVersion = OcrVersionConvert.INSTANCE.convertDO(addOcrVersionDTO);
        ocrVersion.setId(idService.getDateSeq());
        ocrVersion.setVersion(version);
        ocrVersion.setIsEnable(true);
        ocrVersion.setCreateTime(new Date());
        ocrVersion.setUpdateTime(new Date());

        List<OcrRule> ocrRuleList = iOcrRuleTestDao.findAll().stream().map(m-> {
            OcrRule ocrRule = new OcrRule();
            BeanUtil.copyProperties(m,ocrRule);
            ocrRule.setVersion(finalVersion);
            ocrRule.setId(idService.getDateSeq());
            return ocrRule;
        }).collect(Collectors.toList());

        List<OcrSpecialRule> ocrSpecialRuleTestDaoAll = iOcrSpecialRuleTestDao.findAll().stream().map(m-> {
            OcrSpecialRule ocrSpecialRule = new OcrSpecialRule();
            BeanUtil.copyProperties(m,ocrSpecialRule);
            ocrSpecialRule.setVersion(finalVersion);
            ocrSpecialRule.setId(idService.getDateSeq());
            return ocrSpecialRule;
        }).collect(Collectors.toList());

        List<OcrCutConfig> ocrCutConfigList = iOcrCutConfigTestDao.findAll().stream().map(m-> {
            OcrCutConfig ocrCutConfig = new OcrCutConfig();
            BeanUtil.copyProperties(m,ocrCutConfig);
            ocrCutConfig.setVersion(finalVersion);
            ocrCutConfig.setId(idService.getDateSeq());
            return ocrCutConfig;
        }).collect(Collectors.toList());

        List<OcrUrlConfig> ocrUrlConfigList = iOcrUrlConfigTestDao.findAll().stream().map(m-> {
            OcrUrlConfig ocrUrlConfig = new OcrUrlConfig();
            BeanUtil.copyProperties(m,ocrUrlConfig);
            ocrUrlConfig.setVersion(finalVersion);
            ocrUrlConfig.setId(idService.getDateSeq());
            return ocrUrlConfig;
        }).collect(Collectors.toList());

        iOcrRuleDao.saveBatch(ocrRuleList,iOcrRuleDao.DEFAULT_BATCH_SIZE);
        iOcrSpecialRuleDao.saveBatch(ocrSpecialRuleTestDaoAll,iOcrSpecialRuleDao.DEFAULT_BATCH_SIZE);
        iOcrCutConfigDao.saveBatch(ocrCutConfigList,iOcrRuleDao.DEFAULT_BATCH_SIZE);
        iOcrUrlConfigDao.saveBatch(ocrUrlConfigList,iOcrSpecialRuleDao.DEFAULT_BATCH_SIZE);

        List<OcrVersion> isEnableList = iOcrVersionDao.findBy("isEnable", "1").stream().peek(p-> p.setIsEnable(false)).collect(Collectors.toList());
        iOcrVersionDao.updateBatchById(isEnableList,1000);
        return iOcrVersionDao.add(ocrVersion);

    }

    /**
     * 编辑
     *
     * @param editOcrVersionDTO 编辑参数
     * @return 成功或失败
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean edit(EditOcrVersionDTO editOcrVersionDTO) {

        //之前启用的全部变成不起用
        List<OcrVersion> isEnableList = iOcrVersionDao.findBy("isEnable", "1")
                .stream().peek(p-> p.setIsEnable(false))
                .collect(Collectors.toList());
        iOcrVersionDao.updateBatchById(isEnableList,1000);

        OcrVersion ocrVersion = iOcrVersionDao.findById(editOcrVersionDTO.getId());
        ocrVersion.setUpdateTime(new Date());
        ocrVersion.setIsEnable(true);
        return iOcrVersionDao.edit(ocrVersion);

    }

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

