/*
 * Author: Zev
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.build.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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.tofly.gpsboot.common.Constant;
import com.tofly.pojo.build.dto.BuildtypeDto;
import com.tofly.pojo.build.vo.BuildtypeVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.build.BuildtypeMapper;
import com.tofly.gpsboot.service.build.BuildService;
import com.tofly.gpsboot.service.build.BuildtypeService;
import com.tofly.pojo.build.Build;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.pojo.build.Buildtype;
import org.springframework.util.StringUtils;

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


/**
 * 工地类型信息表
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("buildtypeService")
public class BuildtypeServiceImpl extends ServiceImpl<BuildtypeMapper, Buildtype> implements BuildtypeService {

    @Autowired
    private BuildService buildService;

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

    @Override
    public Page<BuildtypeVo> getBuildTypePage(Page page, BuildtypeDto parDto) {
//        Buildtype buildtype = JSON.parseObject(JSON.toJSONString(parDto),Buildtype.class);
        QueryWrapper<Buildtype> wrapper = new QueryWrapper();
        String name = parDto.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("NAME",name);
        }
//        wrapper.setEntity(buildtype);
        Page infoPageList = this.page(page, wrapper);
        infoPageList.setRecords(this.viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }

    @Override
    public void deleteBuildTypeInfo(String idLists) throws BusinessException {
        // 判断当前是否有关联数据 工地
        List<Long> longList = Arrays.stream(idLists.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        List<Build> list = buildService.list(new QueryWrapper<Build>().lambda().in(Build::getTypeId, longList));
        if(null!=list && list.size()>0){
            throw new BusinessException(Constant.BUILD_TYPE_NO_DELETE_CODE,Constant.BUILD_TYPE_NO_DELETE);
        }else {
            this.removeByIds(longList);
        }

    }
}
