package com.zjrs.zwnw.psam.bpo.impl;

import org.mohrss.leaf.core.framework.domain.bpo.impl.BPOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.jbxxgl.bpo.DwxxglBPO;
import com.zjrs.zwnw.jbxxgl.dto.DwxxDTO;
import com.zjrs.zwnw.jbxxgl.dto.JbxxglCommonDTO;
import com.zjrs.zwnw.jbxxgl.po.VSAb01PO;
import com.zjrs.zwnw.psam.blo.PsamKglBLO;
import com.zjrs.zwnw.psam.bpo.PsamKglBPO;
import com.zjrs.zwnw.psam.dto.*;
import com.zjrs.zwnw.psam.po.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 *
 * @author liangxiudong
 * @since 2021-04-07
 */
@Service
public class PsamKglBPOImpl extends BPOImpl implements PsamKglBPO {
  @Resource
  private PsamKglBLO psamKglBLO;
  @Resource
  private DwxxglBPO dwxxglBPO;

  @Override
  public PageResult getList(PsamKglDTO dto, int page, int rows) {
    return this.psamKglBLO.getList(dto, page, rows);
  }

  @Override
  @Transactional
  public AjaxResponse saveRk(PsamRkDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    String psambh=dto.getPsambh();

    CurrentUser currentUser = getCurrentUser();
    String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowDate = dateFormat.format(date);
    if(psambh.contains(",")&&psambh.contains("-")){
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("每次操作仅支持连续的卡号或不连续的卡号，不可同时录入连续的卡号和不连续的卡号");
      return ajaxResponse;
    }
    if(psambh.contains("-")){
      String[] psambhArr=psambh.split("-");
      if(psambhArr.length>2){
        throw new AppException("请按规则录入卡号，如440800100001-440800100002");
      }else{
        long beginNum;
        long endNum;
        if(psambhArr[0].trim().length()>20){
          throw new AppException("卡号过长，请按规则录入卡号，如440800100001-440800100002");
        }else if(psambhArr[1].trim().length()>20){
          throw new AppException("卡号过长，请按规则录入卡号，如440800100001-440800100002");
        }
        try{
          beginNum=Long.parseLong(psambhArr[0].trim());
          endNum=Long.parseLong(psambhArr[1].trim());
        }catch(Exception e){
          throw new AppException("请录入数字型卡号");
        }
        if(beginNum>endNum){
          throw new AppException("请按规则录入卡号，如440800100001-440800100002");
        }
        while(beginNum<=endNum){
          int cnt=psamKglBLO.getCntByPsambh(beginNum+"");
          if(cnt>0) {
            throw new AppException("PSAM卡编号 " + beginNum + " 已存在，请检查！");
          }
          String rsPsamRkxxbLsh=UUID.randomUUID().toString().replace("-", "");
          RsPsamRkxxbPO po=new RsPsamRkxxbPO();
          po.setCjr(userid);
          po.setCjsj(nowDate);
          po.setKlx(dto.getKlx());
          po.setKzt("4");
          po.setPsambh(beginNum+"");
          po.setZhxgr(userid);
          po.setZhxgsj(nowDate);
          po.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
          psamKglBLO.saveRk(po);
          beginNum++;
        }
      }
    }else if(psambh.contains(",")){
      String[] psambhArr=psambh.split(",");
      for(int i=0;i<psambhArr.length;i++){
        if(psambhArr[i].length()>20){
          throw new AppException(psambhArr[i]+" 卡号过长，请按规则录入卡号，如440800100001");
        }
        //判断是否已存在
        int cnt=psamKglBLO.getCntByPsambh(psambhArr[i].trim());
        if(cnt>0){
          Set set = new HashSet(Arrays.asList(psambhArr));
          if(set.size()<psambhArr.length){
            throw new AppException("PSAM卡编号 "+psambhArr[i].trim()+" 录入重复，请检查！");
          }else{
            throw new AppException("PSAM卡编号 "+psambhArr[i].trim()+" 已存在，请检查！");
          }
        }else{
          String rsPsamRkxxbLsh=UUID.randomUUID().toString().replace("-", "");
          try{
            //避免出现非数字型
            long bh=Long.parseLong(psambhArr[i].trim());
          }catch(Exception e){
            throw new AppException("请录入数字型卡号");
          }
          RsPsamRkxxbPO po=new RsPsamRkxxbPO();
          po.setCjr(userid);
          po.setCjsj(nowDate);
          po.setKlx(dto.getKlx());
          po.setKzt("4");
          po.setPsambh(psambhArr[i].trim());
          po.setZhxgr(userid);
          po.setZhxgsj(nowDate);
          po.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
          psamKglBLO.saveRk(po);
        }
      }
    }else{//单个卡号入库
      if(psambh.trim().length()>20){
        throw new AppException(psambh.trim()+" 卡号过长，请按规则录入卡号，如440800100001");
      }
      //判断是否已存在
      int cnt=psamKglBLO.getCntByPsambh(psambh.trim());
      if(cnt>0){
        throw new AppException("PSAM卡编号 "+psambh.trim()+" 已存在，请检查！");
      }else{
        String rsPsamRkxxbLsh=UUID.randomUUID().toString().replace("-", "");
        try{
          //避免出现非数字型
          long bh=Long.parseLong(psambh.trim());
        }catch(Exception e){
          throw new AppException("请录入数字型卡号");
        }
        RsPsamRkxxbPO po=new RsPsamRkxxbPO();
        po.setCjr(userid);
        po.setCjsj(nowDate);
        po.setKlx(dto.getKlx());
        po.setKzt("4");
        po.setPsambh(dto.getPsambh());
        po.setZhxgr(userid);
        po.setZhxgsj(nowDate);
        po.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
        psamKglBLO.saveRk(po);
      }
    }
    return ajaxResponse;
  }

  @Override
  public AjaxResponse logoutKh(PsamZxDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    String rsPsamRkxxbLsh=dto.getRsPsamRkxxbLsh();
    String[] rsPsamRkxxbLshArr=rsPsamRkxxbLsh.split(",");
    CurrentUser currentUser = getCurrentUser();
    String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowDate = dateFormat.format(date);
    String rsPsamPch=UUID.randomUUID().toString().replace("-", "");
    for(int i=0;i<rsPsamRkxxbLshArr.length;i++){
      String kzt=this.psamKglBLO.getKztByRsPsambh(null,rsPsamRkxxbLshArr[i]);
      if(kzt.equals("5")){
        String bh=this.psamKglBLO.getPsambh(rsPsamRkxxbLshArr[i]);
        throw new AppException(bh+" 请勿重复注销!");
      }
      RsPsamLogPO rsPsamLogPO=new RsPsamLogPO();
      rsPsamLogPO.setCjr(userid);
      rsPsamLogPO.setCjsj(nowDate);
      rsPsamLogPO.setCzlx("2");
      rsPsamLogPO.setCzyy(dto.getCzyy());
      rsPsamLogPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamLogPO.setKzt("5");
      rsPsamLogPO.setYkzt(kzt);
      rsPsamLogPO.setRsPsamPch(rsPsamPch);
      psamKglBLO.insertLog(rsPsamLogPO);
      //标识卡状态为注销
      RsPsamRkxxbPO rsPsamRkxxbPO=new RsPsamRkxxbPO();
      rsPsamRkxxbPO.setKzt("5");
      rsPsamRkxxbPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamRkxxbPO.setZhxgsj(nowDate);
      rsPsamRkxxbPO.setZhxgr(userid);
      this.psamKglBLO.updateRkxxb(rsPsamRkxxbPO);
      ajaxResponse.setData("rsPsamPch",rsPsamPch);
    }
    return ajaxResponse;
  }

  @Override
  public AjaxResponse recoverKh(PsamZxDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    String rsPsamRkxxbLsh=dto.getRsPsamRkxxbLsh();
    String[] rsPsamRkxxbLshArr=rsPsamRkxxbLsh.split(",");
    CurrentUser currentUser = getCurrentUser();
    String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowDate = dateFormat.format(date);
    String rsPsamPch=UUID.randomUUID().toString().replace("-", "");
    for(int i=0;i<rsPsamRkxxbLshArr.length;i++){
      String kzt=this.psamKglBLO.getKztByRsPsambh(null,rsPsamRkxxbLshArr[i]);
      if(!kzt.equals("1")){
        String bh=this.psamKglBLO.getPsambh(rsPsamRkxxbLshArr[i]);
        throw new AppException(bh+" 收回失败，只支持收回卡状态为在用的数据！");
      }
      RsPsamLogPO rsPsamLogPO=new RsPsamLogPO();
      rsPsamLogPO.setCjr(userid);
      rsPsamLogPO.setCjsj(nowDate);
      rsPsamLogPO.setCzlx("1");
      rsPsamLogPO.setCzyy(dto.getCzyy());
      rsPsamLogPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamLogPO.setKzt("4");
      rsPsamLogPO.setYkzt(kzt);
      rsPsamLogPO.setRsPsamPch(rsPsamPch);
      psamKglBLO.insertLog(rsPsamLogPO);
      //标识卡状态为空闲
      RsPsamRkxxbPO rsPsamRkxxbPO=new RsPsamRkxxbPO();
      rsPsamRkxxbPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamRkxxbPO.setKzt("4");
      rsPsamRkxxbPO.setZhxgsj(nowDate);
      rsPsamRkxxbPO.setZhxgr(userid);
      rsPsamRkxxbPO.setRsPsamFfxxbLsh("");
      rsPsamRkxxbPO.setRsPsamYwxxSqbLsh("");
      this.psamKglBLO.updateRkxxb(rsPsamRkxxbPO);
      ajaxResponse.setData("rsPsamPch",rsPsamPch);
    }
    return ajaxResponse;
  }

  @Override
  public AjaxResponse frozenIn(PsamZxDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    String rsPsamRkxxbLsh=dto.getRsPsamRkxxbLsh();
    String[] rsPsamRkxxbLshArr=rsPsamRkxxbLsh.split(",");
    CurrentUser currentUser = getCurrentUser();
    String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowDate = dateFormat.format(date);
    String rsPsamPch=UUID.randomUUID().toString().replace("-", "");
    for(int i=0;i<rsPsamRkxxbLshArr.length;i++){
      String kzt=this.psamKglBLO.getKztByRsPsambh(null,rsPsamRkxxbLshArr[i]);
      if(!kzt.equals("1")){
        String bh=this.psamKglBLO.getPsambh(rsPsamRkxxbLshArr[i]);
        throw new AppException(bh+" 冻结失败，只支持冻结卡状态为在用的数据！");
      }
      RsPsamLogPO rsPsamLogPO=new RsPsamLogPO();
      rsPsamLogPO.setCjr(userid);
      rsPsamLogPO.setCjsj(nowDate);
      rsPsamLogPO.setCzlx("3");
      rsPsamLogPO.setCzyy(dto.getCzyy());
      rsPsamLogPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamLogPO.setYkzt(kzt);
      rsPsamLogPO.setKzt("6");
      rsPsamLogPO.setRsPsamPch(rsPsamPch);
      psamKglBLO.insertLog(rsPsamLogPO);
      //标识卡状态为冻结
      RsPsamRkxxbPO rsPsamRkxxbPO=new RsPsamRkxxbPO();
      rsPsamRkxxbPO.setKzt("6");
      rsPsamRkxxbPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamRkxxbPO.setZhxgsj(nowDate);
      rsPsamRkxxbPO.setZhxgr(userid);
      this.psamKglBLO.updateRkxxb(rsPsamRkxxbPO);
      ajaxResponse.setData("rsPsamPch",rsPsamPch);
    }
    return ajaxResponse;
  }

  @Override
  public AjaxResponse frozenOut(PsamZxDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    String rsPsamRkxxbLsh=dto.getRsPsamRkxxbLsh();
    String[] rsPsamRkxxbLshArr=rsPsamRkxxbLsh.split(",");
    CurrentUser currentUser = getCurrentUser();
    String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowDate = dateFormat.format(date);
    String rsPsamPch=UUID.randomUUID().toString().replace("-", "");
    for(int i=0;i<rsPsamRkxxbLshArr.length;i++){
      String kzt=this.psamKglBLO.getKztByRsPsambh(null,rsPsamRkxxbLshArr[i]);
      if(!kzt.equals("6")){
        String bh=this.psamKglBLO.getPsambh(rsPsamRkxxbLshArr[i]);
        throw new AppException(bh+" 解冻失败，只支持解冻卡状态为冻结的数据！");
      }
      RsPsamLogPO rsPsamLogPO=new RsPsamLogPO();
      rsPsamLogPO.setCjr(userid);
      rsPsamLogPO.setCjsj(nowDate);
      rsPsamLogPO.setCzlx("4");
      rsPsamLogPO.setCzyy(dto.getCzyy());
      rsPsamLogPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamLogPO.setKzt("1");
      rsPsamLogPO.setYkzt(kzt);
      rsPsamLogPO.setRsPsamPch(rsPsamPch);
      psamKglBLO.insertLog(rsPsamLogPO);

      RsPsamRkxxbPO rsPsamRkxxbPO=new RsPsamRkxxbPO();
      rsPsamRkxxbPO.setKzt("1");
      rsPsamRkxxbPO.setRsPsamRkxxbLsh(rsPsamRkxxbLshArr[i]);
      rsPsamRkxxbPO.setZhxgsj(nowDate);
      rsPsamRkxxbPO.setZhxgr(userid);
      this.psamKglBLO.updateRkxxb(rsPsamRkxxbPO);
      ajaxResponse.setData("rsPsamPch",rsPsamPch);
    }
    return ajaxResponse;
  }

  @Override
  public AjaxResponse saveFj(PsamFjDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    RsPsamFjxxbPO rsPsamFjxxbPO=new RsPsamFjxxbPO();
    BeanUtils.copyProperties(dto, rsPsamFjxxbPO);
    rsPsamFjxxbPO.setTsYxbz("1");
    psamKglBLO.saveFj(rsPsamFjxxbPO);
    return ajaxResponse;
  }

  @Override
  public AjaxResponse delFj(PsamFjDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    RsPsamFjxxbPO rsPsamFjxxbPO=new RsPsamFjxxbPO();
    BeanUtils.copyProperties(dto, rsPsamFjxxbPO);
    rsPsamFjxxbPO.setTsYxbz("0");
    psamKglBLO.updateFj(rsPsamFjxxbPO);
    return ajaxResponse;
  }

  @Override
  public PageResult getFjList(PsamQueryFjDTO dto, int page, int rows) {
    return this.psamKglBLO.getFjList(dto, page, rows);
  }

  @Override
  @Transactional
  public AjaxResponse provideKh(PsamFfDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    RsPsamFfxxbPO rsPsamFfxxbPO=new RsPsamFfxxbPO();
    CurrentUser currentUser = getCurrentUser();
    String userid = currentUser.getUserid() == null ? "" : currentUser.getUserid().trim();
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowDate = dateFormat.format(date);
    BeanUtils.copyProperties(dto, rsPsamFfxxbPO);
    if(dto.getPsambh().contains(",")&&dto.getPsambh().contains("-")){
      ajaxResponse.setAppcode("1");
      ajaxResponse.setErrorMsg("每次操作仅支持连续的卡号或不连续的卡号，不可同时录入连续的卡号和不连续的卡号");
      return ajaxResponse;
    }
    String rsPsamPch= UUID.randomUUID().toString().replace("-", "");
    if(dto.getPsambh().contains("-")) {
      String[] psambhArr = dto.getPsambh().split("-");
      if (psambhArr.length > 2) {
        ajaxResponse.setAppcode("1");
        ajaxResponse.setErrorMsg("请按规则录入卡号，如440800100001-440800100002");
        return ajaxResponse;
      } else {
        long beginNum;
        long endNum;
        try{
          beginNum = Long.parseLong(psambhArr[0].trim());
          endNum = Long.parseLong(psambhArr[1].trim());
        }catch (Exception e){
          throw new AppException("请录入数字型卡号");
        }
        if(beginNum>endNum){
          ajaxResponse.setAppcode("1");
          ajaxResponse.setErrorMsg("请按规则录入卡号，如440800100001-440800100002");
          return ajaxResponse;
        }
        while (beginNum <= endNum) {
          String rsPsamRkxxbLsh=psamKglBLO.getRsPsamRkxxbLsh(beginNum + "");
          if(rsPsamRkxxbLsh==null){
            throw new AppException("发放失败， "+beginNum+" 未入库");
          }
          //判断卡号状态是否为空闲
          String kzt = psamKglBLO.getKztByRsPsambh(beginNum + "",null);
          if (!kzt.equals("4")) {
            throw new AppException(beginNum+" 发放失败，存在状态异常数据，请检查！");
          }
          //判断卡类型是否选择正确
          int n=psamKglBLO.getCntByKlx(beginNum+"",dto.getKlx());
          if(n==0){
            throw new AppException(beginNum+" 发放失败，卡类型与卡编号无法对应，请检查！");
          }
          //保存发放信息
          rsPsamFfxxbPO.setCjr(userid);
          rsPsamFfxxbPO.setCjsj(nowDate);
          rsPsamFfxxbPO.setPsambh(beginNum+"");
          rsPsamFfxxbPO.setRsPsamFfxxbLsh(null);
          String rsPsamFfxxbLsh=psamKglBLO.insertFfxx(rsPsamFfxxbPO);
          //保存日志
          RsPsamLogPO rsPsamLogPO=new RsPsamLogPO();
          rsPsamLogPO.setRsPsamPch(rsPsamPch);
          rsPsamLogPO.setCjr(userid);
          rsPsamLogPO.setCjsj(nowDate);
          rsPsamLogPO.setCzlx("5");
          rsPsamLogPO.setCzyy("发放");
          rsPsamLogPO.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
          rsPsamLogPO.setKzt("1");
          rsPsamLogPO.setYkzt(kzt);
          psamKglBLO.insertLog(rsPsamLogPO);
          //由“空闲”修改为“在用”
          RsPsamRkxxbPO rsPsamRkxxbPO=new RsPsamRkxxbPO();
          rsPsamRkxxbPO.setKzt("1");
          rsPsamRkxxbPO.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
          rsPsamRkxxbPO.setZhxgsj(nowDate);
          rsPsamRkxxbPO.setZhxgr(userid);
          rsPsamRkxxbPO.setRsPsamFfxxbLsh(rsPsamFfxxbLsh);
          rsPsamRkxxbPO.setRsPsamYwxxSqbLsh("");
          this.psamKglBLO.updateRkxxb(rsPsamRkxxbPO);
          beginNum++;
        }
      }
    }else if(dto.getPsambh().contains(",")) {
      String[] psambhArr=dto.getPsambh().split(",");
      for(int i=0;i<psambhArr.length;i++){
        String rsPsamRkxxbLsh=psamKglBLO.getRsPsamRkxxbLsh(psambhArr[i]);
        if(rsPsamRkxxbLsh==null){
          throw new AppException(psambhArr[i]+" 发放失败，存在状态异常数据，请检查！");
        }
        //判断卡号状态是否为空闲
        String kzt = psamKglBLO.getKztByRsPsambh(psambhArr[i],null);
        if (!kzt.equals("4")) {
          throw new AppException(psambhArr[i]+" 发放失败，存在状态异常数据，请检查！");
        }
        //判断卡类型是否选择正确
        int n=psamKglBLO.getCntByKlx(psambhArr[i],dto.getKlx());
        if(n==0){
          throw new AppException(psambhArr[i]+" 发放失败，卡类型与卡编号无法对应，请检查！");
        }
        //保存发放信息
        rsPsamFfxxbPO.setCjr(userid);
        rsPsamFfxxbPO.setCjsj(nowDate);
        rsPsamFfxxbPO.setPsambh(psambhArr[i]);
        rsPsamFfxxbPO.setRsPsamFfxxbLsh(null);
        String rsPsamFfxxbLsh=psamKglBLO.insertFfxx(rsPsamFfxxbPO);
        //保存日志
        RsPsamLogPO rsPsamLogPO=new RsPsamLogPO();
        rsPsamLogPO.setRsPsamPch(rsPsamPch);
        rsPsamLogPO.setCjr(userid);
        rsPsamLogPO.setCjsj(nowDate);
        rsPsamLogPO.setCzlx("5");
        rsPsamLogPO.setCzyy("发放");
        rsPsamLogPO.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
        rsPsamLogPO.setKzt("1");
        rsPsamLogPO.setYkzt(kzt);
        psamKglBLO.insertLog(rsPsamLogPO);
        //由“空闲”修改为“在用”
        RsPsamRkxxbPO rsPsamRkxxbPO=new RsPsamRkxxbPO();
        rsPsamRkxxbPO.setKzt("1");
        rsPsamRkxxbPO.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
        rsPsamRkxxbPO.setZhxgsj(nowDate);
        rsPsamRkxxbPO.setZhxgr(userid);
        rsPsamRkxxbPO.setRsPsamFfxxbLsh(rsPsamFfxxbLsh);
        rsPsamRkxxbPO.setRsPsamYwxxSqbLsh("");
        this.psamKglBLO.updateRkxxb(rsPsamRkxxbPO);
      }
    }else{

      String rsPsamRkxxbLsh=psamKglBLO.getRsPsamRkxxbLsh(dto.getPsambh());
      if(rsPsamRkxxbLsh==null){
        throw new AppException("发放失败， "+dto.getPsambh()+" 未入库");
      }
      //判断卡号状态是否为空闲
      String kzt = psamKglBLO.getKztByRsPsambh(dto.getPsambh(),null);
      if (!kzt.equals("4")) {
        throw new AppException("发放失败，存在状态异常数据，请检查！");
      }
      //判断卡类型是否选择正确
      int n=psamKglBLO.getCntByKlx(dto.getPsambh(),dto.getKlx());
      if(n==0){
        throw new AppException(dto.getPsambh()+" 发放失败，卡类型与卡编号无法对应，请检查！");
      }

      //保存发放信息
      rsPsamFfxxbPO.setCjr(userid);
      rsPsamFfxxbPO.setCjsj(nowDate);
      String rsPsamFfxxbLsh=psamKglBLO.insertFfxx(rsPsamFfxxbPO);
      //保存日志
      RsPsamLogPO rsPsamLogPO=new RsPsamLogPO();
      rsPsamLogPO.setRsPsamPch(rsPsamPch);
      rsPsamLogPO.setCjr(userid);
      rsPsamLogPO.setCjsj(nowDate);
      rsPsamLogPO.setCzlx("5");
      rsPsamLogPO.setCzyy("发放");
      rsPsamLogPO.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
      rsPsamLogPO.setKzt("1");
      rsPsamLogPO.setYkzt(kzt);
      psamKglBLO.insertLog(rsPsamLogPO);
      //由“空闲”修改为“在用”
      RsPsamRkxxbPO rsPsamRkxxbPO=new RsPsamRkxxbPO();
      rsPsamRkxxbPO.setKzt("1");
      rsPsamRkxxbPO.setRsPsamRkxxbLsh(rsPsamRkxxbLsh);
      rsPsamRkxxbPO.setZhxgsj(nowDate);
      rsPsamRkxxbPO.setZhxgr(userid);
      rsPsamRkxxbPO.setRsPsamFfxxbLsh(rsPsamFfxxbLsh);
      rsPsamRkxxbPO.setRsPsamYwxxSqbLsh("");
      this.psamKglBLO.updateRkxxb(rsPsamRkxxbPO);
    }
    ajaxResponse.setData("rsPsamPch",rsPsamPch);
    return ajaxResponse;
  }

  @Override
  public PageResult kdwwhGetList(PsamKglDTO dto, int page, int rows) {
    return this.psamKglBLO.kdwwhGetList(dto, page, rows);
  }

  @Override
  public AjaxResponse kdwwhGetInfo(PsamKglDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    Map<String,Object> map=this.psamKglBLO.kdwwhGetInfo(dto);
    ajaxResponse.setData("data",map);
    return ajaxResponse;
  }

  @Override
  @Transactional
  public AjaxResponse kdwwhSaveData(PsamKdwwhDTO dto) {
    AjaxResponse ajaxResponse = new AjaxResponse();
    String ztid=this.psamKglBLO.getZtid(dto.getRsPsamYwxxSqbLsh());
    RsPsamYwxxSqbPO rsPsamYwxxSqbPO=new RsPsamYwxxSqbPO();
    BeanUtils.copyProperties(dto, rsPsamYwxxSqbPO);
    this.psamKglBLO.updateYwxxSqb(rsPsamYwxxSqbPO);
    JbxxglCommonDTO jbxxglDTO=new JbxxglCommonDTO();
    jbxxglDTO.setFlag("view");
    jbxxglDTO.setId(ztid);
    VSAb01PO po=this.dwxxglBPO.getDwxx(jbxxglDTO);
    DwxxDTO dwxxDTO=new DwxxDTO();
    BeanUtils.copyProperties(po, dwxxDTO);
    dwxxDTO.setZtid(ztid);
    dwxxDTO.setAab004(dto.getDwmc());
    dwxxDTO.setAae006(dto.getDwdz());
    dwxxDTO.setAab998(dto.getCorpCode());
    dwxxDTO.setAab078(dto.getXzqh());
    this.dwxxglBPO.saveDwxx(dwxxDTO);
    return ajaxResponse;
  }
}
