package com.zcloud.service.hiddenDangerCheckStandard.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.hiddenDangerCheckStandard.CustomHiddenUserMapper;
import com.zcloud.service.hiddenDangerCheckStandard.CustomHiddenUserService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：隐患 相关人员信息
 * 作者：luoxiaobao
 * 时间：2022-04-22
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CustomHiddenUserServiceImpl implements CustomHiddenUserService {

	@Autowired
	private CustomHiddenUserMapper customHiddenUserMapper;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		customHiddenUserMapper.save(pd);
	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		customHiddenUserMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		customHiddenUserMapper.edit(pd);
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return customHiddenUserMapper.datalistPage(page);
	}

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd)throws Exception{
		return customHiddenUserMapper.listAll(pd);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return customHiddenUserMapper.findById(pd);
	}

	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		customHiddenUserMapper.deleteAll(ArrayDATA_IDS);
	}

	/**
	 * 添加隐患相关人员信息 -- 有主要处理人
	 * @param userIds 用户ids
	 * @param hiddenId  隐患id
	 * @param type 类别
	 * @throws Exception
	 */
	public void saveRectificationo(String userIds ,String hiddenId,String type)throws Exception{
		if(!Tools.isEmpty(hiddenId)){
			PageData del = new PageData();
			del.put("TYPE",type);
			del.put("HIDDEN_ID",hiddenId);
			customHiddenUserMapper.deleteByHidden(del); // 先删除后添加
		}
		if(!Tools.isEmpty(userIds)){
			String removeLoginUser = userIds.replaceAll(Jurisdiction.getUSER_ID(),""); //去除当前登录人的用户id
			String userID [] = removeLoginUser.split(",");
			for (int i =0;i<userID.length;i++){
				if(!Tools.isEmpty(userID[i])){
					PageData pd = new PageData();
					pd.put("HIDDENUSER_ID",  UuidUtil.get32UUID());
					pd.put("HIDDEN_ID",hiddenId);
					pd.put("USER_ID",userID[i]);
					pd.put("TYPE",type);
					pd.put("IS_MAIN","0");
					customHiddenUserMapper.save(pd);
				}
			}
		}
		PageData pd = new PageData();
		pd.put("HIDDENUSER_ID", UuidUtil.get32UUID());
		pd.put("HIDDEN_ID",hiddenId);
		pd.put("USER_ID",Jurisdiction.getUSER_ID());
		pd.put("TYPE",type);
		pd.put("IS_MAIN","1");
		customHiddenUserMapper.save(pd);
	}
	/**
	 * 根据隐患ids获取隐患相关人员
	 * @param hiddenIds
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getHiddenUserByHiddenIds (String [] hiddenIds ,String type) throws Exception{
		PageData pageData = new PageData();
		pageData.put("TYPE",type);
		if(hiddenIds.length>0){
			pageData.put("hiddenIds",hiddenIds);
		}
		return customHiddenUserMapper.listAllUser(pageData);
	}
	/**
	 * 获取隐患发现人
	 * @param varList
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getHiddebFind (List<PageData> varList) throws Exception{
		if(varList.size() == 0){
			return varList;
		}
		Map<String,Integer> mapKey = new HashMap<>();
		String [] hiddenIds = new String[varList.size()];
		for (int i =0 ;i<varList.size();i++){
			hiddenIds[i] = varList.get(i).getString("HIDDEN_ID");
			mapKey.put(varList.get(i).getString("HIDDEN_ID"),i);
		}

		List<PageData> hiddenFindUserList = this.getHiddenUserByHiddenIds(hiddenIds,"1");
		Map<String,Object> hiddenUserMap = new HashMap<>();
		for (PageData pageData : hiddenFindUserList){
			String hiddenId = pageData.getString("HIDDEN_ID");
			String userName = pageData.getString("NAME") +";";
			String deptName = pageData.getString("deptName")+";";
			if(hiddenUserMap.containsKey(hiddenId + "findUserNames")){
				userName += hiddenUserMap.get(hiddenId +"findUserNames");
				deptName += hiddenUserMap.get(hiddenId +"findDeptNames");
			}
			hiddenUserMap.put(hiddenId +"findUserNames",userName);
			hiddenUserMap.put(hiddenId +"findDeptNames",deptName);
		}
		for (PageData pageData:varList){
			String hiddenId = pageData.getString("HIDDEN_ID");
			if(hiddenUserMap.containsKey(hiddenId + "findUserNames")){
				pageData.put("findUserNames" ,hiddenUserMap.get(hiddenId + "findUserNames") );
				if(pageData.getString("findUserNames").lastIndexOf(";") == pageData.getString("findUserNames").length() - 1){ //判断最后一个的索引是否是；
					pageData.put("findUserNames" ,pageData.getString("findUserNames").substring(0,pageData.getString("findUserNames").length() - 1));
				}
			}
			if(hiddenUserMap.containsKey(hiddenId + "findDeptNames")){
				pageData.put("findDeptNames" ,hiddenUserMap.get(hiddenId + "findDeptNames") );
				if(pageData.getString("findDeptNames").lastIndexOf(";") == pageData.getString("findDeptNames").length() - 1){ //判断最后一个的索引是否是；
					pageData.put("findDeptNames" ,pageData.getString("findDeptNames").substring(0,pageData.getString("findDeptNames").length() - 1));
				}
			}
		}
		return varList;
	}

	@Override
	public List<PageData> verifylist(PageData pd) throws Exception {
		return customHiddenUserMapper.verifylist(pd);
	}

	@Override
	public void deleteByHidden(PageData pd) throws Exception {
		customHiddenUserMapper.deleteByHidden(pd);
	}
}

