/*
 * 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.core.entity.ResultRespone;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.quality.entity.*;
import com.tofly.quality.entity.dto.AssayDicDQuery;
import com.tofly.quality.mapper.AssayConfigRMapper;
import com.tofly.quality.mapper.CollectWMapper;
import com.tofly.quality.service.AssayDicDService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.quality.mapper.AssayDicDMapper;

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

/**
 * 化验项字典
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@Service("assayDicDService")
@Slf4j
public class AssayDicDServiceImpl extends ServiceImpl<AssayDicDMapper, AssayDicD> implements AssayDicDService {

    @Autowired
    private AssayConfigRMapper assayConfigRMapper;

    @Autowired
    private CollectWMapper collectWMapper;

    @Override
    public Page<AssayDicD> getPageData(Page page, AssayDicDQuery 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 selectByName(String assayDicName, Long id) {

        if(StringUtils.isNullOrEmpty(assayDicName)){
           throw new RuntimeException("化验项字典名称不能为空");
        }

        QueryWrapper<AssayDicD> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ASSAY_DIC_NAME", assayDicName);
        if (id != 0) {
            queryWrapper.ne("ID", id);
        }
        Integer count = baseMapper.selectCount(queryWrapper);

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

    @Override
    public boolean deleteByIds(String ids) {

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

        C_queryWrapper.in("ASSAY_DIC_ID", list);

        Integer count = assayConfigRMapper.selectCount(C_queryWrapper);
        if (count > 0) {
            throw new RuntimeException("化验字典名称已经在化验项配置中使用，不能删除");
        }
        Integer isDelRoot=assayConfigRMapper.isDeleteRoot(SecurityUtils.getUserId());
        if(isDelRoot==0) {
            QueryWrapper<AssayDicD> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("ID", list);
            queryWrapper.ne("CREATE_USER", SecurityUtils.getUserId());
            Integer count_check = this.baseMapper.selectCount(queryWrapper);
            if (count_check > 0) {
                throw new RuntimeException("只能删除自己创建的数据");
            }
        }
        return this.removeByIds(list);
    }
}

