package com.simuwang.manage.api.deletion;

import com.simuwang.base.ano.ExcludeGlobalResult;
import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.common.util.EncodeUtil;
import com.simuwang.base.common.util.ExcelUtil;
import com.simuwang.base.pojo.dto.DeletionDownParam;
import com.simuwang.base.pojo.dto.ExcelDeletionInfoDTO;
import com.simuwang.base.pojo.dto.query.DeletionPageQuery;
import com.simuwang.base.pojo.dto.query.FundDeletionPageQuery;
import com.simuwang.base.pojo.vo.*;
import com.simuwang.logging.SystemLog;
import com.simuwang.manage.service.DeletionService;
import com.smppw.common.pojo.ResultVo;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缺失管理
 * Author:   chenjianhua
 * Date:     2024/9/17 18:16
 * Description: ${DESCRIPTION}
 */
@SystemLog(value = "缺失管理")
@RestController
@RequestMapping("/v1/deletion")
public class DeletionController {

    @Autowired
    private DeletionService deletionService;
    private static final Logger logger = LoggerFactory.getLogger(DeletionController.class);

    /**
     * 查询缺失页面展示数据
     * @param deletionPageQuery
     * @return
     */
    @SystemLog(value = "缺失管理查询")
    @RequestMapping("search-deletion-list")
    public MybatisPage<DeletionInfoVO> searchDeletionList(DeletionPageQuery deletionPageQuery){
        MybatisPage<DeletionInfoVO> result = deletionService.searchDeletionList(deletionPageQuery);
        return result;
    }

    /**
     * 查询基金缺失明细
     * @param fundDeletionPageQuery
     * @return
     */
    @SystemLog(value = "查询基金缺失明细")
    @RequestMapping("search-fund-deletion")
    public MybatisPage<FundDeletionInfoVO> searchFundDeletionList(FundDeletionPageQuery fundDeletionPageQuery){
        MybatisPage<FundDeletionInfoVO> result = deletionService.searchFundDeletionList(fundDeletionPageQuery);
        return result;
    }

    /**
     * 保存缺失备注
     * @param fundDeletionInfoVOList
     * @return
     */
    @SystemLog(value = "保存缺失备注", type = SystemLog.Type.INSERT)
    @RequestMapping("save-fund-deletion")
    public ResultVo saveFundDeletionList(@RequestBody List<FundDeletionInfoVO> fundDeletionInfoVOList){
        deletionService.saveFundDeletionList(fundDeletionInfoVOList);
        return new ResultVo<>(true);
    }

    /**
     * 批量添加缺失备注
     * @param fundDeletionRemarkVO
     * @return
     */
    @SystemLog(value = "批量修改缺失备注", type = SystemLog.Type.UPDATE)
    @RequestMapping("batch-deletion-remark")
    public ResultVo saveBatchDeletionRemark(@RequestBody FundDeletionRemarkVO fundDeletionRemarkVO){
        deletionService.saveBatchDeletionRemark(fundDeletionRemarkVO);
        return new ResultVo<>(true);
    }

    /**
     * 下载数据缺失
     * @param fundDeletionListVO
     * @return
     */
    @SystemLog(value = "下载缺失数据", type = SystemLog.Type.DOWNLOAD_OR_EXPORT)
    @ExcludeGlobalResult
    @PostMapping("/download-fund-deletion")
    public void downloadFundDeletion(@RequestBody FundDeletionListVO fundDeletionListVO, HttpServletResponse response){
        List<ExcelDeletionInfoDTO> fundDeletionInfoVOList = deletionService.selectFundDeletionInfoVOList(fundDeletionListVO);
        Map<String,List<List<String>>> values = new HashMap<>();
        List<String> head = new ArrayList<>();
        head.add("基金ID");
        head.add("基金全称");
        head.add("管理人");
        head.add("缺失类型");
        head.add("缺失日期");
        head.add("缺失备注");
        String sheetName = "缺失明细";
        List<List<String>> dataList = new ArrayList<>();
        for(ExcelDeletionInfoDTO dto : fundDeletionInfoVOList){
            List<String> data = new ArrayList<>();
            data.add(dto.getFundId());
            data.add(dto.getFundName());
            data.add(dto.getCompanyName());
            data.add(dto.getDeletionType());
            data.add(dto.getDeletionDate());
            data.add(dto.getRemark());
            dataList.add(data);
        }
        values.put(sheetName,dataList);
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,head,values,null);
        try {
            response.setContentType("application/x-xls");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + EncodeUtil.encodeUTF8("缺失明细.xls"));
            ServletOutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        } finally {
            try {
                wb.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 计算缺失
     * @param deletionDownParam
     * @return
     */
    @SystemLog(value = "计算基金缺失")
    @RequestMapping("compute-deletion")
    public ResultVo computeDeletion(@RequestBody DeletionDownParam deletionDownParam){
        deletionService.computeDeletion(deletionDownParam);
        return new ResultVo<>(true);
    }
}
