package com.unis.detectionnum.server.domain;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unis.common.utils.DateUtil;
import com.unis.common.utils.LsBeanUtils;
import com.unis.detectionnum.common.vo.*;
import com.unis.detectionnum.server.dao.*;
import com.unis.detectionnum.server.entity.*;
import com.unis.detectionnum.server.schedule.job.DetectionNumNewJob;
import com.unis.detectionnum.server.service.DCfxxsjyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class DeviceParamDomain extends BaseDomain{

	@Autowired
    private DeviceParamDao deviceParamDao;
	
	@Autowired
	private DeviceInfoDao deviceInfoDao;
	
	@Autowired
	private ProbeParamDao probeParamDao;
	
	@Autowired
	private CameraParamDao cameraParamDao;
	
	@Autowired
	private DCfxxsjyService cfxxsjyService;

	@Autowired
	private ScanRuleDomain scanRuleDomain;
	
	@Autowired
	private TaskInfoDao taskInfoDao;

	@Autowired
	private AlarmParamDao alarmParamDao;

	public int saveDeviceParam(DeviceParamVO paramVO) {
		DeviceParamDO paramDO = new DeviceParamDO();
		LsBeanUtils.copyAllProperties(paramVO, paramDO);
		return deviceParamDao.insertSelective(paramDO);
	}

	/**
	 * @Title: paramDomain 
	 * @Description: 添加
	 * @param deviceProbeVO
	 * @return int
	 * @throws 
	 */
	@Transactional
	public int saveProbeParam(DeviceProbeParamVO deviceProbeVO) {
		
		String sbCode = this.getMaxCodeById(deviceInfoDao.getMaxCodeById( deviceProbeVO.getCfbh()), deviceProbeVO.getCfbh(), 3);
		
		//保存探头设备网络参数信息
		DeviceInfoDO infoDO = new DeviceInfoDO();
		LsBeanUtils.copyAllProperties(deviceProbeVO, infoDO);
		infoDO.setSbCode(sbCode);
		infoDO.setCreateDate(new Date());
		infoDO.setSbXybb("V2.0");
		infoDO.setKddm(deviceProbeVO.getKddm());
		deviceInfoDao.insertSelective(infoDO);

		//保存探头基础参数信息
		ProbeParamDO probeParamDO = new ProbeParamDO();
		LsBeanUtils.copyAllProperties(deviceProbeVO, probeParamDO);
		probeParamDO.setSbCode(sbCode);
		probeParamDao.insertSelective(probeParamDO);

		AlarmParamDO record =new AlarmParamDO();
		record.setCfbh(deviceProbeVO.getCfbh());
		record.setCfmc(deviceProbeVO.getCfmc());
		alarmParamDao.insertSelective(record);
		//获取仓房信息
		DCfxxsjyVO cfxxsjyVO = cfxxsjyService.getDCfxxxsjyInfo(deviceProbeVO.getCfbh());

		//获取仓房状态 0:空仓 1:库存  2:出仓  3:入仓
//		String cfStatus = cfxxsjyVO.getCfzt();
		
		//根据仓房状态添加设备定时扫描任务
		TaskInfoDO startScanTask = new TaskInfoDO();
		String taskNo = this.getMaxCodeById(taskInfoDao.getMaxCodeById( "T"+DateUtil.getDate()), "T"+DateUtil.getDate(), 3);
		startScanTask.setTaskNo(taskNo);
		startScanTask.setTaskName("探头"+sbCode+"扫描开启");
		startScanTask.setExecuteType(0);
		startScanTask.setCronExpr("0 0 12 * * ?");
//		startScanTask.setExecuteClass(DetectionNumJob.class.getName());
		startScanTask.setExecuteClass(DetectionNumNewJob.class.getName());
		/*if("V1.0".equals(deviceProbeVO.getSbXybb())){
			startScanTask.setExecuteClass(DetectionNumJob.class.getName());
		}else if("V2.0".equals(deviceProbeVO.getSbXybb())){
			startScanTask.setExecuteClass(DetectionNumNewJob.class.getName());
		}*/
		startScanTask.setSbCode(sbCode);
		taskInfoDao.insertSelective(startScanTask);
		return 0;
	}

	/** 
	 * @Title: updateProbeParam 
	 * @Description: TODO
	 * @param @param deviceProbeVO
	 * @param @return
	 * @return int
	 * @throws 
	 */
	@Transactional
	public int updateProbeParam(DeviceProbeParamVO deviceProbeVO) {
		
		DeviceInfoDO deviceInfoDO = new DeviceInfoDO();
		LsBeanUtils.copyAllProperties(deviceProbeVO, deviceInfoDO);
		return deviceInfoDao.updateByPrimaryKeySelective(deviceInfoDO);
		
		/*ProbeParamDO probeParamDO = new ProbeParamDO();
		LsBeanUtils.copyAllProperties(deviceProbeVO, probeParamDO);
		return probeParamDao.updateBySbCode(probeParamDO);*/
	}

	/** 
	 * @Title: getDevicePageList 
	 * @Description: TODO
	 * @param deviceInfoQueryVO
	 * @param pageNum
	 * @param pageSize
	 * @return PageInfo<DeviceInfoVO>
	 * @throws 
	 */
	public PageInfo<DeviceInfoVO> getDevicePageList(DeviceInfoQueryVO deviceInfoQueryVO, Integer pageNum,
			Integer pageSize) {
		
		PageHelper.startPage(pageNum, pageSize);
		List<DeviceInfoDO> infoDOList = deviceInfoDao.selectListByParams(deviceInfoQueryVO);
		PageInfo<DeviceInfoDO> pageInfoDO = new PageInfo<>(infoDOList);
		
		PageInfo<DeviceInfoVO> pageInfoVO = new PageInfo<>();
		LsBeanUtils.copyAllProperties(pageInfoDO, pageInfoVO);
		
		return pageInfoVO;
	}

	/** 
	 * @Title: getDeviceProbeParam 
	 * @Description: TODO
	 * @param sbCode
	 * @param @return
	 * @return DeviceProbeParamVO
	 * @throws 
	 */
	public DeviceProbeParamVO getDeviceProbeParam(String sbCode) {
		DeviceInfoDO deviceInfoDO = deviceInfoDao.selectByPrimaryKey(sbCode);
		ProbeParamDO probeParamDO = probeParamDao.selectBySbCode(sbCode);
		
		DeviceProbeParamVO deviceProbeParamVO = new DeviceProbeParamVO();
		if(deviceInfoDO != null) {
			LsBeanUtils.copyAllProperties(deviceInfoDO, deviceProbeParamVO);
		}
		if(probeParamDO != null) {
			LsBeanUtils.copyAllProperties(probeParamDO, deviceProbeParamVO);
		}
		return deviceProbeParamVO;
	}

	/** 
	 * @Title: deleteDeviceParam 
	 * @Description: 根据设备code删除相关设备参数
	 * @param sbCode
	 * @return int
	 * @throws 
	 */
	public int deleteProbeParam(String sbCode) {
		
		int i = deviceInfoDao.deleteByPrimaryKey(sbCode);
		if(i > 0) {
			scanRuleDomain.deleteBySbCode(sbCode);
			probeParamDao.deleteBySbCode(sbCode);
			taskInfoDao.deleteBySbCode(sbCode);
		}
		return i;
	}

	/** 
	 * @Title: saveCameraParam 
	 * @Description: 摄像头参数新增
	 * @param deviceCameraVO
	 * @return int
	 * @throws 
	 */
	public int saveCameraParam(DeviceCameraParamVO deviceCameraVO) {
		
		String sbCode = this.getMaxCodeById(deviceInfoDao.getMaxCodeById(deviceCameraVO.getCfbh()), deviceCameraVO.getCfbh(), 3);
		
		DeviceInfoDO deviceinfoDO = new DeviceInfoDO();
		LsBeanUtils.copyAllProperties(deviceCameraVO, deviceinfoDO);
		deviceinfoDO.setSbCode(sbCode);
		deviceinfoDO.setCreateDate(new Date());
		deviceInfoDao.insertSelective(deviceinfoDO);
		
		CameraParamDO cameraParamDO = new CameraParamDO();
		LsBeanUtils.copyAllProperties(deviceCameraVO, cameraParamDO);
		cameraParamDO.setSbCode(sbCode);
		return cameraParamDao.insertSelective(cameraParamDO);
	}

	/** 
	 * @Title: updateCameraParam 
	 * @Description: 摄像头信息修改
	 * @param deviceCameraVO
	 * @return int
	 * @throws 
	 */
	public int updateCameraParam(DeviceCameraParamVO deviceCameraVO) {
		DeviceInfoDO deviceInfoDO = new DeviceInfoDO();
		LsBeanUtils.copyAllProperties(deviceCameraVO, deviceInfoDO);
		deviceInfoDao.updateByPrimaryKeySelective(deviceInfoDO);
		
		CameraParamDO cameraParamDO = new CameraParamDO();
		LsBeanUtils.copyAllProperties(deviceCameraVO, cameraParamDO);
		return cameraParamDao.updateBySbCode(cameraParamDO);
	}

	/** 
	 * @Title: getDeviceCameraParam 
	 * @Description: TODO
	 * @param sbCode
	 * @return DeviceCameraParamVO
	 * @throws 
	 */
	public DeviceCameraParamVO getDeviceCameraParam(String sbCode) {
		// TODO Auto-generated method stub
		DeviceInfoDO deviceInfoDO = deviceInfoDao.selectByPrimaryKey(sbCode);
		CameraParamDO cameraParamDO = cameraParamDao.selectBySbCode(sbCode);
		
		DeviceCameraParamVO deviceCameraParamVO = new DeviceCameraParamVO();
		if(deviceInfoDO != null) {
			LsBeanUtils.copyAllProperties(deviceInfoDO, deviceCameraParamVO);
		}
		if(cameraParamDO != null) {
			LsBeanUtils.copyAllProperties(cameraParamDO, deviceCameraParamVO);
		}
		return deviceCameraParamVO;
	}

	/** 
	 * @Title: deleteCameraParam 
	 * @Description:  根据sbCode删除摄像头参数
	 * @param sbCode
	 * @return int
	 * @throws 
	 */
	public int deleteCameraParam(String sbCode) {
		
		int i = deviceInfoDao.deleteByPrimaryKey(sbCode);
		if(i > 0) {
			scanRuleDomain.deleteBySbCode(sbCode);
			cameraParamDao.deleteBySbCode(sbCode);
		}
		return i;
	}

	/** 
	 * @Title: getDeviceNo 
	 * @Description: TODO
	 * @param cfbh
	 * @return String
	 * @throws 
	 */
	public String getDeviceNo(String cfbh) {
		// TODO Auto-generated method stub
		return this.getMaxCodeById(deviceInfoDao.getMaxCodeById(cfbh), cfbh, 3);
	}
}
