/*
 * Author: Zev
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.config.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.pojo.plan.vo.PeriodVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.plan.PeriodMapper;
import com.tofly.gpsboot.service.paln.task.PlanService;
import com.tofly.pojo.plan.Period;
import com.tofly.gpsboot.service.config.PeriodService;
import com.tofly.pojo.plan.Plan;
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;


/**
 * 巡检周期配置信息表：TF_YWPN_PERIOD_D
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("periodService")
public class PeriodServiceImpl extends ServiceImpl<PeriodMapper, Period> implements PeriodService {

    @Autowired
    private PlanService planService;

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

    @Override
    public void deletePeriodInfo(String idLists) throws BusinessException {
        // 判断当前是否有关联数据 计划
        List<Long> longList = Arrays.stream(idLists.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        List<Plan> list = planService.list(new QueryWrapper<Plan>().lambda().in(Plan::getPeriodId, longList));
        if (null != list && list.size() > 0) {
            throw new BusinessException(Constant.PERIOD_TYPE_NO_DELETE_CODE, Constant.PERIOD_TYPE_NO_DELETE);
        } else {
            this.removeByIds(longList);
        }
    }
}
