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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import com.zjrs.zwnw.psam.blo.PsamKglBLO;
import com.zjrs.zwnw.psam.dto.PsamKglDTO;
import com.zjrs.zwnw.psam.dto.PsamQueryFjDTO;
import com.zjrs.zwnw.psam.entity.*;
import com.zjrs.zwnw.psam.po.*;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Map;
import java.util.UUID;

/**
 *
 * @author liangxiudong
 * @since 2021-04-07
 */
@Service
public class PsamKglBLOImpl extends BLOImpl implements PsamKglBLO {
  @Resource
  private RsPsamFfxxbMapper rsPsamFfxxbMapper;
  @Resource
  private RsPsamRkxxbMapper rsPsamRkxxbMapper;
  @Resource
  private RsPsamLogMapper rsPsamLogMapper;
  @Resource
  private RsPsamFjxxbMapper rsPsamFjxxbMapper;
  @Resource
  private RsPsamYwxxSqbMapper rsPsamYwxxSqbMapper;


  @Override
  public PageResult getList(PsamKglDTO dto, int page, int rows) {
    Page<Map<String,Object>> page1 = new Page<>(page, rows);
    Page<Map<String,Object>> pageResult = (Page) rsPsamFfxxbMapper.getList(page1, dto);
    PageResult result = PageUtil.PageResult(pageResult);
    return result;
  }

  @Override
  public void saveRk(RsPsamRkxxbPO rsPsamRkxxbPO) {
    rsPsamRkxxbMapper.insert(rsPsamRkxxbPO);
  }

  @Override
  public int getCntByPsambh(String psambh) {
    QueryWrapper<RsPsamRkxxbPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("psambh", psambh);
    int cnt = rsPsamRkxxbMapper.selectCount(queryWrapper);
    return cnt;
  }

  @Override
  public void insertLog(RsPsamLogPO rsPsamLogPO) {
    QueryWrapper<RsPsamRkxxbPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.select("kzt");
    queryWrapper.eq("RS_PSAM_RKXXB", rsPsamLogPO.getRsPsamRkxxbLsh());
    String kzt=rsPsamRkxxbMapper.selectOne(queryWrapper).getKzt();
    rsPsamLogPO.setYkzt(kzt);

    rsPsamLogMapper.insert(rsPsamLogPO);
  }

  @Override
  public void saveFj(RsPsamFjxxbPO rsPsamFjxxbPO) {
    rsPsamFjxxbMapper.insert(rsPsamFjxxbPO);
  }

  @Override
  public void updateFj(RsPsamFjxxbPO rsPsamFjxxbPO) {
    UpdateWrapper<RsPsamFjxxbPO> updateWrapper=new UpdateWrapper<>();
    updateWrapper.eq("RS_PSAM_PCH",rsPsamFjxxbPO.getRsPsamPch());
    updateWrapper.eq("TYCCID",rsPsamFjxxbPO.getTyccid());
    rsPsamFjxxbMapper.update(rsPsamFjxxbPO,updateWrapper);
  }

  @Override
  public PageResult getFjList(PsamQueryFjDTO dto, int page, int rows) {
    Page<Map<String,Object>> page1 = new Page<>(page, rows);
    Page<Map<String,Object>> pageResult = (Page) rsPsamFjxxbMapper.getFjList(page1, dto);
    return PageUtil.PageResult(pageResult);
  }

  @Override
  public String insertFfxx(RsPsamFfxxbPO rsPsamFfxxbPO) {
    String rsPsamFfxxbLsh= UUID.randomUUID().toString().replace("-", "");
    rsPsamFfxxbPO.setRsPsamFfxxbLsh(rsPsamFfxxbLsh);
    rsPsamFfxxbMapper.insert(rsPsamFfxxbPO);
    return rsPsamFfxxbLsh;
  }

  @Override
  public String getKztByRsPsambh(String psambh,String rsPsamRkxxbLsh) {
    QueryWrapper<RsPsamRkxxbPO> queryWrapper = new QueryWrapper<>();
    if(psambh!=null){
      queryWrapper.eq("psambh", psambh);
    }
    if(rsPsamRkxxbLsh!=null){
      queryWrapper.eq("rs_psam_rkxxb", rsPsamRkxxbLsh);
    }
    int cnt=rsPsamRkxxbMapper.selectCount(queryWrapper);
    if(cnt>0){
      queryWrapper.select("kzt");
      return rsPsamRkxxbMapper.selectOne(queryWrapper).getKzt();
    }
    return null;
  }

  @Override
  public String getRsPsamRkxxbLsh(String psambh) {
    QueryWrapper<RsPsamRkxxbPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("psambh", psambh);

    int cnt=rsPsamRkxxbMapper.selectCount(queryWrapper);
    if(cnt>0){
      queryWrapper.select("RS_PSAM_RKXXB");
      return rsPsamRkxxbMapper.selectOne(queryWrapper).getRsPsamRkxxbLsh();
    }
    return null;
  }

  @Override
  public String getPsambh(String rsPsamRkxxbLsh) {
    QueryWrapper<RsPsamRkxxbPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("RS_PSAM_RKXXB", rsPsamRkxxbLsh);

    int cnt=rsPsamRkxxbMapper.selectCount(queryWrapper);
    if(cnt>0){
      queryWrapper.select("psambh");
      return rsPsamRkxxbMapper.selectOne(queryWrapper).getPsambh();
    }
    return null;
  }

  @Override
  public void updateRkxxb(RsPsamRkxxbPO rsPsamRkxxbPO) {
    rsPsamRkxxbMapper.updateById(rsPsamRkxxbPO);
  }

  @Override
  public PageResult kdwwhGetList(PsamKglDTO dto, int page, int rows) {
    Page<Map<String,Object>> page1 = new Page<>(page, rows);
    Page<Map<String,Object>> pageResult = (Page) rsPsamFfxxbMapper.kdwwhGetList(page1, dto);
    PageResult result = PageUtil.PageResult(pageResult);
    return result;
  }

  @Override
  public Map<String, Object> kdwwhGetInfo(PsamKglDTO dto) {
    return rsPsamFfxxbMapper.kdwwhGetInfo(dto);
  }

  @Override
  public String getZtid(String rsPsamYwxxSqbLsh) {
    QueryWrapper<RsPsamYwxxSqbPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("rs_psam_ywxx_sqb", rsPsamYwxxSqbLsh);
    return rsPsamYwxxSqbMapper.selectOne(queryWrapper).getZtid();
  }

  @Override
  public void updateYwxxSqb(RsPsamYwxxSqbPO rsPsamYwxxSqbPO) {
    rsPsamYwxxSqbMapper.updateById(rsPsamYwxxSqbPO);
  }

  @Override
  public int getCntByKlx(String psambh,String klx) {
    QueryWrapper<RsPsamRkxxbPO> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("psambh", psambh);
    return rsPsamRkxxbMapper.selectCount(queryWrapper);
  }
}
