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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
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.sxgl.blo.HcpglBLO;
import com.zjrs.zwnw.sxgl.dto.HcpCkDTO;
import com.zjrs.zwnw.sxgl.dto.HcpCkQueryDTO;
import com.zjrs.zwnw.sxgl.dto.HcpDTO;
import com.zjrs.zwnw.sxgl.dto.HcpQueryDTO;
import com.zjrs.zwnw.sxgl.entity.RsJkHcpMapper;
import com.zjrs.zwnw.sxgl.entity.RsKsjgDybMapper;
import com.zjrs.zwnw.sxgl.po.RsJkHcpPO;
import com.zjrs.zwnw.sxgl.po.RsKsjgDybPO;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;

@Service
public class HcpglBLOImpl extends BLOImpl implements HcpglBLO {
	
	@Resource
	RsJkHcpMapper rsJkHcpMapper;
	
	@Resource
	RsKsjgDybMapper rsKsjgDybMapper;
	
	@Override
	public PageResult getHcpList(HcpQueryDTO dto) {
		Page<HcpDTO> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<HcpDTO> pageResult = (Page) this.rsJkHcpMapper.getHcpList(page, dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
	}
	
	@Override
	public void updateRsJkHcp(RsJkHcpPO rsJkHcpPO) {
		rsJkHcpMapper.updateById(rsJkHcpPO);
	}
	
	@Override
	public List<RsKsjgDybPO> getZbks(String orgId, String ssbmmc){
		QueryWrapper<RsKsjgDybPO> queryWrapper = new QueryWrapper<RsKsjgDybPO>();
		if(StringUtils.isNotBlank(ssbmmc)) {
			queryWrapper.ne("zbksdm", ssbmmc);
			queryWrapper.likeRight("zbksdm", ssbmmc);
		}
		queryWrapper.orderByAsc("zbksdm");
		return rsKsjgDybMapper.selectList(queryWrapper);
	}
	
	@Override
	public HcpCkDTO getSlxx(HcpCkQueryDTO dto) {
		HcpCkDTO hcpCkDTO = rsJkHcpMapper.getSlxx(dto);
		if(hcpCkDTO != null) {
			//查询是否已评价
			QueryWrapper<RsJkHcpPO> queryWrapper = new QueryWrapper<RsJkHcpPO>();
			queryWrapper.eq("sllsh", dto.getSllsh());
			
			int cnt = rsJkHcpMapper.selectCount(queryWrapper);
			//已办结
			if("2".equals(hcpCkDTO.getEvalprocess())) {
				if(cnt == 0) {//未评价
					hcpCkDTO.setEvalprocess("1,2");
				}else if(cnt > 1) {//已评价
					hcpCkDTO.setEvalprocess("");
				}
			}else {
				if(cnt > 0) {//已评价
					hcpCkDTO.setEvalprocess("");
				}
			}
		}
		return hcpCkDTO;
	}
	
	@Override
	public void saveRsJkHcp(RsJkHcpPO rsJkHcpPO) {
		rsJkHcpMapper.insert(rsJkHcpPO);
	}
}
