package com.zcloud.service.hiddenApi.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.hiddenApi.HiddenUserApiMapper;
import com.zcloud.service.hiddenApi.HiddenUserApiService;
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.List;

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

	@Autowired
	private HiddenUserApiMapper hiddenUserApiMapper;

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

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

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

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

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

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

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

	/**
	 * 根基隐患ID删除隐患发现人
	 * @param hiddenId
	 * @return
	 * @throws Exception
	 */
	public Integer deleteByHidden(String hiddenId)throws Exception{
		return  hiddenUserApiMapper.deleteByHidden(hiddenId);
	}
	/**
	 * 添加隐患相关人员信息 -- 有主要处理人
	 * @param findCreator 用户ids
	 * @param hiddenId  隐患id
	 * @param type 类别
	 * @param loginUserId 登录人的ID
	 * @throws Exception
	 */
	public void saveRectificationo(String findCreator ,String hiddenId,String type,String loginUserId)throws Exception{
		if(!Tools.isEmpty(hiddenId)){
			hiddenUserApiMapper.deleteByHidden(hiddenId); // 先删除后添加
		}
		if(!Tools.isEmpty(findCreator)){
			if(!Tools.isEmpty(findCreator)){
				String removeLoginUser = findCreator.replaceAll(loginUserId,""); //去除当前登录人的用户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");
						hiddenUserApiMapper.save(pd);
					}
				}
			}
		}
		PageData pd = new PageData();
		pd.put("HIDDENUSER_ID", UuidUtil.get32UUID());
		pd.put("HIDDEN_ID",hiddenId);
		pd.put("USER_ID",loginUserId);
		pd.put("TYPE",type);
		pd.put("IS_MAIN","1");
		hiddenUserApiMapper.save(pd);
	}

	/**
	 * 根据隐患id获取隐患发现人
	 * @param hiddenId
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getListByHiddenId (String hiddenId) throws Exception{
		return hiddenUserApiMapper.getListByHidden(hiddenId);
	}
	/**
	 * 根据隐患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 hiddenUserApiMapper.getListByHiddenIds(pageData);
	}
	/**
	 * 获取隐患发现人
	 * @param
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getListByHiddenIds (String [] hiddenIds) throws Exception{
		List<PageData> hiddenFindUserList = this.getHiddenUserByHiddenIds(hiddenIds,"1");
		return hiddenFindUserList;
	}

}

