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

import org.apache.commons.lang.StringUtils;
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.util.SecurityUtils;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.zwnw.sxgl.blo.HcpglBLO;
import com.zjrs.zwnw.sxgl.bpo.HcpglBPO;
import com.zjrs.zwnw.sxgl.dto.*;
import com.zjrs.zwnw.sxgl.po.RsJkHcpPO;
import com.zjrs.zwnw.sxgl.po.RsKsjgDybPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HcpglBPOImpl extends BPOImpl implements HcpglBPO {
	
	@Resource
    private HcpglBLO hcpglBLO;
	
	@Override
	public AjaxResponse getHcpList(HcpQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//查询列表数据
		if(StringUtils.isBlank(dto.getRsJkHcpLsh())) {
			//实施部门名称
			if(StringUtils.isBlank(dto.getSsbmmc())) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("实施部门名称不能为空！");
				return ajaxResponse;
			}
			//管理员级别
			if(StringUtils.isBlank(dto.getGlyjb())) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("管理员级别不能为空！");
				return ajaxResponse;
			}
			//若主办科室为空，则校验是否管理员
			if(StringUtils.isBlank(dto.getSxlb())) {
				//管理员权限，查询所在区所有主办科室事项
				if("1".equals(dto.getGlyjb()) || "2".equals(dto.getGlyjb())) {
					dto.setSxlb(dto.getSsbmmc());
				}else {
					ajaxResponse.setAppcode("1");
					ajaxResponse.setErrorMsg("主办科室不能为空！");
					return ajaxResponse;
				}
			}
		}
		PageResult pageResult = hcpglBLO.getHcpList(dto);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", dto.getPagesize());
        ajaxResponse.setData("pagenum", dto.getPagenum());
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse editHcp(HcpEditDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	String czsj = LocalDateTime.now().format(formatters);//获取当前时间
    	CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
    	
    	String[] rsJkHcpLsh = dto.getRsJkHcpLsh().split(",");
    	for(int i=0;i<rsJkHcpLsh.length;i++) {
    		RsJkHcpPO rsJkHcp = new RsJkHcpPO();
    		BeanUtils.copyProperties(dto, rsJkHcp);
    		rsJkHcp.setRsJkHcpLsh(rsJkHcpLsh[i]);
    		rsJkHcp.setTszt("1");//待推送
    		rsJkHcp.setXgpjr(cUser.getUserid());//经办人
    		rsJkHcp.setXgpjsj(czsj);//评价时间
    		hcpglBLO.updateRsJkHcp(rsJkHcp);
    	}
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getZbks(ZbksQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
		List<RsKsjgDybPO> zbksList = hcpglBLO.getZbks(cUser.getOrgid(),"");
		if(zbksList != null && zbksList.size() >0) {
			//管理员可查询全部科室
			String glyjb = zbksList.get(0).getGlyjb();
			if("1".equals(glyjb) || "2".equals(glyjb)) {
				zbksList = hcpglBLO.getZbks(cUser.getOrgid(),dto.getSsbmmc());
			}
			ajaxResponse.setData("glyjb", glyjb);
			
			//获取字典项
			ArrayList<Map<String, Object>> keyList = new ArrayList<>();
			for(RsKsjgDybPO rsKsjgDybPO : zbksList) {
				Map<String, Object> keyVlueMap = new HashMap<>();
				keyVlueMap.put("code", rsKsjgDybPO.getZbksdm());
                keyVlueMap.put("detail", rsKsjgDybPO.getZbks());
                keyList.add(keyVlueMap);
			}
			ajaxResponse.setData("zbks", keyList);
		}
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getSlxx(HcpCkQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		HcpCkDTO hcpCkDTO = hcpglBLO.getSlxx(dto);
		ajaxResponse.setData("data", hcpCkDTO);
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveCkHcp(HcpCkDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	String czsj = LocalDateTime.now().format(formatters);//获取当前时间
    	
    	String[] pjjd = dto.getEvalprocess().split(",");
    	for(int i=0;i<pjjd.length;i++) {
    		RsJkHcpPO vRsJkHcp = new RsJkHcpPO();
    		BeanUtils.copyProperties(dto, vRsJkHcp);
    		vRsJkHcp.setEvalprocess(pjjd[i]);
    		vRsJkHcp.setPjlx("1");//主动评价
    		vRsJkHcp.setTszt("1");//待推送
    		vRsJkHcp.setYhpjsj(czsj);//评价时间
    		hcpglBLO.saveRsJkHcp(vRsJkHcp);
    	}
    	return ajaxResponse;
	}
}
