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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.trouble.ToublerangeMapper;
import com.tofly.gpsboot.service.trouble.ToublerangeService;
import com.tofly.gpsboot.service.trouble.TroubleService;
import com.tofly.pojo.trouble.Toublerange;
import com.tofly.pojo.trouble.Trouble;
import com.tofly.pojo.trouble.vo.ToublerangeVo;
import com.tofly.pojo.trouble.vo.ToubletypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 隐患等级表
 *
 * @author admin
 * @date Fri Aug 06 00:00:00 CST 2021
 */
@Service("toublerangeService")
public class ToublerangeServiceImpl extends ServiceImpl<ToublerangeMapper, Toublerange> implements ToublerangeService {

    @Autowired
    private TroubleService troubleService;

    @Override
    public List<ToublerangeVo> viewConversion(List<Toublerange> infoList) {
        /// 无转换类型
        return JSONArray.parseArray(JSON.toJSONString(infoList), ToublerangeVo.class);
    }

    @Override
    public void deleteToublerangeInfo(String idLists) throws BusinessException {
        // 是否已经关联数据
        List<Long> longList = Arrays.stream(idLists.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        List<Trouble> list = troubleService.list(new QueryWrapper<Trouble>().lambda().in(Trouble::getToubleRangeId, longList));
        if(null!=list && list.size()>0){
            throw new BusinessException(Constant.TROUBLE_RANGE_NO_DELETE_CODE,Constant.TROUBLE_RANGE_NO_DELETE);
        }else {
            this.removeByIds(longList);
        }
    }
}
