package com.xjrsoft.module.asset.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.module.asset.dto.WfAssetManageConditionalSearchQueryDto;
import com.xjrsoft.module.asset.dto.WfAssetManagePageDto;
import com.xjrsoft.module.asset.dto.WfAssetManageSelectRecordQueryDto;
import com.xjrsoft.module.asset.entity.WfAssetManage;
import com.xjrsoft.module.asset.mapper.WfAssetManageMapper;
import com.xjrsoft.module.asset.service.IWfAssetManageService;
import com.xjrsoft.module.asset.vo.WfAssetManagePageVo;
import com.xjrsoft.module.asset.vo.WfAssetManageQueryVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @title: 资产管理
* @Author dzx
* @Date: 2024-03-28
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class WfAssetManageServiceImpl extends MPJBaseServiceImpl<WfAssetManageMapper, WfAssetManage> implements IWfAssetManageService {

    private final WfAssetManageMapper wfAssetManageMapper;
    @Override
    public IPage<WfAssetManagePageVo> getPage(WfAssetManagePageDto dto) {
        IPage<WfAssetManagePageVo> page = wfAssetManageMapper.getPage(ConventPage.getPage(dto), dto);
        return page;
    }

    @Override
    public ByteArrayOutputStream listWfAssetManageSelectRecordQuery(WfAssetManageSelectRecordQueryDto dto) {
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        List<WfAssetManageQueryVo> result = wfAssetManageMapper.listWfAssetManageSelectRecordQuery(dto);
        if(!result.isEmpty()){
            //以第一条数据中的其他信息的key作为整个excel的表头，后面的数据根据填充
            List<List<String>> headList = head(result.get(0));
            List<List<Object>> dataList = dataList(headList, result);

            EasyExcel.write(bot).head(headList).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);

            return bot;
        }
        return bot;

    }

    @Override
    public ByteArrayOutputStream listWfAssetManageConditionalSearchQuery(WfAssetManageConditionalSearchQueryDto dto) {
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        List<WfAssetManageQueryVo> result = wfAssetManageMapper.listWfAssetManageConditionalSearchQuery(dto);
        if(!result.isEmpty()){
            //以第一条数据中的其他信息的key作为整个excel的表头，后面的数据根据填充
            List<List<String>> headList = head(result.get(0));
            List<List<Object>> dataList = dataList(headList, result);

            EasyExcel.write(bot).head(headList).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(dataList);

            return bot;
        }
        return bot;
    }

    private List<List<String>> head(WfAssetManageQueryVo firstRecord) {
        List<List<String>> list = ListUtils.newArrayList();

        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> extendJsonMap = new HashMap<>();
        try {
            extendJsonMap = objectMapper.readValue(firstRecord.getExtendJson(), new TypeReference<Map<String, Object>>(){});
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 获取对象的所有属性
        String[] fields = {"资产类型", "资产类别", "资产种类", "名称", "所属部门", "使用人", "金额"};
        for (String field : fields) {
            list.add(new ArrayList<String>(){{
                add(field);
            }});
        }

        for (Map.Entry<String, Object> entry : extendJsonMap.entrySet()){
            String key = entry.getKey();
            list.add(new ArrayList<String>(){{
                add(key);
            }});
        }

        return list;
    }

    private List<List<Object>> dataList(List<List<String>> headList, List<WfAssetManageQueryVo> result) {
        List<List<Object>> list = ListUtils.newArrayList();
        ObjectMapper objectMapper = new ObjectMapper();
        for (WfAssetManageQueryVo wfAssetManageQueryVo : result) {
            List<Object> oneRecord = ListUtils.newArrayList();

            //先处理固定字段
            oneRecord.add(wfAssetManageQueryVo.getAssetTypeCn());
            oneRecord.add(wfAssetManageQueryVo.getAssetCategoryCn());
            oneRecord.add(wfAssetManageQueryVo.getAssetSpeciesCn());
            oneRecord.add(wfAssetManageQueryVo.getName());
            oneRecord.add(wfAssetManageQueryVo.getDeptIdCn());
            oneRecord.add(wfAssetManageQueryVo.getUserIdCn());
            oneRecord.add(wfAssetManageQueryVo.getAmount());

            String extendJson = wfAssetManageQueryVo.getExtendJson();
            if(!extendJson.isEmpty()){
                try {
                    Map<String, Object> data = objectMapper.readValue(extendJson, new TypeReference<Map<String, Object>>(){});
                    for (List<String> head : headList){
                        if(data.get(head.get(0)) != null){
                            oneRecord.add(data.get(head.get(0)));
                        }
                    }
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
            list.add(oneRecord);
        }
        return list;
    }
}
