/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.quality.entity.AssayReportW;
import com.tofly.quality.entity.AssayResultW;
import com.tofly.quality.entity.CollectW;
import com.tofly.quality.entity.ReportTypeD;
import com.tofly.quality.entity.dto.CollectWQuery;
import com.tofly.quality.mapper.AssayConfigRMapper;
import com.tofly.quality.mapper.AssayResultWMapper;
import com.tofly.quality.service.CollectWService;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.quality.mapper.CollectWMapper;

import java.sql.Wrapper;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采集记录管理
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@Service("collectWService")
public class CollectWServiceImpl extends ServiceImpl<CollectWMapper, CollectW> implements CollectWService {
   @Autowired
    AssayResultWMapper assayResultWMapper;

    @Autowired
    AssayConfigRMapper assayConfigRMapper;
    @Override
    public boolean save(CollectW entity) {
        //采样编号开头8位数：
        Date now = new Date(); // 创建一个Date对象，获取当前时间
        String strDateFormat = "yyyyMMdd";
        String start_no = DateFormatUtils.format(now, strDateFormat);
        //获取当天最大的采样编号
        String Sample_No = this.baseMapper.collectMaxNo(start_no);

        int firstNum = 1;
        if (Sample_No.length() == 12) {
            firstNum = Integer.parseInt(Sample_No.substring(9)) + 1;
        }

        String end_no = String.format("%04d", firstNum); //补全数字，不足用0补位

        String collectNo = start_no + end_no;

        entity.setCollectNo(collectNo);

        return super.save(entity);

    }


    @Override
    public Page<CollectW> getPageData(Page page, CollectWQuery query) {
        if (query.getStartTime() != null && query.getStartTime() != null) {
            if (query.getStartTime().getTime() > query.getEndTime().getTime()) {
                throw new RuntimeException("创建开始时间不能大于结束时间");
            }
        }
        if (query.getInspectionStartTime() != null && query.getInspectionEndTime() != null) {
            if (query.getInspectionStartTime().getTime() > query.getInspectionEndTime().getTime()) {
                throw new RuntimeException("送检开始时间不能大于结束时间");
            }
        }

        if (query.getCollectStartTime() != null && query.getCollectEndTime() != null) {
            if (query.getCollectStartTime().getTime() > query.getCollectEndTime().getTime()) {
                throw new RuntimeException("采样开始时间不能大于结束时间");
            }
        }

        return this.baseMapper.getPageData(page, query);
    }

    @Override
    public boolean deleteByIds(String ids) {

        List<String> list = Arrays.asList(ids.split(","));

        Long userId = SecurityUtils.getUserId();
        QueryWrapper<CollectW> querySWrapper = new QueryWrapper<>();
        querySWrapper.in("ID", list);
        querySWrapper.ne("CREATE_USER", userId);
        Integer count_check=0;
        Integer isDelRoot=assayConfigRMapper.isDeleteRoot(SecurityUtils.getUserId());
        if(isDelRoot==0){
             count_check = this.baseMapper.selectCount(querySWrapper);
            if (count_check > 0) {
                throw new RuntimeException("只能删除自己创建的数据");
            }
        }
        List<String> list_collectNo=this.baseMapper.selectBatchIds(list).stream().map(e->e.getCollectNo()).collect(Collectors.toList());
        QueryWrapper<AssayResultW> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("COLLECT_NO",list_collectNo);
        count_check = assayResultWMapper.selectCount(queryWrapper);
        if (count_check > 0) {
           throw new RuntimeException("采集记录已经有对应的化验结果，不能删除");
        }

        return this.removeByIds(list);
    }

    @Override
    public List<CollectW> getCollectList(Long resultId) {
        if(resultId==null){
            resultId=0L;
        }
        return this.baseMapper.getCollectList(resultId);
    }
}

