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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.entity.Rivermanage;
import com.tofly.xrtygis.entity.Waterparams;
import com.tofly.xrtygis.mapper.RivermanageMapper;
import com.tofly.xrtygis.service.RivermanageService;
import com.tofly.xrtygis.service.WaterparamsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 河流管理配置表
 *
 * @author admin
 * @date Thu Sep 09 00:00:00 CST 2021
 */
@Service("rivermanageService")
@AllArgsConstructor
public class RivermanageServiceImpl extends ServiceImpl<RivermanageMapper, Rivermanage> implements RivermanageService {

    private final WaterparamsService waterparamsService;

    /**
     * 删除河道配置
     *
     * @param ids id
     * @return boolean
     */
    @Override
    public boolean deleteRiver(String ids) {

        if (null == ids){
            throw new ToflyDeniedException("请传入id");
        }

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

        for (String id : idsList) {

            waterparamsService.remove(Wrappers.<Waterparams>lambdaQuery().eq(Waterparams::getRvid,id));

            baseMapper.deleteById(id);
        }
        return true;
    }

    /**
     * 添加河流配置信息
     *
     * @param river river
     * @return boolean
     */
    @Override
    public boolean insertRiver(Rivermanage river) {
        if (null == river){
            throw new ToflyDeniedException("数据不允许为空");
        }
        if (river.getCode() == null || "".equals(river.getCode())){
            throw new ToflyDeniedException("编号不允许为空");
        }
        if (river.getName() == null || "".equals(river.getName())){
            throw new ToflyDeniedException("名称不能为空");
        }
        List<Rivermanage> list = this.list(Wrappers.<Rivermanage>lambdaQuery()
                .eq(Rivermanage::getCode, river.getCode())
                .select(Rivermanage::getCode));
        if (list != null && list.size() >= 1){
            throw new ToflyDeniedException("编号：" + river.getCode() + " 已存在");
        }

        List<Rivermanage> list2 = this.list(Wrappers.<Rivermanage>lambdaQuery()
                .eq(Rivermanage::getName, river.getName())
                .select(Rivermanage::getName));
        if (list2 != null && list2.size() >= 1){
            throw new ToflyDeniedException("河道：" + river.getName() + " 已配置");
        }

        return river.insert();
    }

    /**
     * 获取码表信息
     * @return list
     */
    @Override
    public List<Map> getCodeInfo() {
        return baseMapper.getCodeInfo();
    }


}
