package com.zcloud.service.majordangersource.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.majordangersource.MajorDangerSourceMapper;
import com.zcloud.service.majordangersource.MajorDangerSourceService;
import com.zcloud.service.majordangersource.MajordangersourceStoragetankService;
import com.zcloud.service.majordangersource.MonitoringDeviceSensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 说明：重大危险源
 * 作者：luoxiaobao
 * 时间：2022-03-22
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class MajorDangerSourceServiceImpl implements MajorDangerSourceService{

	@Autowired
	private MajorDangerSourceMapper majordangersourceMapper;

	@Autowired
	private MonitoringDeviceSensorService monitoringdevicesensorService;
	@Autowired
	private MajordangersourceStoragetankService majordangersourceStoragetankService;

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

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

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

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

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

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

	/**批量删除
	 * @param pd
	 * @throws Exception
	 */
	public void deleteAll(PageData pd)throws Exception{
		majordangersourceMapper.deleteAll(pd);
	}

	/**列表(根据所选ID获取数据)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> findByIds(PageData pd) throws Exception {
		return majordangersourceMapper.findByIds(pd);
	}

	@Override
	public PageData showById(PageData pd) throws Exception {
		PageData data = majordangersourceMapper.findById(pd);
		data.put("SENSORLIST", monitoringdevicesensorService.listAll(pd));
		return data;
	}

	@Override
	public List<PageData> findRealTimeMonitoringListAll(PageData pd) throws Exception {
		List<PageData> pdlist = majordangersourceMapper.findRealTimeMonitoringListAll(pd);
		List<PageData> data =  majordangersourceStoragetankService.listAll(new PageData());
		if (data.size()>0) {
			for (int i = 0; i < pdlist.size(); i++) {
				if (i<9) {
					pdlist.get(i).put("STORAGETANK_LIQUIDLEVEL", data.get(0).getString("STORAGETANK" + (i + 1) + "_LIQUIDLEVEL"));
					pdlist.get(i).put("STORAGETANK_TEMPERATURE", data.get(0).getString("STORAGETANK" + (i + 1) + "_TEMPERATURE"));
				}
			}
		}
		return pdlist;
	}

	@Override
	public PageData findRealtimeInfo(PageData pd) throws Exception {
		PageData source = majordangersourceMapper.findById(pd);
		List<PageData> data =  majordangersourceStoragetankService.listAll(new PageData());
		if (data.size()>0) {
			source.put("STORAGETANK_LIQUIDLEVEL", data.get(0).getString("STORAGETANK" + source.get("MAJORDANGERSOURCE_STORAGETANK").toString() + "_LIQUIDLEVEL"));
			source.put("STORAGETANK_TEMPERATURE", data.get(0).getString("STORAGETANK" + source.get("MAJORDANGERSOURCE_STORAGETANK").toString() + "_TEMPERATURE"));
		}
		return source;
	}

	@Override
	public PageData statisticsMajorDangerSource(PageData pd) throws Exception {
		return majordangersourceMapper.statisticsMajorDangerSource(pd);
	}

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

