package com.prolog.rdc.tms.app.service.business.impl;

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 com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.app.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.app.dao.mapper.TmsShipbillNodeTrackingMapper;
import com.prolog.rdc.tms.app.model.dto.ShipbillNodeTrackingDTO;
import com.prolog.rdc.tms.app.model.entity.ShipbillNodeTracking;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillNodeTrackingListVO;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillNodeTrackingRollbackVO;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillNodeTrackingSaveNewVO;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillNodeTrackingSaveVO;
import com.prolog.rdc.tms.app.service.business.ShipbillNodeTrackingService;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;

import cn.hutool.core.date.DateUtil;

/**
 * 运单节点跟踪实现层
 *
 * @author zhangsj
 * @since 2022-03-31 14:49:08
 */
@Service
public class ShipbillNodeTrackingServiceImpl implements ShipbillNodeTrackingService {

	@Autowired
	private TmsShipbillNodeTrackingMapper tmsShipbillNodeTrackingMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	/**
	 * 运单节点查看
	 */
	@Override
	public List<ShipbillNodeTrackingDTO> listShipbillNodeTrackingData(ShipbillNodeTrackingListVO shipbillNodeTrackingListVO) {
		ToolKits.isParamNull(shipbillNodeTrackingListVO, "运单节点查看请求不为空");
		ToolKits.isParamNull(shipbillNodeTrackingListVO.getShipbillId(), "运单ID不能为空");

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

		Criteria criteria = Criteria.forClass(ShipbillNodeTracking.class);
		Restriction r1 = Restrictions.eq("shipbillId", shipbillNodeTrackingListVO.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().asc(new String[] { "nodeDate" }));
		List<ShipbillNodeTracking> tmsShipbillNodeTrackingModels = tmsShipbillNodeTrackingMapper.findByCriteria(criteria);

		// 返回数据集
		List<ShipbillNodeTrackingDTO> list = Lists.newArrayList();

		for (ShipbillNodeTracking tmsShipbillNodeTrackingModel : tmsShipbillNodeTrackingModels) {
			ShipbillNodeTrackingDTO dto = new ShipbillNodeTrackingDTO();
			BeanUtils.copyProperties(tmsShipbillNodeTrackingModel, dto);
			list.add(dto);
		}

		return list;
	}

	/**
	 * 运单节点新增
	 * 
	 * @param vo
	 */
	@Override
	public void addShipbillNodeTrackingData(ShipbillNodeTrackingSaveVO vo) {
		ToolKits.isParamNull(vo.getShipbillId(), "运单ID不能为空");
		ToolKits.isParamNull(vo.getShipbillNo(), "运单编号不能为空");
		ToolKits.isParamNull(vo.getNodeEvent(), "节点事件不能为空");
		ToolKits.isParamNull(vo.getStatus(), "运单状态不能为空");

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

		// 获取节点ID
		RestMessage<String> restMessage = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_NODE_TRACKING_ID, "L");
		if (!restMessage.isSuccess()) {
			throw new RuntimeException("生成节点ID失败");
		}
		String id = restMessage.getData();

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

		// 节点记录
		ShipbillNodeTracking model = new ShipbillNodeTracking();
		BeanUtils.copyProperties(vo, model);
		model.setId(id);
		model.setNodeDate(ToolKits.isEmpty(vo.getNodeDate()) ? date : vo.getNodeDate());
		model.setUserId(user.getUserid());
		model.setUserName(user.getNickname());
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());
		model.setCreateperson(user.getNickname());
		model.setCreatetime(date);
		model.setUpdateperson(user.getNickname());
		model.setUpdatetime(date);
		tmsShipbillNodeTrackingMapper.save(model);
	}

	/**
	 * 运单节点新增(新)
	 *
	 * @param vo
	 */
	@Override
	public void addShipbillNodeTrackingNewData(ShipbillNodeTrackingSaveNewVO vo) {
		ToolKits.isParamNull(vo.getShipbillId(), "运单ID不能为空");
		ToolKits.isParamNull(vo.getShipbillNo(), "运单编号不能为空");
		ToolKits.isParamNull(vo.getNodeEvent(), "节点事件不能为空");
		ToolKits.isParamNull(vo.getStatus(), "运单状态不能为空");
		ToolKits.isParamNull(vo.getNodeDate(), "节点时间不能为空");

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

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

		// 获取节点ID
		RestMessage<String> restMessage = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_NODE_TRACKING_ID, "L");
		if (!restMessage.isSuccess()) {
			throw new RuntimeException("生成节点ID失败");
		}
		String id = restMessage.getData();

		// 节点记录
		ShipbillNodeTracking model = new ShipbillNodeTracking();
		BeanUtils.copyProperties(vo, model);
		model.setId(id);
		model.setUserId(user.getUserid());
		model.setUserName(user.getNickname());
		model.setEnterpriseId(user.getEnterpriseId());
		model.setEnterpriseName(user.getEnterpriseName());
		model.setCreateperson(user.getNickname());
		model.setCreatetime(date);
		model.setUpdateperson(user.getNickname());
		model.setUpdatetime(date);
		tmsShipbillNodeTrackingMapper.save(model);
	}

	/**
	 * 运单节点回退
	 *
	 * @param vo
	 */
	@Override
	public void rollbackShipbillNodeTracking(ShipbillNodeTrackingRollbackVO vo) {
		ToolKits.isParamNull(vo.getShipbillId(), "运单ID不能为空");
		ToolKits.isParamNull(vo.getStatus(), "运单状态不能为空");

		Criteria criteria = Criteria.forClass(ShipbillNodeTracking.class);
		Restriction r1 = Restrictions.eq("shipbillId", vo.getShipbillId());
		Restriction r2 = Restrictions.gt("status", vo.getStatus());
		criteria.setRestriction(Restrictions.and(r1, r2));

		tmsShipbillNodeTrackingMapper.deleteByCriteria(criteria);
	}

}
