package com.yuanchu.mom.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.exception.ErrorException;
import com.yuanchu.mom.mapper.*;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.service.InsOrderPlanService;
import com.yuanchu.mom.service.PkMasterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.mom.vo.ProductVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.*;
/**
*
* 服务实现类
*
*
* @author 江苏鵷雏网络科技有限公司
* @since 2024-07-29 01:16:26
*/
@Service
public class PkMasterServiceImpl extends ServiceImpl implements PkMasterService {
@Resource
private PkMasterMapper pkMasterMapper;
@Resource
private PkSlaveMapper pkSlaveMapper;
@Resource
private InsOrderMapper insOrderMapper;
@Resource
private InsSampleMapper insSampleMapper;
@Resource
private InsProductMapper insProductMapper;
@Resource
private InsProductResultMapper insProductResultMapper;
@Resource
private InsProductUserMapper insProductUserMapper;
@Resource
private UserMapper userMapper;
@Resource
private InsOrderPlanService insOrderPlanService;
@Resource
GetLook getLook;
//温度循环数采
@Override
@Transactional(rollbackFor = Exception.class)
public int temDataAcquisition(PkMaster pkMaster) {
Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
//查询数采关联的子表信息
PkMaster master = pkMasterMapper.selectOne(Wrappers.lambdaQuery()
.eq(PkMaster::getEntrustCode, pkMaster.getEntrustCode())
.eq(PkMaster::getSampleCode, pkMaster.getSampleCode())
.eq(PkMaster::getModel, pkMaster.getModel())
.eq(PkMaster::getTemperature, pkMaster.getTemperature()));
if (ObjectUtils.isEmpty(master)) {
throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode() + "的数采数据");
}
List pkSlaves = pkSlaveMapper.selectList1(master.getIsid());
if (CollectionUtils.isEmpty(pkSlaves)) {
throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode() + "的数采子数据");
}
//查询对应的检验单表
InsOrder insOrder = insOrderMapper.selectOne(Wrappers.lambdaQuery().eq(InsOrder::getState, 1).eq(InsOrder::getEntrustCode, pkMaster.getEntrustCode()));
if (ObjectUtils.isEmpty(insOrder)) {
throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode());
}
//查询对应检验样品id
InsSample insSample = insSampleMapper.selectOne(Wrappers.lambdaQuery()
.eq(InsSample::getInsOrderId, insOrder.getId())
.eq(InsSample::getSampleCode, pkMaster.getSampleCode())
.eq(InsSample::getModel, pkMaster.getModel()));
if (ObjectUtils.isEmpty(insSample)) {
throw new ErrorException("没有找到对应委托单下的该样品" + pkMaster.getSampleCode());
}
//查询对应检验项目
Map map = insOrderPlanService.temCycle(insSample.getId(), pkMaster.getCycles(), pkMaster.getTemperature());
List productVos = (List) map.get("productVos");
if (CollectionUtils.isEmpty(map)) {
throw new ErrorException("没有找到对应委托单下该样品的相关温度循环检验项目的循环" + pkMaster.getCycles() + "次+温度" + pkMaster.getTemperature());
}
//todo 如果温度循环的项目nm有改动这里需要改动 @zss
for (PkSlave pkSlave : pkSlaves) {
for (ProductVo productVo : productVos) {
if (pkSlave.getInsBushingId().equals(productVo.getBushColor()) &&
pkSlave.getInsFibersId().equals(productVo.getCode()) &&
pkSlave.getInsFiberId().equals(productVo.getColor())) {
//找到对应的数据(对应套管光纤的数据)
List results = insProductResultMapper.selectList(Wrappers.lambdaQuery().eq(InsProductResult::getInsProductId, productVo.getInsProduct().getId()));
InsProductResult result;
if (CollectionUtils.isEmpty(results)) {
result = new InsProductResult();
} else {
result = results.get(0);
}
result.setInsProductId(productVo.getInsProduct().getId());//检验项目id
List