/*
 * 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.gpsboot.exception.BusinessException;
import com.tofly.pojo.maintain.dto.RepairmaterialDto;
import com.tofly.pojo.maintain.vo.RepairmaterialVo;
import com.tofly.gpsboot.mapper.maintain.RepairmaterialMapper;
import com.tofly.gpsboot.service.maintain.RepairmaterialService;
import com.tofly.pojo.maintain.Repairmaterial;
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("repairmaterialService")
public class RepairmaterialServiceImpl extends ServiceImpl<RepairmaterialMapper, Repairmaterial> implements RepairmaterialService {


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

    @Override
    public Page<RepairmaterialVo> getRepairMaterialPage(Page page, RepairmaterialDto parDto) {
        Repairmaterial repairMaterial = JSONObject.parseObject(JSON.toJSONString(parDto), Repairmaterial.class);

        QueryWrapper<Repairmaterial> wrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(parDto.getName())) {
            List<String> list = Arrays.asList(parDto.getName().split(","));
            wrapper.in("NAME", list);
            repairMaterial.setName(null);
        }
        wrapper.setEntity(repairMaterial);
        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<Repairmaterial> list = this.list();
        Repairmaterial repairmaterial = list.get(0);
        repairmaterial.setName("导入模板测试数据,正式导入清除");
        List<Repairmaterial> oneList = new ArrayList<>();
        oneList.add(repairmaterial);
        parentMap.put("list", oneList);
        returnMap.add(parentMap);
        return returnMap;
    }

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

            Set<String> collect = list.stream().map(Repairmaterial::getName).collect(Collectors.toSet());
            // 判断当前新增产品名称是否存在/或者是否为null
            repairmaterials.forEach(info -> {
                String name = info.getName();
                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_MATERIAL_DATA_NULL_ERROR_CODE,Constant.EXCEL_IMPORT_REPAIR_MATERIAL_DATA_NULL_ERROR);
            }else if (repeatList.size()>0){
                throw new BusinessException(Constant.EXCEL_IMPORT_REPAIR_MATERIAL_DATA_REPEAT_ERROR_CODE,"重复产品名称为: "+JSON.toJSONString(repeatList));
            }
            // 无错落库
            repairmaterials.forEach(this::save);
        }else {
            throw new BusinessException(Constant.EXCEL_IMPORT_DATA_NULL_ERROR_CODE,Constant.EXCEL_IMPORT_DATA_NULL_ERROR);
        }
        return true;
    }


}
