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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.User;
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.utils.DateTimeUtil;
import com.tofly.xrtymis.custom.vo.GdgltTypeTreeVO;
import com.tofly.xrtymis.entity.Gdflowconfig;
import com.tofly.xrtymis.entity.Gdgltypebm;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.xrtymis.mapper.GdgltypebmMapper;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.time.ZoneId;
import java.util.*;


/**
 * 业务工单类型编码表
 *
 * @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) throws InvocationTargetException, IllegalAccessException {
        if(!"1".equals(yxbz)){
            yxbz = null;
        }
        if(StringUtils.isEmpty(datatype)){
            datatype = "0";
        }
        Map<Long,GdgltTypeTreeVO> map = new HashMap<>();
        List<GdgltTypeTreeVO> treeVOList = new ArrayList<>();
        List<Gdgltypebm> gdgltypebmList = list(new QueryWrapper<Gdgltypebm>().lambda()
                .eq(Gdgltypebm::getStatusFlag,1).like(StringUtil.isNotEmpty(viewType),Gdgltypebm::getViewType , viewType)
                .eq(!StringUtils.isEmpty(yxbz),Gdgltypebm::getYxbz,yxbz).eq(Gdgltypebm::getDataType,datatype).orderByAsc(Gdgltypebm::getGdlxId));
        for (Gdgltypebm g : gdgltypebmList) {
            GdgltTypeTreeVO vo = new GdgltTypeTreeVO();
            BeanUtils.copyProperties(vo,g);
            map.put(vo.getId(),vo);
            if(vo.getGdlxId()==0){
                vo.setDj(1);
                if("0".equals(datatype)) {
                    List<Gdflowconfig> permissions = gdflowconfigService.list(new QueryWrapper<Gdflowconfig>().lambda()
                            .eq(!StringUtils.isEmpty(yxbz), Gdflowconfig::getSfDelete, yxbz)
                            .eq(Gdflowconfig::getGdlx, g.getId()).isNull(Gdflowconfig::getGdxf1).like(StringUtil.isNotEmpty(viewType),Gdflowconfig::getViewType , viewType));
                    if (!CollectionUtil.isEmpty(permissions)) {
                        vo.setIslc(1);
                    }
                }
                treeVOList.add(vo);
            }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);
                }
                if(fvo.getGdglproblemdetails()==null){
                    List<GdgltTypeTreeVO> fvos = new ArrayList<>();
                    fvo.setGdglproblemdetails(fvos);
                }
                fvo.getGdglproblemdetails().add(vo);
                if("0".equals(datatype)) {
                    List<Gdflowconfig> permissions = gdflowconfigService.list(new QueryWrapper<Gdflowconfig>().lambda()
                            .eq(!StringUtils.isEmpty(yxbz), Gdflowconfig::getSfDelete, yxbz)
                            .eq(Gdflowconfig::getGdlx, g.getGdlxId()).eq(Gdflowconfig::getGdxf1, g.getId()).isNull(Gdflowconfig::getGdxf2)
                            .like(StringUtil.isNotEmpty(viewType),Gdflowconfig::getViewType , viewType));
                    if (!CollectionUtil.isEmpty(permissions)) {
                        vo.setIslc(1);
                    }
                }
            }
        }
        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;
    }
}
