/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtymis.custom.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.core.util.io.ExcelUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.pojo.comm.Audit;
import com.tofly.xrtymis.custom.entity.OrderTypeTemplate;
import com.tofly.xrtymis.custom.service.GdflowconfigService;
import com.tofly.xrtymis.custom.service.GdgltypebmService;
import com.tofly.xrtymis.custom.vo.GdgltTypeTreeVO;
import com.tofly.xrtymis.entity.Gdgltypebm;
import com.tofly.xrtymis.mapper.GdgltypebmMapper;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 业务工单类型编码表
 *
 * @author fu
 * @date Sat Mar 20 00:00:00 CST 2021
 */
@Service("gdgltypebmService")
public class GdgltypebmServiceImpl extends ServiceImpl<GdgltypebmMapper, Gdgltypebm> implements GdgltypebmService {

    @Autowired
    private GdflowconfigService gdflowconfigService;

    @Override
    public ResultRespone getGdgltypeTree(String yxbz, String datatype, String viewType) {
        if (!"1".equals(yxbz)) {
            yxbz = null;
        }
        if (StringUtils.isEmpty(datatype)) {
            datatype = "0";
        }

        String statusFlag = "1";

        List<GdgltTypeTreeVO> vos = this.baseMapper.getTypeTree(statusFlag, yxbz, datatype, viewType);
        Map<Long, GdgltTypeTreeVO> map = new HashMap<>();
        List<GdgltTypeTreeVO> treeVOList = new ArrayList<>();

        for (GdgltTypeTreeVO vo : vos) {
            map.put(vo.getId(), vo);
            vo.setGdglproblemdetails(new ArrayList<>());
            if (vo.getGdlxId() == 0) {
                treeVOList.add(vo);
                vo.setDj(1);
            } else {
                if (!map.containsKey(vo.getGdlxId())) {
                    continue;
                }
                GdgltTypeTreeVO fvo = map.get(vo.getGdlxId());
                if (fvo.getDj() == 1) {
                    vo.setDj(2);
                } else if (fvo.getDj() == 2) {
                    vo.setDj(3);
                }
                fvo.getGdglproblemdetails().add(vo);
            }
        }

        if (CollectionUtil.isNotEmpty(treeVOList)) {
            List<GdgltTypeTreeVO> result = treeVOList.stream().filter(Objects::nonNull)
                    .sorted(Comparator.comparing(Gdgltypebm::getGdlxMc))
                    .collect(Collectors.toList());
            for (GdgltTypeTreeVO vo : result) {
                List<GdgltTypeTreeVO> gdglproblemdetails = vo.getGdglproblemdetails();
                if (CollectionUtil.isNotEmpty(gdglproblemdetails)) {
                    vo.setGdglproblemdetails(gdglproblemdetails.stream()
                            .sorted(Comparator.comparing(Gdgltypebm::getGdlxMc)).collect(Collectors.toList()));
                }
            }
            return ResultRespone.success(result);
        }
        return ResultRespone.success(treeVOList);
    }

    @Override
    @SneakyThrows
    public boolean orderTypeImport(MultipartFile file) {
        if (StringUtil.isNull(file)) {
            throw new ToflyDeniedException("上传文件为空");
        }
        InputStream inputStream = file.getInputStream();
        if (inputStream == null) {
            throw new ToflyDeniedException("文件流关闭");
        }
        ExcelUtil opsExcel = new ExcelUtil(OrderTypeTemplate.class);
        List<OrderTypeTemplate> ips = opsExcel.importExcel(inputStream);
        Date createTime = Date8Util.getCurrentDate();
        if (ips == null || ips.isEmpty()) {
            throw new ToflyDeniedException("数据为空！");
        }

        return true;
    }

    @Override
    public List<Audit> getAuditInfo(Audit audit) {
        return this.baseMapper.getAuditInfo(audit);
    }

    @Override
    public void updateViewType(Long id, String viewType) {
        baseMapper.updateViewType(id, viewType);
    }
}
