package com.zcloud.service.risk.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.risk.RiskPointAnalysisMapper;
import com.zcloud.mapper.datasource.risk.RiskpointAnalysisLogMapper;
import com.zcloud.service.risk.RiskPointAnalysisService;
import com.zcloud.util.Warden;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：风险点台账
 * 作者：luoxiaobao
 * 时间：2020-12-29
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class RiskPointAnalysisServiceImpl implements RiskPointAnalysisService {

	@Resource
	private RiskPointAnalysisMapper riskpointAnalysisMapper;

	@Resource
	private RiskpointAnalysisLogMapper riskpointAnalysisLogMapper;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	@Transactional
	public void save(PageData pd)throws Exception{
		riskpointAnalysisMapper.save(pd);
		this.saveLog(pd);
	}

	private void saveLog(PageData pd){
		//1、将所有的历史数据更新
		PageData condition = new PageData();
		condition.put("HIDDEN_ID",pd.getString("HIDDEN_ID"));
		condition.put("IS_NEW","0");
		riskpointAnalysisLogMapper.updateByHiddenId(condition);
		//2、保存最新的分析数据
		PageData entity = new PageData();
		entity.putAll(pd);
		Warden.initDate(entity);
		entity.put("RISKPOINT_ANALYSIS_LOG_ID",Warden.get32UUID());
		entity.put("IS_NEW","1");
		riskpointAnalysisLogMapper.save(entity);
	}

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

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	@Transactional
	public void edit(PageData pd)throws Exception{
		riskpointAnalysisMapper.edit(pd);
		this.saveLog(pd);
	}

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

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

	/**查询所有（资源库配置使用）
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAllToRes(PageData pd)throws Exception{
		return riskpointAnalysisMapper.listAllToRes(pd);
	}
	public List<PageData> listAllByDc(PageData pd)throws Exception{
		return riskpointAnalysisMapper.listAllByDc(pd);
	}

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

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

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

	/**总数统计
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public List<PageData> statisticsAll(PageData pd)throws Exception{
		return riskpointAnalysisMapper.statisticsAll(pd);
	}
	/**
	 * 列表 搜索 风险单元辨识部位模糊查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getListForLikeName (Page page)throws Exception{
		return riskpointAnalysisMapper.datalistPageForNameLikes(page);
	}

	/**app增加位置信息
	 * @param pd
	 * @throws Exception
	 */
	public void editLonAndLat(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		riskpointAnalysisMapper.editLonAndLat(pd);
	}

	@Override
	public String countByCorpInfo(PageData pd) throws Exception {
		// TODO Auto-generated method stub
		return riskpointAnalysisMapper.countByCorpInfo(pd);
	}

	/**绩效得分
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> getDeptExamine(PageData pd) throws Exception {
		return riskpointAnalysisMapper.getDeptExamine(pd);
	}
	public List<PageData> getUserExamine(PageData pd)throws Exception {
		return riskpointAnalysisMapper.getUserExamine(pd);
	}

	public List<PageData> statisticsBI(PageData pd)throws Exception{
		return riskpointAnalysisMapper.statisticsBI(pd);
	}

	@Override
	public Map<String, Object> getLogs(PageData request, Page page) {
		page.setPd(request);
		List<PageData> list = riskpointAnalysisLogMapper.findByConditionslistPage(page);
		HashMap<String,Object> map = new HashMap<>();
		map.put("list",list);
		map.put("page",page);
		return map;
	}

	@Override
	public PageData getInfo(PageData request) throws Exception {
		PageData condition = new PageData();
		condition.put("RISKPOINT_ANALYSIS_ID",request.getString("RISKPOINT_ANALYSIS_ID"));
		return riskpointAnalysisMapper.getDto(condition);
	}
}

