package com.chinaweal.aiccs.aiccs.fraudreg.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSWritService;
import com.chinaweal.aiccs.aiccs.fraudreg.dto.RevokeFraudBatchDto;
import com.chinaweal.aiccs.aiccs.fraudreg.entity.FraudBatchBaseinfo;
import com.chinaweal.aiccs.aiccs.fraudreg.entity.RevokeFraudBatch;
import com.chinaweal.aiccs.aiccs.fraudreg.service.RevokeFraudBatchService;
import com.chinaweal.aiccs.aiccs.revoke.Vo.DisableListVo;
import com.chinaweal.aiccs.common.base.controller.BaseController;
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.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sh
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/revokeFraudBatch")
public class RevokeFraudBatchController extends BaseController {

    @Autowired
    private RevokeFraudBatchService revokeFraudBatchService;
    @Autowired
    private TSWritService tsWritService;

    /**
     * 启动批量撤销登记流程待办
     * @author sh
     * @since 2023-03-14
     */
    @PostMapping("/startTaskRevokeTFraud")
    public RestResult<?> startTaskRevokeFraudBatch(@RequestParam String pripids, HttpServletRequest request) {
        return RestResult.ok(revokeFraudBatchService.startTaskRevokeFraudBatch(pripids));
    }

    /**
     * 获取批量撤销业务相关业务信息
     * @since 2023-03-14
     */
    @GetMapping("/getCxBusFraudInfo")
    public RestResult<Map<String, Object>> getRevokeFraudBatch(@RequestParam String bizseq,
                                                             @RequestParam String workflowId) {
        AssertUtils.isNotBlank(bizseq, workflowId);
        Map<String, Object> result = revokeFraudBatchService.getRevokeFraudBatch(bizseq, workflowId);
        return RestResult.ok(result);
    }

    /**
     * 批量撤销撤销案件流程控制
     * @since 2023-03-14
     */
    @PostMapping("/processControlFraudBatch")
    public RestResult<?> processControlFraudBatch(@RequestBody @Validated RevokeFraudBatchDto revokeFraudBatchDto, BindingResult bindingResult, HttpServletRequest request) {
        AssertUtils.isBndingValidit(bindingResult);
        revokeFraudBatchService.processControlFraudBatch(revokeFraudBatchDto);
        return RestResult.ok();
    }

    @PostMapping("/saveContent")
    public RestResult<?> saveContent(@RequestBody RevokeFraudBatch revokeFraudBatch) {
        revokeFraudBatchService.saveContent(revokeFraudBatch);
        return RestResult.ok();
    }

    @PostMapping("createDocument")
    public RestResult<?> createDocument(String bizseq, String contentType) {
        return revokeFraudBatchService.createDocument(bizseq, contentType);
    }

    @PostMapping("queryTSWrits")
    public RestResult<?> queryTSWrits(@RequestBody PageRequestDto pageRequestDto) {
        return RestResult.ok(tsWritService.queryByEntParam(pageRequestDto));
    }

    @PostMapping("queryRevokeFraudBatchInfo")
    public RestResult<?> queryRevokeFraudBatchInfo(String bizseq) {
        return revokeFraudBatchService.queryRevokeFraudBatchInfo(bizseq);
    }

    @RequestMapping("importExcel")
    public RestResult<?> importExcel(HttpServletRequest request, MultipartFile file) {
        AssertUtils.isNotNull(file);
        return revokeFraudBatchService.importExcel(request ,file);
    }

    @RequestMapping(value = "exportFailExcel")
    public void exportFailExcel(@RequestBody List<DisableListVo> disableListVoList, HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream=null;
        try {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String("主体名单.xls".getBytes(), "UTF-8"));
            response.setContentType("application/msexcel;charset=UTF-8");
            outputStream = response.getOutputStream();
            ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
            WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "sheet1").head(DisableListVo.class).build();
            excelWriter.write(disableListVoList, writeSheet1);
            excelWriter.finish();
        } catch (IOException e) {
            outputStream.close();
            e.printStackTrace();
        } finally {
            outputStream.close();
        }
    }

    @PostMapping("queryEntlockAbnInfo")
    public RestResult<?> queryEntlockAbnInfo(String pripid) {
        return revokeFraudBatchService.queryEntlockAbnInfo(pripid);
    }

    @PostMapping("getBasePage")
    public RestResult<?> getBasePage(@RequestBody PageRequestDto pageRequestDto,HttpServletRequest request){
        return revokeFraudBatchService.getBasePage(pageRequestDto, request);
    }
}

