/*
 * 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.AssayDicD;
import com.tofly.quality.entity.CollectW;
import com.tofly.quality.entity.SamplePlaceD;
import com.tofly.quality.entity.dto.SamplePlaceDQuery;
import com.tofly.quality.mapper.AssayConfigRMapper;
import com.tofly.quality.mapper.CollectWMapper;
import com.tofly.quality.service.SamplePlaceDService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.quality.mapper.SamplePlaceDMapper;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 采样地点配置表
 *
 * @author admin
 * @date Wed Nov 02 00:00:00 CST 2022
 */
@Service("samplePlaceDService")
@Slf4j
public class SamplePlaceDServiceImpl extends ServiceImpl<SamplePlaceDMapper, SamplePlaceD> implements SamplePlaceDService {

    @Autowired
    private CollectWMapper collectWMapper;

    @Autowired
    private AssayConfigRMapper assayConfigRMapper;


    @Override
    public Page<SamplePlaceD> getPageData(Page page, SamplePlaceDQuery 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
    @Transactional
    public boolean deleteByIds(String ids) {

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

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

        queryWrapper.in("SAMPLE_PLACE_ID", list);

        Integer count = collectWMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new RuntimeException("采样地点已经在采集记录里面使用，不能删除");
        }

        Integer isDelRoot=assayConfigRMapper.isDeleteRoot(SecurityUtils.getUserId());
        if(isDelRoot==0){
            Long userId = SecurityUtils.getUserId();
            QueryWrapper<SamplePlaceD> 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(String samplePlaceName, Long id) {
        if(StringUtils.isNullOrEmpty(samplePlaceName)){
            throw new RuntimeException("采样点名称不能为空");
        }

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

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


}
