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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.maintain.Repairorder;
import com.tofly.pojo.maintain.dto.RepairtypeDto;
import com.tofly.pojo.maintain.vo.RepairtypeVo;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.maintain.RepairtypeMapper;
import com.tofly.gpsboot.service.maintain.RepairtypeService;
import com.tofly.pojo.maintain.Repairtype;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;


/**
 * 维修类型信息表
 *
 * @author admin
 * @date Tue Apr 06 00:00:00 CST 2021
 */
@Service("repairtypeService")
public class RepairtypeServiceImpl extends ServiceImpl<RepairtypeMapper, Repairtype> implements RepairtypeService {

    @Autowired
    private RepairtypeMapper repairtypeMapper;

    @Override
    public List<RepairtypeVo> viewConversion(List<Repairtype> infoList) {
        return JSONArray.parseArray(JSON.toJSONString(infoList), RepairtypeVo.class);
    }

    @Override
    public void deleteRepairTypeInfo(String idLists) throws BusinessException {
        // 判断当前是否有关联数据 巡检点信息表 巡检线信息表 巡检计划详细信息表
        List<Long> longList = Arrays.stream(idLists.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        QueryWrapper<Repairorder> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("p.ID",longList);
        Integer isDelete = repairtypeMapper.isDelete(queryWrapper);
        if(1<isDelete){
            throw new BusinessException(Constant.REPAIR_TYPE_NO_DELETE_CODE,Constant.REPAIR_TYPE_NO_DELETE);
        }else {

            this.removeByIds(longList);
        }
    }

    @Override
    public Page<RepairtypeVo> getRepairTypePage(Page page, RepairtypeDto parDto) {
        Repairtype repairType = JSONObject.parseObject(JSON.toJSONString(parDto), Repairtype.class);

        QueryWrapper<Repairtype> wrapper = new QueryWrapper();
        if(!StringUtils.isEmpty(parDto.getName())){
            wrapper.like("NAME",parDto.getName());
            repairType.setName(null);
        }
        wrapper.setEntity(repairType);
        wrapper.orderByDesc("CREATE_TIME");
        Page infoPageList = this.page(page, wrapper);
        infoPageList.setRecords(this.viewConversion(infoPageList.getRecords()));
        return infoPageList;
    }

    @Override
    public List<Map<String, Object>> getExportData(String dto, String templateFlag) {
        // 目前只有下载模板
        List<Map<String, Object>> returnMap = new ArrayList<>();
        Map<String, Object> parentMap = new HashMap<>(12);
        List<Repairtype> list = this.list();
        Repairtype repairmaterial = list.get(0);
        repairmaterial.setGroupName("导入模板测试数据,正式导入清除");
        List<Repairtype> oneList = new ArrayList<>();
        oneList.add(repairmaterial);
        parentMap.put("list", oneList);
        returnMap.add(parentMap);
        return returnMap;
    }

    @Override
    public Boolean excelDataStorage(List<Object> dataList) {
        List<Repairtype> list = this.list();
        List<Repairtype> repairTypeList = JSONArray.parseArray(JSON.toJSONString(dataList), Repairtype.class);
        if(null!=repairTypeList){
            // 判断传入数据是否产品名称是否为null
            AtomicBoolean isNull = new AtomicBoolean(false);
            // 数据重复列表
            List<String> repeatList = new ArrayList<>();

            Set<String> collect = list.stream().map(Repairtype::getGroupName).collect(Collectors.toSet());
            // 判断当前新增归属类名称是否存在/或者是否为null
            repairTypeList.forEach(info -> {
                String name = info.getGroupName();
                if(StringUtils.isEmpty(name)){
                    isNull.set(true);
                }else {
                    if(collect.contains(name)){
                        repeatList.add(name);
                    }
                }
            });

            if(isNull.get()){
                throw new BusinessException(Constant.EXCEL_IMPORT_REPAIR_TYPE_DATA_NULL_ERROR_CODE,Constant.EXCEL_IMPORT_REPAIR_TYPE_DATA_NULL_ERROR);
            }else if (repeatList.size()>0){
                throw new BusinessException(Constant.EXCEL_IMPORT_REPAIR_TYPE_DATA_REPEAT_ERROR_CODE,"重复归属类名称为: "+JSON.toJSONString(repeatList));
            }
            // 无错落库
            repairTypeList.forEach(this::save);
        }else {
            throw new BusinessException(Constant.EXCEL_IMPORT_DATA_NULL_ERROR_CODE,Constant.EXCEL_IMPORT_DATA_NULL_ERROR);
        }
        return true;
    }
}
