package com.willalp.yiqing.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.bean.BeanUtils;
import com.willalp.yiqing.domain.HsOrdersLog;
import com.willalp.yiqing.domain.HsOrdersLogData;
import com.willalp.yiqing.domain.HsYiqingRnsResult;
import com.willalp.yiqing.domain.vo.OrdersVo;
import com.willalp.yiqing.domain.vo.RegisterVo;
import com.willalp.yiqing.mapper.HsOrdersLogDataMapper;
import com.willalp.yiqing.service.IHsOrdersLogDataService;
import com.willalp.yiqing.service.IHsOrdersLogService;
import com.willalp.yiqing.service.IHsYiqingRnsResultService;
import org.springframework.stereotype.Service;

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

/**
 * 派单记录数据Service业务层处理
 *
 * @author willalp
 * @date 2022-05-24
 */
@Service
public class HsOrdersLogDataServiceImpl extends ServiceImpl<HsOrdersLogDataMapper, HsOrdersLogData> implements IHsOrdersLogDataService {
    @Resource
    private HsOrdersLogDataMapper hsOrdersLogDataMapper;

    @Resource
    private IHsOrdersLogService ordersLogService;

    @Resource
    private IHsYiqingRnsResultService resultService;

    /**
     * 查询派单记录数据列表
     *
     * @param hsOrdersLogData 派单记录数据
     * @return 派单记录数据
     */
    @Override
    public List<HsOrdersLogData> selectHsOrdersLogDataList(HsOrdersLogData hsOrdersLogData) {
        return hsOrdersLogDataMapper.selectHsOrdersLogDataList(hsOrdersLogData);
    }

    @Override
    public void insertDatas(List<RegisterVo> list, OrdersVo ordersVo) {
        if (0 != list.size()) {
            //生成记录
            HsOrdersLog ordersLog = new HsOrdersLog();
            ordersLog.setOrdersTime(ordersVo.getOrderImportTime());
            ordersLog.setDays(String.valueOf(ordersVo.getRegisterGapDay()));
            ordersLog.setRnsResult(ordersVo.getRnaResult());
            ordersLog.setDateline(new Date());
            String idStr = IdWorker.getIdStr();
            while (true) {
                HsOrdersLog ordersLog1 = ordersLogService.getBaseMapper().selectById(idStr);
                if (null == ordersLog1) {
                    break;
                }
            }
            ordersLog.setId(idStr);
            ordersLogService.save(ordersLog);
            List<HsYiqingRnsResult> rs = resultService.getBaseMapper().selectList(null);
            List<HsOrdersLogData> saveList = new ArrayList<>();
            list.forEach(item -> {
                HsOrdersLogData ordersLogData = new HsOrdersLogData();
                BeanUtils.copyBeanProp(ordersLogData, item);
                ordersLogData.setLogId(idStr);
                int size = rs.size();
                for (int i = 0; i < size; i++) {
                    if (StringUtils.isNotBlank(rs.get(i).getUserDataId()) &&
                            rs.get(i).getUserDataId().equals(item.getUserDataId()) &&
                            DateUtil.formatDate(rs.get(i).getSamplingTime()).equals(item.getSendOrdersEnd())) {
                        ordersLogData.setSamplingTime(rs.get(i).getSamplingTime());
                        ordersLogData.setAntigenResult(rs.get(i).getAntigenResult());
                        ordersLogData.setCtValue(rs.get(i).getCtValue());
                        ordersLogData.setRnaResult(rs.get(i).getRnaResult());
                        ordersLogData.setResultTime(rs.get(i).getResultTime());
                        ordersLogData.setExcelT1(rs.get(i).getExcelT1());
                        ordersLogData.setExcelT2(rs.get(i).getExcelT2());
                        break;
                    }
                }
                saveList.add(ordersLogData);
            });
            this.saveBatch(saveList);
        }
    }
}
