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.PsamKglBPO;
import com.zjrs.zwnw.psam.dto.*;
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.validation.Valid;

/**
 * @author liangxiudong
 * @since 2021-04-07
 */
@Api(tags = "PSAM卡信息管理")
@RestController
@RequestMapping("/api/psam/kgl")
public class PsamKglController {
  private static final Logger LOG = LoggerFactory.getLogger(PsamKglController.class);
  @Resource
  private PsamKglBPO psamKglBPO;
  @ApiOperation(value = "查询列表")
  @PostMapping("/kztgl/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 = psamKglBPO.getList(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("/kztgl/saveRk")
  public AjaxResponse saveRk(@RequestBody @Valid PsamRkDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.saveRk(dto);
    }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("/kztgl/logoutKh")
  public AjaxResponse logoutKh(@RequestBody @Valid PsamZxDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.logoutKh(dto);
    }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("/kztgl/recoverKh")
  public AjaxResponse recoverKh(@RequestBody @Valid PsamZxDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.recoverKh(dto);
    } 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("/kztgl/frozenIn")
  public AjaxResponse frozenIn(@RequestBody @Valid PsamZxDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.frozenIn(dto);
    } 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("/kztgl/frozenOut")
  public AjaxResponse frozenOut(@RequestBody @Valid PsamZxDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.frozenOut(dto);
    } 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("/kztgl/saveFj")
  public AjaxResponse saveFj(@RequestBody @Valid PsamFjDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.saveFj(dto);
    } 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("/kztgl/delFj")
  public AjaxResponse delFj(@RequestBody @Valid PsamFjDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.delFj(dto);
    } 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("/kztgl/getFjList")
  public AjaxResponse getFjList(@RequestBody @Valid PsamQueryFjDTO 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 = psamKglBPO.getFjList(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) {
      LOG.error(e.getMessage());
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("系统异常");
    }
    return ajaxResponse;
  }

  @ApiOperation(value = "发放")
  @PostMapping("/kztgl/provideKh")
  public AjaxResponse provideKh(@RequestBody @Valid PsamFfDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.provideKh(dto);
    }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("/kdwwh/getList")
  public AjaxResponse kdwwhGetList(@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 = psamKglBPO.kdwwhGetList(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("/kdwwh/getInfo")
  public AjaxResponse kdwwhGetInfo(@RequestBody PsamKglDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.kdwwhGetInfo(dto);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("系统异常");
    }
    return ajaxResponse;
  }
  @ApiOperation(value = "保存")
  @PostMapping("/kdwwh/saveData")
  public AjaxResponse kdwwhSaveData(@RequestBody @Valid PsamKdwwhDTO dto, BindingResult bindingResult) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    //校验入参
    if(bindingResult.hasErrors()) {
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
      return ajaxResponse;
    }
    try {
      return psamKglBPO.kdwwhSaveData(dto);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error(e.getMessage());
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("系统异常");
    }
    return ajaxResponse;
  }
}
