package digital.laboratory.platform.inspection.service.impl;
/*
 *@title ScreenInfoServiceImpl
 *@description
 *@author xy
 *@version 1.0
 *@create 2023/12/8 11:57
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.inspection.entity.AssignmentInfo;
import digital.laboratory.platform.inspetion.api.entity.SampleInfo;
import digital.laboratory.platform.inspection.entity.ScreenInfo;
import digital.laboratory.platform.inspection.mapper.ScreenInfoMapper;
import digital.laboratory.platform.inspection.service.AssignmentInfoService;
import digital.laboratory.platform.inspection.service.SampleInfoService;
import digital.laboratory.platform.inspection.service.ScreenInfoService;
import digital.laboratory.platform.inspetion.api.entity.MaterialDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
public class ScreenInfoServiceImpl extends ServiceImpl<ScreenInfoMapper, ScreenInfo> implements ScreenInfoService {

    @Resource
    private SampleInfoService sampleInfoService;

    @Resource
    private AssignmentInfoService assignmentInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ScreenInfo addScreenInfo(ScreenInfo screenInfo) {
        //设置业务类型
        screenInfo.setBusinessType("30001");
        if (StringUtils.isBlank(screenInfo.getId())) {
            screenInfo.setId(IdWorker.get32UUID());
            log.info("保存对象entrustInfo的ID为空，由系统生成一个给它使用 {}", screenInfo.getId());
        }
        ScreenInfo info = this.getOne(Wrappers.<ScreenInfo>lambdaQuery().eq(ScreenInfo::getOriginalId, screenInfo.getOriginalId()));
        if (info != null) {
            throw new RuntimeException(String.format("当前筛查任务已经同步至检验鉴定系统，无需再次同步!"));
        }
        List<MaterialDto> materialList = screenInfo.getMaterialList();
        if (materialList != null && materialList.size() > 0) {
            List<SampleInfo> sampleInfos = new ArrayList<>();
            for (MaterialDto material : materialList) {
                SampleInfo sampleInfo = new SampleInfo();
                sampleInfoService.copy(sampleInfo, material, 2);
                sampleInfo.setBusinessType(screenInfo.getBusinessType());
                sampleInfo.setBusinessId(screenInfo.getId());
                sampleInfos.add(sampleInfo);
            }
            sampleInfoService.saveBatch(sampleInfos);
        }
        boolean ret = this.save(screenInfo);
        if (ret) {
            log.info("{} 保存成功", screenInfo);
            return screenInfo;
        } else {
            log.info("{} 保存失败", screenInfo);
            return null;
        }
    }

    @Override
    public ScreenInfo updateScreenInfo(ScreenInfo screenInfo) {
        if (screenInfo.getSource() == 0) {
            log.info("数据是其他系统推送录入的，不能修改");
            return null;
        }
        boolean ret = this.updateById(screenInfo);
        if (ret) {
            return screenInfo;
        } else {
            return null;
        }
    }

    @Override
    public Boolean deleteScreenInfo(String id) {
        List<AssignmentInfo> assignmentInfos = assignmentInfoService.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, id));
        if (assignmentInfos.size() > 0) {
            throw new RuntimeException(String.format("当前业务已被分配，无法删除!"));
        }

        return this.removeById(id);
    }

    @Override
    public IPage getScreenPageList(Page page, ScreenInfo screenInfo,String keywords) {
        IPage<ScreenInfo> ret = this.page(page, Wrappers.<ScreenInfo>lambdaQuery()
                .like(StringUtils.isNotBlank(keywords), ScreenInfo::getCaseName,keywords)
                .eq(StringUtils.isNotBlank(screenInfo.getId()), ScreenInfo::getId, screenInfo.getId())
                .orderByDesc(ScreenInfo::getCreateTime));

        for (ScreenInfo info : ret.getRecords()) {
            List<AssignmentInfo> list = assignmentInfoService.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, info.getId()));
            if (list.size() > 0) {
                info.setIsDistribution(true);
            } else {
                info.setIsDistribution(false);
            }
        }

        return ret;
    }

    @Override
    public List<ScreenInfo> getScreenList(ScreenInfo screenInfo,String keywords) {
        List<ScreenInfo> retList = this.list(Wrappers.<ScreenInfo>lambdaQuery()
                .like(StringUtils.isNotBlank(keywords), ScreenInfo::getCaseName, keywords)
                .eq(StringUtils.isNotBlank(screenInfo.getId()), ScreenInfo::getId, screenInfo.getId())
                .orderByDesc(ScreenInfo::getCreateTime));

        for (ScreenInfo info : retList) {
            List<AssignmentInfo> list = assignmentInfoService.list(Wrappers.<AssignmentInfo>lambdaQuery().eq(AssignmentInfo::getBusinessId, info.getId()));
            if (list.size() > 0) {
                info.setIsDistribution(true);
            } else {
                info.setIsDistribution(false);
            }
        }
        return retList;
    }

    @Override
    public Boolean checkExist(String id) {
        List<ScreenInfo> retList = this.list(Wrappers.<ScreenInfo>lambdaQuery()
                .eq(ScreenInfo::getId, id));
        if (retList.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
}
