package cn.iocoder.yudao.module.product.service.inspection;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.enums.BlockStateEnum;
import cn.iocoder.yudao.framework.common.enums.CodeEnum;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.basic.api.businessinfo.BusinessInfoApi;
import cn.iocoder.yudao.module.basic.api.goodsinfo.GoodsInfoApi;
import cn.iocoder.yudao.module.basic.api.goodsinfo.dto.GoodsInfoRespDTO;
import cn.iocoder.yudao.module.basic.api.redis.dto.BusinessInfoDTO;
import cn.iocoder.yudao.module.chain.api.blockcertificate.BlockCertificateApi;
import cn.iocoder.yudao.module.chain.api.blockcertificate.dto.BlockCertificateCreateReqDTO;
import cn.iocoder.yudao.module.chain.api.blockcertificate.dto.BlockCertificateRespDTO;
import cn.iocoder.yudao.module.chain.api.blockcertificate.dto.UpdateStatusReqDTO;
import cn.iocoder.yudao.module.chain.api.operatedetail.dto.OperateDetailSaveReqDTO;
import cn.iocoder.yudao.module.chain.enums.*;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.*;
import cn.iocoder.yudao.module.product.convert.inspection.InspectionConvert;
import cn.iocoder.yudao.module.product.convert.inspectionitem.InspectionItemConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.inspection.InspectionDO;
import cn.iocoder.yudao.module.product.dal.dataobject.inspectionitem.InspectionItemDO;
import cn.iocoder.yudao.module.product.dal.mysql.inspection.InspectionMapper;
import cn.iocoder.yudao.module.product.dal.mysql.inspectionitem.InspectionItemMapper;
import cn.iocoder.yudao.module.product.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.product.enums.InspectionResultEnums;
import cn.iocoder.yudao.module.product.service.harvestbatch.HarvestBatchService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 检验检疫 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InspectionServiceImpl implements InspectionService {

    @Resource
    private InspectionMapper inspectionMapper;

    @Resource
    private InspectionItemMapper inspectionItemMapper;

    @Resource
    private BusinessInfoApi businessInfoApi;

    @Resource
    private GoodsInfoApi goodsInfoApi;

    @Resource
    private BlockCertificateApi blockCertificateApi;

    @Resource
    private HarvestBatchService harvestBatchService;

    @Override
    @Transactional
    public Long createInspection(InspectionCreateReqVO createReqVO) {
        BusinessInfoDTO businessInfo = new BusinessInfoDTO();
        GoodsInfoRespDTO goodsInfo = new GoodsInfoRespDTO();
        businessInfo.setId(createReqVO.getBelongBusinessId());
        goodsInfo.setGoodsId(createReqVO.getGoodsId());
        this.verifyParams(businessInfo, goodsInfo, createReqVO.getHarvestBatchId());

        InspectionDO inspection = InspectionConvert.INSTANCE.convert(createReqVO);
        inspection.setGoodsName(goodsInfo.getGoodsName());
        inspection.setBelongBusinessName(businessInfo.getBusinessName());
        inspection.setInspectionId(CodeEnum.INSPECTION_CODE.getCode());
        inspection.setBlockState(BlockStateEnum.NOT_ON_CHAIN.getBlockState());

        List<InspectionItemDO> inspectionItems = new ArrayList<>();
        createReqVO.getInspectionItems().forEach(item -> {
            InspectionItemDO inspectionItem = new InspectionItemDO();
            BeanUtil.copyProperties(item, inspectionItem);
            inspectionItem.setId(null);
            inspectionItem.setInspectionId(inspection.getInspectionId());
            inspectionItem.setInspectionItemId(CodeEnum.INSPECTION_ITEM_CODE.getCode());
            inspectionItem.setBelongBusinessId(businessInfo.getId());
            inspectionItem.setBelongBusinessName(businessInfo.getBusinessName());

            inspectionItems.add(inspectionItem);
        });

        // 插入
        inspectionMapper.insert(inspection);

        inspectionItemMapper.insertBatch(inspectionItems, Integer.MAX_VALUE);
        // 返回
        return inspection.getId();
    }

    private void verifyParams(BusinessInfoDTO businessInfo, GoodsInfoRespDTO goodsInfo, String batchId) {
        // 校验商户
        BusinessInfoDTO businessInfoDTO = businessInfoApi.getBusinessInfoById(businessInfo.getId()).getData();
        if(businessInfoDTO == null) {
            throw ServiceExceptionUtil.exception(cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants.BUSINESS_INFO_NOT_EXISTS);
        }
        BeanUtil.copyProperties(businessInfoDTO, businessInfo);

        List<String> goodsIds = new ArrayList<>();
        goodsIds.add(goodsInfo.getGoodsId());
        // 校验商品
        List<GoodsInfoRespDTO> goodsInfos = goodsInfoApi.getGoodsInfoListByGoodsIds(goodsIds, businessInfo.getId()).getData();
        if(goodsInfos == null || goodsInfos.isEmpty()) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.GOODS_INFO_NOT_EXISTS);
        }
        BeanUtil.copyProperties(goodsInfos.get(0), goodsInfo);
        // 校验入库批次 暂时不去查询上链状态
        Map<String, List<String>> batchIdMap = harvestBatchService.getBatchIdsByGoodsId(goodsIds, businessInfo.getId(), null);
        if(batchIdMap.get(goodsIds.get(0)) == null || !batchIdMap.get(goodsIds.get(0)).contains(batchId)) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.HARVEST_BATCH_NOT_EXISTS);
        }
    }

    @Override
    @Transactional
    public void updateInspection(InspectionUpdateReqVO updateReqVO) {
        // 校验存在
        InspectionDO inspectionDO = validateInspectionExists(updateReqVO.getId());
        // 判断是否是未上链
//        if(!inspectionDO.getBlockState().equals(BlockStateEnum.NOT_ON_CHAIN.getBlockState())) {
//            // 此时无法编辑
//            throw ServiceExceptionUtil.exception(ErrorCodeConstants.CAN_ONLY_BE_EDITED_WHEN_NOT_ON_CHAIN);
//        }

        // 查询数据库里已有的item
        List<InspectionItemDO> inspectionItemsDB = inspectionItemMapper.selectList(new LambdaQueryWrapperX<InspectionItemDO>().eq(InspectionItemDO::getInspectionId, inspectionDO.getInspectionId()));
        List<Long> willUpdateItemIds = new ArrayList<>();
        List<Long> itemIdsDB = new ArrayList<>();
        Map<Long, InspectionItemDO> itemDBmAP = new HashMap<>();
        updateReqVO.getInspectionItems().forEach(item -> {
            if(item.getId() != null) {
                willUpdateItemIds.add(item.getId());
            }
        });
        inspectionItemsDB.forEach(item -> {
            itemIdsDB.add(item.getId());
            itemDBmAP.put(item.getId(), item);
        });

        // 需要删除的项目id集合
        List<Long> deleteItemIds = new ArrayList<>();
        if(willUpdateItemIds.isEmpty()) {
            deleteItemIds.addAll(itemIdsDB);
        } else {
            deleteItemIds.addAll(itemIdsDB.stream().filter(id -> !willUpdateItemIds.contains(id)).collect(Collectors.toList()));
        }

        InspectionDO updateInspection = new InspectionDO();
        BeanUtil.copyProperties(updateReqVO, updateInspection);

        List<InspectionItemDO> createInspectionItems = new ArrayList<>();
        List<InspectionItemDO> updateInspectionItems = new ArrayList<>();
        updateReqVO.getInspectionItems().forEach(item -> {
            InspectionItemDO inspectionItem = new InspectionItemDO();
            BeanUtil.copyProperties(item, inspectionItem);
            if(item.getId() != null) {
                if(itemDBmAP.get(item.getId()) == null) {
                    throw ServiceExceptionUtil.exception(ErrorCodeConstants.INSPECTION_ITEM_NOT_EXISTS);
                }

                updateInspectionItems.add(inspectionItem);
            } else {
                inspectionItem.setInspectionId(inspectionDO.getInspectionId());
                inspectionItem.setInspectionItemId(CodeEnum.INSPECTION_ITEM_CODE.getCode());
                inspectionItem.setBelongBusinessId(inspectionDO.getBelongBusinessId());
                inspectionItem.setBelongBusinessName(inspectionDO.getBelongBusinessName());
                createInspectionItems.add(inspectionItem);
            }
        });

        inspectionMapper.updateById(updateInspection);

        if(!createInspectionItems.isEmpty()) {
            inspectionItemMapper.insertBatch(createInspectionItems);
        }

        if(!updateInspectionItems.isEmpty()) {
            inspectionItemMapper.updateBatch(updateInspectionItems, Integer.MAX_VALUE);
        }

        if(!deleteItemIds.isEmpty()) {
            inspectionItemMapper.deleteBatchIds(deleteItemIds);
        }
    }

    @Override
    public void deleteInspection(Long id) {
        // 校验存在
        InspectionDO inspectionDO = validateInspectionExists(id);
        if(!inspectionDO.getBlockState().equals(BlockStateEnum.NOT_ON_CHAIN.getBlockState())) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.CAN_ONLY_BE_DELETE_WHEN_NOT_ON_CHAIN);
        }
        // 删除
        inspectionMapper.deleteById(id);
    }

    private InspectionDO validateInspectionExists(Long id) {
        InspectionDO inspectionDO = inspectionMapper.selectById(id);
        if (inspectionDO == null) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.INSPECTION_NOT_EXISTS);
        }
        return inspectionDO;
    }

    @Override
    public InspectionRespVO getInspection(Long id) {
        InspectionRespVO inspectionResp = new InspectionRespVO();
        InspectionDO inspection = inspectionMapper.selectById(id);
        BeanUtil.copyProperties(inspection, inspectionResp);

        List<InspectionItemDO> inspectionItems = inspectionItemMapper.selectList(new LambdaQueryWrapperX<InspectionItemDO>()
                .eq(InspectionItemDO::getInspectionId, inspection.getInspectionId()));
        inspectionResp.setInspectionItems(InspectionItemConvert.INSTANCE.convertList(inspectionItems));
        return inspectionResp;
    }

    @Override
    public List<InspectionDO> getInspectionList(Collection<Long> ids) {
        return inspectionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectionDO> getInspectionPage(InspectionPageReqVO pageReqVO) {
        return inspectionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InspectionDO> getInspectionList(InspectionExportReqVO exportReqVO) {
        return inspectionMapper.selectList(exportReqVO);
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void upChain(Long id) {
        // 校验存在
        InspectionDO inspectionDO = validateInspectionExists(id);
        // 判断是否是未上链
        if(!inspectionDO.getBlockState().equals(BlockStateEnum.NOT_ON_CHAIN.getBlockState())) {
            // 此时无法编辑
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.CAN_ONLY_BE_ON_CHAIN_WHEN_NOT_ON_CHAIN);
        }

        List<BlockCertificateCreateReqDTO> blockCertificates = new ArrayList<>();
        BlockCertificateCreateReqDTO blockCertificate = new BlockCertificateCreateReqDTO();
        blockCertificate.setChainOperateId(CodeEnum.CHAIN_OPERATE_CODE.getCode());
        blockCertificate.setOperateType(OperateTypeEnum.BATCH.getType()); // 操作类型
        blockCertificate.setOperateName(OperateTypeEnum.BATCH.getName()); //
        blockCertificate.setBatchId(inspectionDO.getHarvestBatchId());
        blockCertificate.setBatchType(BatchTypeEnum.INSPECTION_BATCH.getType());
        // 上链未做完，这里只改状态
        blockCertificate.setChainState(ChainStateEnum.ON_CHAIN.getState()); // 上链状态（是否自动上链
        blockCertificate.setBelongBusinessId(inspectionDO.getBelongBusinessId());
        blockCertificate.setBelongBusinessName(inspectionDO.getBelongBusinessName());
        blockCertificate.setLinkId(CodeEnum.TRACE_LINK_CODE.getCode());
        blockCertificate.setLinkName("检验检疫");
        blockCertificate.setScanShow(Boolean.TRUE);
        blockCertificate.setSort(BigInteger.ZERO.intValue());

        String operateInfo = "检验检疫";
        String dateFormat = "yyyy-MM-dd";
        List<OperateDetailSaveReqDTO> operateDetails = new ArrayList<>();
        OperateDetailSaveReqDTO operateDetail = new OperateDetailSaveReqDTO();
        operateDetail.setTitle("入库批次");
        operateDetail.setContent(inspectionDO.getHarvestBatchId());
        operateDetail.setType(OperateDetailTypeEnum.TEXT.getType());
        operateDetail.setOperateInfo(operateInfo);
        operateDetail.setScanShow(Boolean.TRUE);
        operateDetail.setBelongBusinessId(inspectionDO.getBelongBusinessId());
        operateDetail.setBelongBusinessName(inspectionDO.getBelongBusinessName());
        operateDetails.add(operateDetail);

        OperateDetailSaveReqDTO harvestTime = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, harvestTime);
        harvestTime.setTitle("生产时间");
        harvestTime.setContent(DateUtil.format(inspectionDO.getHarvestTime(), dateFormat));
        operateDetails.add(harvestTime);

        OperateDetailSaveReqDTO inspectionStandard = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionStandard);
        inspectionStandard.setTitle("执行标准");
        inspectionStandard.setContent(inspectionDO.getInspectionStandard());
        operateDetails.add(inspectionStandard);

        OperateDetailSaveReqDTO inspectionResult = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionResult);
        inspectionResult.setTitle("检验结果");
        inspectionResult.setContent(inspectionDO.getInspectionResult().equals(InspectionResultEnums.QUALIFIED.getResult())?InspectionResultEnums.QUALIFIED.getResultName():InspectionResultEnums.UNQUALIFIED.getResultName());
        operateDetails.add(inspectionResult);

        OperateDetailSaveReqDTO inspectionOwnItem = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionOwnItem);
        inspectionOwnItem.setTitle("自检检验项目");
        inspectionOwnItem.setContent(inspectionDO.getInspectionOwnItem());
        operateDetails.add(inspectionOwnItem);

        OperateDetailSaveReqDTO inspectionUser = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionUser);
        inspectionUser.setTitle("检验人");
        inspectionUser.setContent(inspectionDO.getInspectionUser());
        operateDetails.add(inspectionUser);

        OperateDetailSaveReqDTO inspectionEntrustItem = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionEntrustItem);
        inspectionEntrustItem.setTitle("委托检验项目");
        inspectionEntrustItem.setContent(inspectionDO.getInspectionEntrustItem() == null ? "" : inspectionDO.getInspectionEntrustItem());
        operateDetails.add(inspectionEntrustItem);

        OperateDetailSaveReqDTO inspectionEntrustUser = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionEntrustUser);
        inspectionEntrustUser.setTitle("检测机构");
        inspectionEntrustUser.setContent(inspectionDO.getInspectionOrganization() == null ? "" : inspectionDO.getInspectionOrganization());
        operateDetails.add(inspectionEntrustUser);

        OperateDetailSaveReqDTO inspectionDate = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionDate);
        inspectionDate.setTitle("检验日期");
        inspectionDate.setContent(DateUtil.format(inspectionDO.getInspectionDate(), dateFormat));
        operateDetails.add(inspectionDate);

        OperateDetailSaveReqDTO inspectionLevel = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionLevel);
        inspectionLevel.setTitle("等级");
        inspectionLevel.setContent(inspectionDO.getInspectionLevel() == null ? "" : inspectionDO.getInspectionLevel());
        operateDetails.add(inspectionLevel);

        OperateDetailSaveReqDTO issuanceDate = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, issuanceDate);
        issuanceDate.setTitle("签发日期");
        issuanceDate.setContent(inspectionDO.getIssuanceDate() == null ? "-" : DateUtil.format(inspectionDO.getIssuanceDate(), dateFormat));
        operateDetails.add(issuanceDate);

        OperateDetailSaveReqDTO inspectionImgUrl = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionImgUrl);
        inspectionImgUrl.setTitle("检验图片");
        if(inspectionDO.getInspectionImgUrl() == null) {
            inspectionImgUrl.setContent("-");
        } else {
            inspectionImgUrl.setType(OperateDetailTypeEnum.IMAGE.getType());
            inspectionImgUrl.setContent(inspectionDO.getInspectionImgUrl());
        }
        operateDetails.add(inspectionImgUrl);

        OperateDetailSaveReqDTO inspectionVideoUrl = new OperateDetailSaveReqDTO();
        BeanUtil.copyProperties(operateDetail, inspectionVideoUrl);
        inspectionVideoUrl.setTitle("检验视频");
        if(inspectionDO.getInspectionVideoUrl() == null) {
            inspectionVideoUrl.setContent("-");
        } else {
            inspectionVideoUrl.setType(OperateDetailTypeEnum.VIDEO.getType());
            inspectionVideoUrl.setContent(inspectionDO.getInspectionVideoUrl());
        }
        operateDetails.add(inspectionVideoUrl);

        blockCertificate.setDetails(operateDetails);
        blockCertificates.add(blockCertificate);
        List<BlockCertificateRespDTO> blocks = blockCertificateApi.createBlockCertificateBatch(blockCertificates).getData();
        if(blocks == null || blocks.isEmpty()) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.CREATE_BLOCK_CERTIFICATE_FAIL);
        }

        inspectionDO.setBlockChainBlockIdentity(blocks.get(0).getChainBlockIdentity());
        inspectionDO.setBlockChainId(blocks.get(0).getChainOperateId());
        inspectionDO.setBlockState(BlockStateEnum.ACTIVATION.getBlockState());// 改为激活状态
        inspectionMapper.updateById(inspectionDO);

        // 生成检测报告
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void updateChainStatus(Long id, Integer state) {
        // 校验存在
        InspectionDO inspectionDO = validateInspectionExists(id);
        if(inspectionDO.getBlockState().equals(BlockStateEnum.NOT_ON_CHAIN.getBlockState())) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.ONLY_ON_CHAIN_INSPECTION_CAN_DO_UPDATE_CHAIN_STATUS);
        }

        Integer blockState = state.equals(BlockStateEnum.ACTIVATION.getBlockState()) ? BlockStateEnum.ACTIVATION.getBlockState() : BlockStateEnum.FREEZE.getBlockState();
        Integer chainStatus = state.equals(BlockStateEnum.ACTIVATION.getBlockState()) ? ChainStatusEnum.ACTIVE.getStatus() : ChainStatusEnum.FREEZE.getStatus();
        List<UpdateStatusReqDTO> reqDTOS = new ArrayList<>();
        UpdateStatusReqDTO inspectionReqDTO = new UpdateStatusReqDTO();
        inspectionReqDTO.setChainOperateId(inspectionDO.getBlockChainId());
        inspectionReqDTO.setChainStatus(chainStatus);
        reqDTOS.add(inspectionReqDTO);
        if(blockCertificateApi.updateStatusBatch(reqDTOS).isError()) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.UPDATE_CREATE_BLOCK_CERTIFICATE_FAIL);
        }

        InspectionDO updateDO = new InspectionDO();
        updateDO.setId(id);
        updateDO.setBlockState(blockState);

        inspectionMapper.updateById(updateDO);
    }

    @Override
    public List<InspectionRespVO> getByHarvestBatchId(String harvestBatchId) {
        Map<String, Object> params = new HashMap<>();
        params.put("harvestBatchId", harvestBatchId);
        // 获取检验检疫

        // 正常来说这个东西应该直接丢给前端包装，以节省性能，懒得改了。
//        List<InspectionSaveReqVO> results = new ArrayList<>();
//        String dateFormat = "yyyy-MM-dd";
//        respVOS.forEach(resp -> {
//            InspectionSaveReqVO inspection = new InspectionSaveReqVO();
//            inspection.setOperateObjId(resp.getId());
//            inspection.setLinkName("检验检疫");
//            inspection.setScanShow(Boolean.TRUE);
//
//            List<OperateDetailSaveReqVO> operateDetails = new ArrayList<>();
//            OperateDetailSaveReqVO harvestTime = new OperateDetailSaveReqVO();
//            harvestTime.setTitle("生产时间");
//            harvestTime.setContent(DateUtil.format(resp.getHarvestTime(), dateFormat));
//            harvestTime.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(harvestTime);
//
//            OperateDetailSaveReqVO inspectionStandard = new OperateDetailSaveReqVO();
//            inspectionStandard.setTitle("执行标准");
//            inspectionStandard.setContent(resp.getInspectionStandard());
//            inspectionStandard.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(inspectionStandard);
//
//            OperateDetailSaveReqVO inspectionResult = new OperateDetailSaveReqVO();
//            inspectionResult.setTitle("检验结果");
//            inspectionResult.setContent(resp.getInspectionResult().equals(InspectionResultEnums.QUALIFIED.getResult())?InspectionResultEnums.QUALIFIED.getResultName():InspectionResultEnums.UNQUALIFIED.getResultName());
//            inspectionResult.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(inspectionResult);
//
//            OperateDetailSaveReqVO inspectionOwnItem = new OperateDetailSaveReqVO();
//            inspectionOwnItem.setTitle("自检检验项目");
//            inspectionOwnItem.setContent(resp.getInspectionOwnItem());
//            inspectionOwnItem.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(inspectionOwnItem);
//
//            OperateDetailSaveReqVO inspectionUser = new OperateDetailSaveReqVO();
//            inspectionUser.setTitle("检验人");
//            inspectionUser.setContent(resp.getInspectionUser());
//            inspectionUser.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(inspectionUser);
//
//            OperateDetailSaveReqVO inspectionEntrustItem = new OperateDetailSaveReqVO();
//            inspectionEntrustItem.setTitle("委托检验项目");
//            inspectionEntrustItem.setContent(resp.getInspectionEntrustItem() == null ? "" : resp.getInspectionEntrustItem());
//            inspectionEntrustItem.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(inspectionEntrustItem);
//
//            OperateDetailSaveReqVO inspectionOrganization = new OperateDetailSaveReqVO();
//            inspectionOrganization.setTitle("检测机构");
//            inspectionOrganization.setContent(resp.getInspectionOrganization() == null ? "" : resp.getInspectionOrganization());
//            inspectionOrganization.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(inspectionOrganization);
//
//            OperateDetailSaveReqVO inspectionDate = new OperateDetailSaveReqVO();
//            inspectionDate.setTitle("检验日期");
//            inspectionDate.setContent(DateUtil.format(resp.getInspectionDate(), dateFormat));
//            inspectionDate.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(inspectionDate);
//
//            OperateDetailSaveReqVO issuanceDate = new OperateDetailSaveReqVO();
//            issuanceDate.setTitle("签发日期");
//            issuanceDate.setContent(resp.getIssuanceDate() == null ? "-" : DateUtil.format(resp.getIssuanceDate(), dateFormat));
//            issuanceDate.setType(OperateDetailTypeEnum.TEXT.getType());
//            operateDetails.add(issuanceDate);
//
//            OperateDetailSaveReqVO inspectionImgUrl = new OperateDetailSaveReqVO();
//            inspectionImgUrl.setTitle("检验图片");
//            if(resp.getInspectionImgUrl() == null) {
//                inspectionImgUrl.setType(OperateDetailTypeEnum.TEXT.getType());
//                inspectionImgUrl.setContent("-");
//            } else {
//                inspectionImgUrl.setType(OperateDetailTypeEnum.IMAGE.getType());
//                inspectionImgUrl.setContent(resp.getInspectionImgUrl());
//            }
//            operateDetails.add(inspectionImgUrl);
//
//            OperateDetailSaveReqVO inspectionVideoUrl = new OperateDetailSaveReqVO();
//            inspectionVideoUrl.setTitle("检验视频");
//            if(resp.getInspectionVideoUrl() == null) {
//                inspectionVideoUrl.setType(OperateDetailTypeEnum.TEXT.getType());
//                inspectionVideoUrl.setContent("-");
//            } else {
//                inspectionVideoUrl.setType(OperateDetailTypeEnum.VIDEO.getType());
//                inspectionVideoUrl.setContent(resp.getInspectionVideoUrl());
//            }
//            operateDetails.add(inspectionVideoUrl);
//
//            List<BlockCertificateSaveReqVO> inspectionItems = new ArrayList<>();
//            resp.getInspectionItems().forEach(item -> {
//                BlockCertificateSaveReqVO inspectionItem = new BlockCertificateSaveReqVO();
//                inspectionItem.setOperateObjId(item.getId());
//                inspectionItem.setLinkName("检验项目");
//                inspectionItem.setScanShow(Boolean.TRUE);
//
//                List<OperateDetailSaveReqVO> details = new ArrayList<>();
//                OperateDetailSaveReqVO itemName = new OperateDetailSaveReqVO();
//                itemName.setTitle("检验项目");
//                itemName.setContent(item.getInspectionItem());
//                itemName.setType(OperateDetailTypeEnum.TEXT.getType());
//                details.add(itemName);
//
//                OperateDetailSaveReqVO inspectionUnit = new OperateDetailSaveReqVO();
//                inspectionUnit.setTitle("单位");
//                inspectionUnit.setContent(item.getInspectionUnit());
//                inspectionUnit.setType(OperateDetailTypeEnum.TEXT.getType());
//                details.add(inspectionUnit);
//
//                OperateDetailSaveReqVO inspectionRequire = new OperateDetailSaveReqVO();
//                inspectionRequire.setTitle("技术要求");
//                inspectionRequire.setContent(item.getInspectionRequire());
//                inspectionRequire.setType(OperateDetailTypeEnum.TEXT.getType());
//                details.add(inspectionRequire);
//
//                OperateDetailSaveReqVO itemResult = new OperateDetailSaveReqVO();
//                itemResult.setTitle("检测结果");
//                itemResult.setContent(item.getInspectionResult());
//                itemResult.setType(OperateDetailTypeEnum.TEXT.getType());
//                details.add(itemResult);
//
//                OperateDetailSaveReqVO inspectionConclusion = new OperateDetailSaveReqVO();
//                inspectionConclusion.setTitle("单项结论");
//                inspectionConclusion.setContent(item.getInspectionConclusion());
//                inspectionConclusion.setType(OperateDetailTypeEnum.TEXT.getType());
//                details.add(inspectionConclusion);
//
//                inspectionItem.setOperateDetails(details);
//                inspectionItems.add(inspectionItem);
//            });
//
//            inspection.setOperateDetails(operateDetails);
//            inspection.setInspectionItems(inspectionItems);
//            results.add(inspection);
//        });
        return inspectionMapper.getInspectionList(params);
    }
}
