package com.prolog.rdc.tms.service.basicConfigure.impl;

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

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.ParameterException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.dao.mapper.SysAlertMessageMapper;
import com.prolog.rdc.tms.model.dto.basicConfigure.MessageCountDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SysAlertMessageQueryDTO;
import com.prolog.rdc.tms.model.entity.SysAlertMessageModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.AlertMessageAddVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.AlertMessageIsExistVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysAlertMessageHandleVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysAlertMessageQueryVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SysAlertMessageReadVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.basicConfigure.SysAlertMessageService;

import cn.hutool.core.date.DateUtil;

/**
 * 首页-预警消息实现层
 *
 * @author :fanzw
 * @version :1.0.0
 * @className :SysAlertMessageServiceImpl
 * @creationTime :2022/01/18
 * @description :
 * @modifier :fanzw
 * @modifyTime :2022/01/18
 * @reviseRemarks:
 */
@Service
public class SysAlertMessageServiceImpl implements SysAlertMessageService {

	@Autowired
	private SysAlertMessageMapper sysAlertMessageMapper;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	/**
	 * 预警消息查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<SysAlertMessageQueryDTO> listAlertMessageData(SysAlertMessageQueryVO sysAlertMessageQueryVO) {
		ToolKits.isParamNull(sysAlertMessageQueryVO, "预警消息查询请求不为空");
		ToolKits.isParamNull(sysAlertMessageQueryVO.getPageNum(), "页码不为空");
		ToolKits.isParamNull(sysAlertMessageQueryVO.getPageSize(), "页码数量不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 响应对象集合
		List<SysAlertMessageQueryDTO> sysAlertMessageQueryDTOs = new ArrayList<SysAlertMessageQueryDTO>();

		// 分页
		PageUtils.startPage(sysAlertMessageQueryVO.getPageNum(), sysAlertMessageQueryVO.getPageSize());

		// 查询
		Criteria criteria = Criteria.forClass(SysAlertMessageModel.class);
		Restriction r1 = Restrictions.eq("receivePeopleId", String.valueOf(user.getUserid()));
		Restriction r2 = Restrictions.eq("messageTypeCode", BussinessConstant.ALERT_MESSAGE_TYPE_YJ);
		Restriction r3 = Restrictions.eq("whetherRead", BussinessConstant.ALERT_MESSAGE_WHETHER_READ_N);
		Restriction r4 = Restrictions.eq("status", BussinessConstant.ALERT_MESSAGE_STATUS_N);
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<SysAlertMessageModel> sysAlertMessageModels = sysAlertMessageMapper.findByCriteria(criteria);
		for (SysAlertMessageModel sysAlertMessageModel : sysAlertMessageModels) {
			SysAlertMessageQueryDTO sysAlertMessageQueryDTO = new SysAlertMessageQueryDTO();
			BeanUtils.copyProperties(sysAlertMessageModel, sysAlertMessageQueryDTO);

			sysAlertMessageQueryDTOs.add(sysAlertMessageQueryDTO);
		}

		Page<SysAlertMessageQueryDTO> page = PageUtils.getPage(sysAlertMessageQueryDTOs);
		if (sysAlertMessageModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<SysAlertMessageModel> p0 = (com.github.pagehelper.Page<SysAlertMessageModel>) sysAlertMessageModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 预警消息是否存在
	 *
	 * @return
	 */
	public Boolean alertMessageIsExist(AlertMessageIsExistVO alertMessageIsExistVO) {
		ToolKits.isParamNull(alertMessageIsExistVO, "预警消息是否存在请求不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		Boolean isExist = false;

		// 查询
		Criteria criteria = Criteria.forClass(SysAlertMessageModel.class);
		Restriction r1 = Restrictions.eq(SysAlertMessageModel::getMessageTypeCode, alertMessageIsExistVO.getMessageTypeCode());
		Restriction r2 = Restrictions.eq(SysAlertMessageModel::getRefbillid, alertMessageIsExistVO.getRefbillid());
		Restriction r3 = Restrictions.eq(SysAlertMessageModel::getCustomerIds, alertMessageIsExistVO.getCustomerIds());
		criteria.setRestriction(Restrictions.and(r1, r2, r3));

		List<SysAlertMessageModel> sysAlertMessageModels = sysAlertMessageMapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(sysAlertMessageModels)) {
			isExist = true;
		}
		return isExist;
	}

	/**
	 * 预警消息新增
	 *
	 * @return
	 */
	@Transactional(rollbackFor = { Exception.class })
	public void alertMessageAdd(List<AlertMessageAddVO> alertMessageAddVOs) {
		ToolKits.isParamNull(alertMessageAddVOs, "预警消息新增请求不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		Date date = DateUtil.date();

		for (AlertMessageAddVO addVO : alertMessageAddVOs) {
			SysAlertMessageModel messageModel = new SysAlertMessageModel();
			BeanUtils.copyProperties(addVO, messageModel);
			String msgId = sysCodeengineService.getCodeNumberByCode(BussinessConstant.SYS_ALERT_MESSAGE_ID, "L");
			ToolKits.isParamNull(msgId, "消息ID生成失败");

			messageModel.setMessageId(msgId);
			messageModel.setReceiveTime(date);
			messageModel.setSendTime(date);
			messageModel.setSendPeopleName("系统管理员");
			messageModel.setMessageTypeCode(BussinessConstant.ALERT_MESSAGE_TYPE_YJ);
			messageModel.setMessageTypeName("预警");
			messageModel.setWhetherRead(BussinessConstant.ALERT_MESSAGE_WHETHER_READ_N);
			messageModel.setStatus(BussinessConstant.ALERT_MESSAGE_STATUS_N);
			messageModel.setCreateperson(user.getNickname());
			messageModel.setCreatetime(date);
			sysAlertMessageMapper.save(messageModel);
		}
	}

	/**
	 * 消息数量查询
	 */
	@Override
	public MessageCountDTO countMessageData() {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 响应对象集合
		MessageCountDTO messageCountDTO = new MessageCountDTO();

		// 查询
		Criteria criteria = Criteria.forClass(SysAlertMessageModel.class);
		Restriction r1 = Restrictions.eq("receivePeopleId", String.valueOf(user.getUserid()));
		Restriction r2 = Restrictions.eq("messageTypeCode", BussinessConstant.ALERT_MESSAGE_TYPE_YJ);
		Restriction r3 = Restrictions.eq("whetherRead", BussinessConstant.ALERT_MESSAGE_WHETHER_READ_N);
		Restriction r4 = Restrictions.eq("status", BussinessConstant.ALERT_MESSAGE_STATUS_N);
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		long count = sysAlertMessageMapper.findCountByCriteria(criteria);

		// 预警消息数量
		messageCountDTO.setAlertMessageQuantity(Integer.valueOf(String.valueOf(count)));

		return messageCountDTO;
	}

	/**
	 * 预警消息已读
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void readAlertMessageData(List<SysAlertMessageReadVO> sysAlertMessageReadVOs) {
		ToolKits.isParamNull(sysAlertMessageReadVOs, "预警消息已读请求不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 当前时间
		Date date = DateUtil.date();

		for (SysAlertMessageReadVO sysAlertMessageReadVO : sysAlertMessageReadVOs) {
			ToolKits.isParamNull(sysAlertMessageReadVO.getMessageId(), "预警消息ID不为空");

			SysAlertMessageModel sysAlertMessageModel = sysAlertMessageMapper.findById(sysAlertMessageReadVO.getMessageId(), SysAlertMessageModel.class);
			if (ToolKits.isNotEmpty(sysAlertMessageModel)) {
				sysAlertMessageModel.setWhetherRead(BussinessConstant.ALERT_MESSAGE_WHETHER_READ_Y);
				sysAlertMessageModel.setUpdateperson(user.getNickname());
				sysAlertMessageModel.setUpdatetime(date);
				sysAlertMessageMapper.update(sysAlertMessageModel);
			} else {
				throw new ParameterException("预警消息ID【" + sysAlertMessageReadVO.getMessageId() + "】不存在");
			}
		}
	}

	/**
	 * 预警消息处理
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void handleAlertMessageData(List<SysAlertMessageHandleVO> sysAlertMessageHandleVOs) {
		ToolKits.isParamNull(sysAlertMessageHandleVOs, "预警消息处理请求不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 当前时间
		Date date = DateUtil.date();

		for (SysAlertMessageHandleVO sysAlertMessageHandleVO : sysAlertMessageHandleVOs) {
			ToolKits.isParamNull(sysAlertMessageHandleVO.getMessageId(), "预警消息ID不为空");

			SysAlertMessageModel sysAlertMessageModel = sysAlertMessageMapper.findById(sysAlertMessageHandleVO.getMessageId(), SysAlertMessageModel.class);
			if (ToolKits.isNotEmpty(sysAlertMessageModel)) {
				sysAlertMessageModel.setWhetherRead(BussinessConstant.ALERT_MESSAGE_WHETHER_READ_Y);
				sysAlertMessageModel.setStatus(BussinessConstant.ALERT_MESSAGE_STATUS_Y);
				sysAlertMessageModel.setUpdateperson(user.getNickname());
				sysAlertMessageModel.setUpdatetime(date);
				sysAlertMessageMapper.update(sysAlertMessageModel);
			} else {
				throw new ParameterException("预警消息ID【" + sysAlertMessageHandleVO.getMessageId() + "】不存在");
			}
		}
	}

}
