package com.zcloud.service.hiddenApi.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.hiddenApi.HiddenCheckApiMapper;
import com.zcloud.mapper.datasource.hiddenApi.HiddenExamineApiMapper;
import com.zcloud.service.hiddenApi.HiddenCheckApiService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.UuidUtil;
import com.zcloud.util.message.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 说明：隐患验收人员记录
 * 作者：luoxiaobao
 * 时间：2021-01-20
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class HiddenCheckApiServiceImpl implements HiddenCheckApiService {

	@Autowired
	private HiddenCheckApiMapper hiddenCheckApiMapper;
	@Autowired
	private HiddenExamineApiMapper hiddenExamineApiMapper; // 隐患流程
	@Autowired
	private MessageService messageService;
	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		hiddenCheckApiMapper.save(pd);
	}

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

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

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

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

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



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

	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		hiddenCheckApiMapper.deleteAll(ArrayDATA_IDS);
	}
	/**
	 * 隐患验收
	 * @param checkUserId 验收人
	 * @param checkUserDeptId 验收部门
	 * @param checkOhther 其他验收人
	 * @param hiddenId
	 * @param hiddenDescr 隐患描述
	 * @return
	 * @throws Exception
	 */
	public int saveHiddenCheck(String checkUserId,String checkUserDeptId ,String checkOhther,String hiddenId,String hiddenDescr )throws Exception{
		int retint = 0;
		PageData checkUser = new PageData();
		checkUser.put("HIDDENCHECK_ID", UuidUtil.get32UUID()); // 主键
		checkUser.put("HIDDEN_ID", hiddenId);
		checkUser.put("DEPARTMENT_ID", checkUserDeptId);
		checkUser.put("USER_ID", checkUserId);
		checkUser.put("STATUS", "0");
		retint += hiddenCheckApiMapper.save(checkUser);
		Map<String,Object> notRepr = new HashMap<>();
		notRepr.put("hiddendescr",hiddenDescr);//
		notRepr.put("params2", "隐患验收");//
		notRepr.put("params3", "验收");//
		messageService.sendMessageByParameter("HIDDEN_MAIL_004","友情提示：有一条隐患需要验收",
				Jurisdiction.getCORPINFO_ID(),checkUserId,hiddenId,
				hiddenDescr,"隐患验收","验收","","");

		if (checkOhther != null && !checkOhther.equals("") && !checkOhther.equals("[]")) {
			List<JSONObject> list = (List<JSONObject>) JSON.parse(checkOhther);
			for (JSONObject json : list) {
				PageData other = new PageData();
				other.put("HIDDENCHECK_ID", UuidUtil.get32UUID()); // 主键
				other.put("HIDDEN_ID", hiddenId);
				other.put("DEPARTMENT_ID", json.get("DEPARTMENT_ID"));
				other.put("USER_ID", json.get("USER_ID"));
				other.put("STATUS", "0");
				retint += hiddenCheckApiMapper.save(other);
				messageService.sendMessageByParameter("HIDDEN_MAIL_004","友情提示：有一条隐患需要验收",
						Jurisdiction.getCORPINFO_ID(),json.get("USER_ID").toString(),hiddenId,
						hiddenDescr,"隐患验收","验收","","");
			}
		}
		return retint;
	}

	/**通过id获取数据 验收信息
	 * @param hiddenId 隐患id
	 * @throws Exception
	 */
	public List<PageData> findByHidden(String hiddenId)throws Exception{
		return hiddenCheckApiMapper.findByHidden(hiddenId);
	}

	/**
	 * 验收隐患
	 *
	 * @param
	 * @throws Exception
	 */
	public int checkHidden(String HIDDEN_ID, String HIDDENCHECK_ID,String CHECKDESCR, String CHECK_TIME,
		String DEPARTMENT_ID, String USER_ID) throws Exception {
		PageData pd = new PageData();
		pd.put("HIDDENCHECK_ID",HIDDENCHECK_ID);
		pd.put("HIDDEN_ID",HIDDEN_ID); //隐患ID
		pd.put("DEPARTMENT_ID",DEPARTMENT_ID);//部门
		pd.put("USER_ID",USER_ID);//人员
		pd.put("STATUS","1");//是否合格
		pd.put("CHECK_TIME",CHECK_TIME);//验收时间
		pd.put("CHECKDESCR",CHECKDESCR);//验收描述

		PageData examPd = new PageData();

		examPd.put("HIDDENEXAMINE_ID", UuidUtil.get32UUID());
		examPd.put("CREATOR", Jurisdiction.getUSER_ID());
		examPd.put("CREATTIME", DateUtil.date2Str(new Date()));
		examPd.put("OPERATOR",Jurisdiction.getUSER_ID());
		examPd.put("HIDDEN_ID",HIDDEN_ID);
		examPd.put("OPERATTIME",DateUtil.date2Str(new Date()));
		examPd.put("spare1",CHECKDESCR);
		examPd.put("spare2",CHECK_TIME);
		examPd.put("TYPE","8");
		hiddenExamineApiMapper.save(examPd);
		return hiddenCheckApiMapper.checkHidden(pd);
	}
	/**根据隐患信息删除隐患验收人。
	 * @param hiddenId
	 * @throws Exception
	 */
	public int deleteByHiddenId(String hiddenId)throws Exception{
		return hiddenCheckApiMapper.deleteByHiddenId(hiddenId);
	}

	/**
	 * 获取隐患发现人
	 * @param hiddenIds
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getListByHiddenIds (String [] hiddenIds) throws Exception{
		PageData pd = new PageData();
		pd.put("HIDDEN_ID",hiddenIds);
		pd.put("ArrayDATA_IDS",hiddenIds);
		return hiddenCheckApiMapper.listAllByIds(pd);
	}

}

