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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.quality.entity.AssayConfigD;
import com.tofly.quality.entity.AssayConfigR;
import com.tofly.quality.entity.AssayResultW;
import com.tofly.quality.entity.CollectW;
import com.tofly.quality.entity.dto.AssayConfigDQuery;
import com.tofly.quality.mapper.AssayConfigRMapper;
import com.tofly.quality.mapper.AssayResultWMapper;
import com.tofly.quality.mapper.CollectWMapper;
import com.tofly.quality.service.AssayConfigDService;
import com.tofly.quality.service.AssayConfigRService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.quality.mapper.AssayConfigDMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 化验项配置
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@Service("assayConfigDService")
@Slf4j
public class AssayConfigDServiceImpl extends ServiceImpl<AssayConfigDMapper, AssayConfigD> implements AssayConfigDService {

    @Autowired
    private CollectWMapper collectWMapper;
    @Autowired
    private AssayConfigRService assayConfigRService;

    @Autowired
private AssayConfigRMapper assayConfigRMapper;

    @Override
    public boolean selectByName(String assayConfigName, long id) {
        QueryWrapper<AssayConfigD> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASSAY_CONFIG_NAME", assayConfigName);
        if (id != 0) {
            queryWrapper.ne("ID", id);
        }
        Integer count = baseMapper.selectCount(queryWrapper);

        if (count > 0) {
            return false;
        }
        return true;
    }

    @Override
    public Page<AssayConfigD> getPageData(Page page, AssayConfigDQuery query) {
        if (query.getStartTime() != null && query.getStartTime() != null) {
            if (query.getStartTime().getTime() > query.getEndTime().getTime()) {
                throw new RuntimeException("开始时间不能大于结束时间");
            }
        }

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

    @Override
    public boolean deleteByIds(String ids) {

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

        QueryWrapper<CollectW> W_queryCWrapper = new QueryWrapper<>();

        W_queryCWrapper.in("ASSAY_CONFIG_ID", list);

        Integer count_c = collectWMapper.selectCount(W_queryCWrapper);
        if (count_c > 0) {
            throw new RuntimeException("化验项配置已经在采集记录中使用，不能删除");
        }


        Long userId = SecurityUtils.getUserId();
        Integer isDelRoot=assayConfigRMapper.isDeleteRoot(userId);
        if(isDelRoot==0){
        QueryWrapper<AssayConfigD> querySWrapper = new QueryWrapper<>();
        querySWrapper.in("ID", list);
        querySWrapper.ne("CREATE_USER", userId);
        Integer count_check = this.baseMapper.selectCount(querySWrapper);
        if (count_check > 0) {
            throw new RuntimeException("只能删除自己创建的数据");
        }
        }
  //删除中间表
        QueryWrapper<AssayConfigR> queryWrapperR = new QueryWrapper<>();
        queryWrapperR.in("ASSAY_CONFIG_ID",list);
        assayConfigRMapper.delete(queryWrapperR);
        return this.removeByIds(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveConfig(AssayConfigD assayConfigD, boolean isAdd) {

        //AssayConfigD assayConfigD = JSONObject.parseObject(JSON.toJSONString(assayConfigDVo), AssayConfigD.class);
        boolean flag = false;

        Long config_id = null;
        if (isAdd) {
            flag = this.save(assayConfigD);
        } else {
            flag = this.updateById(assayConfigD);

              QueryWrapper<AssayConfigR> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ASSAY_CONFIG_ID", assayConfigD.getId());
                assayConfigRService.remove(queryWrapper);

        }
        config_id = assayConfigD.getId();

        if (!StringUtils.isNullOrEmpty(assayConfigD.getConfigAssayDicIds())) {
            List<AssayConfigR> list = new ArrayList<>();
            for (String dic_id : assayConfigD.getConfigAssayDicIds().split(",")
            ) {
                AssayConfigR model = new AssayConfigR();
                model.setAssayConfigId(config_id);
                model.setAssayDicId(Long.parseLong(dic_id));
                list.add(model);
            }

            flag = assayConfigRService.saveBatch(list);

        }
        return flag;
    }

}
