package com.simuwang.manage.api.deletion;

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.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.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}
 */
@RestController
@RequestMapping("/v1/deletion")
public class DeletionController {

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

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

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

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

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

    /**
     * 下载数据缺失
     * @param fundIdListVO
     * @return
     */
    @PostMapping("/download-fund-deletion")
    public void downloadFundDeletion(@RequestBody FundIdListVO fundIdListVO, HttpServletResponse response, HttpServletRequest request){
        List<ExcelDeletionInfoDTO> fundDeletionInfoVOList = deletionService.selectFundDeletionInfoVOList(fundIdListVO);
        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/octet-stream");
            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);
        }
    }
}
