package com.chinaweal.aiccs.aiccs.abnormal.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinaweal.aiccs.aiccs.abnormal.entity.dto.AbnStatDto;
import com.chinaweal.aiccs.aiccs.abnormal.entity.dto.AbnStatisticsDto;
import com.chinaweal.aiccs.aiccs.abnormal.entity.excel.StatisticsAbnExcelDto;
import com.chinaweal.aiccs.aiccs.abnormal.service.AbnStatisticsService;
import com.chinaweal.aiccs.aiccs.abnormal.service.impl.EBaseinfoServiceImpl;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.TSSerIllegalList;
import com.chinaweal.aiccs.aiccs.seriousillegal.excelDto.IllegalExcelDto;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.function.CustomWrapperPackingFunction;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 经营异常目录统计表
 * </p>
 *
 * @author sh
 * @since 2021-12-21
 */
@Api(tags = "经营异常目录统计相关接口")
@RestController
@RequestMapping("/abnStatistics")
@Slf4j
public class AbnStatisticsController extends BaseController {

    @Autowired
    private AbnStatisticsService abnStatisticsService;

    @RequestMapping("queryStatistics")
    public RestResult<?> queryStatistics(@RequestBody Map<String, Object> customParamMap, HttpServletRequest request) {
        if(customParamMap == null) customParamMap = new HashMap<>();
        AICUser aicUser = getLoginUser(request);
        Map<String,Object> rs = abnStatisticsService.queryStatAll(customParamMap, aicUser);
        return RestResult.ok(rs);
    }

    @RequestMapping("StatisticsForAbn")
    public RestResult<?> StatisticsForAbn(@RequestBody Map<String, Object> customParamMap) {
        if(customParamMap == null) customParamMap = new HashMap<>();
        Map<String, Object> rs = abnStatisticsService.StatisticsForAbn(customParamMap);
        return RestResult.ok(rs);
    }

    @ApiOperation(value = "经营异常主体信息列表")
    @PostMapping("getEntListByStatistics")
    public RestResult<?> getEntListByStatistics(@RequestBody PageRequestDto pageRequestDto) {
        AssertUtils.isNotNull(pageRequestDto);
        IPage<StatisticsAbnExcelDto> data = abnStatisticsService.getEntListByStatistics(pageRequestDto);
        return RestResult.ok(data);
    }

    /**
     * 经营异常主体信息列表导出
     * @param pageRequestDto
     * @param response
     * @return
     */
    @PostMapping("/exportEntListByStatistics")
    public void exportEntListByStatistics(@RequestBody PageRequestDto pageRequestDto, HttpServletResponse response) throws IOException {
        AssertUtils.isNotNull(pageRequestDto);

        IPage<StatisticsAbnExcelDto> data = abnStatisticsService.getEntListByStatistics(pageRequestDto);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("经营异常主体信息列表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        ExcelWriter excelWriter = null;
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            excelWriter = EasyExcel.write(bos, StatisticsAbnExcelDto.class).inMemory(true).build();
            excelWriter.write(data.getRecords(), EasyExcel.writerSheet("经营异常主体信息列表").registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build());
            // 结束
            excelWriter.finish();
            response.getOutputStream().write(bos.toByteArray());
        } finally {
            // 结束
            if (excelWriter != null) excelWriter.finish();
        }
    }
}

