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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.*;
import com.tofly.quality.mapper.AssayConfigRMapper;
import com.tofly.quality.mapper.AssayReportWMapper;
import com.tofly.quality.service.ReportTypeDService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.quality.mapper.ReportTypeDMapper;
import java.util.Arrays;
import java.util.List;

/**
 * 报告类别
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@Service("reportTypeDService")
@Slf4j
public class ReportTypeDServiceImpl extends ServiceImpl<ReportTypeDMapper, ReportTypeD> implements ReportTypeDService {

    @Autowired
    private AssayReportWMapper assayReportWMapper;
    @Autowired
    private AssayConfigRMapper assayConfigRMapper;

    @Override
    public boolean deleteByIds(String ids) {
        QueryWrapper<AssayReportW> queryWrapper = new QueryWrapper<>();

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

        queryWrapper.in("REPORT_TYPE_ID", list);

        Integer count = assayReportWMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new RuntimeException("报告类别已经在化验报告中使用，不能删除");
        }

        Long userId = SecurityUtils.getUserId();
        Integer isDelRoot=assayConfigRMapper.isDeleteRoot(SecurityUtils.getUserId());
        if(isDelRoot==0){
            QueryWrapper<ReportTypeD> 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("只能删除自己创建的数据");
            }
        }
        return this.removeByIds(list);
    }


    @Override
    public boolean selectByName(ReportTypeD reportTypeD) {

        QueryWrapper<ReportTypeD> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("REPORT_TYPE_NAME", reportTypeD.getReportTypeName());
        if (reportTypeD.getId()!=null && reportTypeD.getId()>0) {
            queryWrapper.ne("ID", reportTypeD.getId());
        }
        Integer count = baseMapper.selectCount(queryWrapper);

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

}
