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 org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.psam.bpo.SjtjBPO;
import com.zjrs.zwnw.psam.dto.PsamKglDTO;
import com.zjrs.zwnw.psam.dto.PsamXfycDTO;
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 = "卡单位信息维护")
@RestController
@RequestMapping("/api/psam/sjtj")
public class SjtjController {
    private static final Logger LOG = LoggerFactory.getLogger(SjtjController.class);
    @Resource
    private SjtjBPO sjtjBPO;

    @ApiOperation(value = "按卡状态统计")
    @PostMapping("/getKzttjList")
    public AjaxResponse getKzttjList() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            return sjtjBPO.kzttjList();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
    @ApiOperation(value = "导出按卡状态统计")
    @PostMapping("/exportKzttjList")
    public void exportKzttjList(HttpServletResponse res) {
        try {
            sjtjBPO.exportKzttjList(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @ApiOperation(value = "商户数量统计")
    @PostMapping("/getShsltjList")
    public AjaxResponse getShsltjList(@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 = sjtjBPO.getShsltjList(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;
    }

    @ApiOperation(value = "导出商户数量统计")
    @PostMapping("/exportShsltjList")
    public void exportShsltjList(@RequestBody PsamKglDTO dto,HttpServletResponse res) {
        try {
            sjtjBPO.exportShsltjList(dto,res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "按机构统计")
    @PostMapping("/getJgtjList")
    public AjaxResponse getJgtjList(@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 = sjtjBPO.getJgtjList(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;
    }

    @ApiOperation(value = "导出按机构统计")
    @PostMapping("/exportJgtjList")
    public void exportJgtjList(@RequestBody PsamKglDTO dto,HttpServletResponse res) {
        try {
            sjtjBPO.exportJgtjList(dto,res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "按商户统计")
    @PostMapping("/getShtjList")
    public AjaxResponse getShtjList(@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 = sjtjBPO.getShtjList(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;
    }

    @ApiOperation(value = "导出按商户统计")
    @PostMapping("/exportShtjList")
    public void exportShtjList(@RequestBody PsamKglDTO dto,HttpServletResponse res) {
        try {
            sjtjBPO.exportShtjList(dto,res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "按商户统计-详细信息")
    @PostMapping("/getShtjDetailList")
    public AjaxResponse getShtjDetailList(@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 = sjtjBPO.getShtjDetailList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        }catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "按日期统计")
    @PostMapping("/getRqList")
    public AjaxResponse getRqList(@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 = sjtjBPO.getRqList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "导出按日期统计")
    @PostMapping("/exportRqList")
    public void exportRqList(@RequestBody PsamKglDTO dto,HttpServletResponse res) {
        try {
            sjtjBPO.exportRqList(dto,res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "消费异常卡信息-查询列表")
    @PostMapping("/getXfyckList")
    public AjaxResponse getXfyckList(@RequestBody @Valid PsamXfycDTO 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 = sjtjBPO.getXfyckList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "查询消费明细列表")
    @PostMapping("/getXfmxList")
    public AjaxResponse getXfmxList(@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 = sjtjBPO.getXfmxList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }


    @ApiOperation(value = "详细信息")
    @PostMapping("/getJgtjDetailList")
    public AjaxResponse getJgtjDetailList(@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 = sjtjBPO.getJgtjDetailList(dto, pagenum, pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }
}
