package com.zjrs.zwnw.psam.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.psam.bpo.PsamDcBPO;
import com.zjrs.zwnw.psam.dto.PsamKglDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
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 jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;

/**
 * @author liangxiudong
 * @since 2021-04-07
 */
@Api(tags = "PSAM卡信息导出")
@RestController
@RequestMapping("/api/psam/dc")
public class PsamDcController {
  private static final Logger LOG = LoggerFactory.getLogger(PsamDcController.class);
  @Resource
  private PsamDcBPO psamDcBPO;

  @ApiOperation(value = "导出PSAM卡信息-txt格式",produces = "application/octet-stream")
  @PostMapping(value = "/exportTxt")
  public void exportTxt(@RequestBody PsamKglDTO dto, HttpServletResponse res){
    try {
      psamDcBPO.exportTxt(dto,res);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @ApiOperation(value = "导出PSAM卡信息-Excel",produces = "application/octet-stream")
  @PostMapping(value = "/exportExcel")
  public void exportExcel(@RequestBody PsamKglDTO dto, HttpServletResponse res){
    try {
      psamDcBPO.exportExcel(dto,res);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @ApiOperation(value = "查询列表")
  @PostMapping("/getList")
  public AjaxResponse getList(@RequestBody @Valid PsamKglDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    Integer pagenum = dto.getPagenum();
    Integer pagesize = dto.getPagesize();
    try {
      PageResult pageResult = psamDcBPO.getList(dto, pagenum, pagesize);
      ajaxResponse.setData("data", pageResult);
      ajaxResponse.setData("pagesize", pagesize);
      ajaxResponse.setData("pagenum", pagenum);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("系统异常");
    }
    return ajaxResponse;
  }
}
