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.TmsShipbillOperateLogMapper;
import com.prolog.rdc.tms.app.model.dto.business.ShipbillOperateLogDTO;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillOperateLogModel;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillOperateLogListVO;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillOperateLogSaveNewVO;
import com.prolog.rdc.tms.app.model.vo.business.ShipbillOperateLogSaveVO;
import com.prolog.rdc.tms.app.service.business.ShipbillOperateLogService;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;

import cn.hutool.core.date.DateUtil;

/**
 * 运单操作日志实现层
 *
 * @author zhangsj
 * @since 2022-03-31 14:45:00
 */
@Service
public class ShipbillOperateLogServiceImpl implements ShipbillOperateLogService {

	@Autowired
	private TmsShipbillOperateLogMapper tmsShipbillOperateLogMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	/**
	 * 运单操作日志查看
	 */
	@Override
	public List<ShipbillOperateLogDTO> listShipbillOperateLogData(ShipbillOperateLogListVO shipbillOperateLogListVO) {
		ToolKits.isParamNull(shipbillOperateLogListVO, "运单操作日志查看请求不为空");
		ToolKits.isParamNull(shipbillOperateLogListVO.getShipbillId(), "运单ID不能为空");

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

		Criteria criteria = Criteria.forClass(TmsShipbillOperateLogModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", shipbillOperateLogListVO.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().asc(new String[] { "operateDate" }));
		List<TmsShipbillOperateLogModel> tmsShipbillOperateLogModels = tmsShipbillOperateLogMapper.findByCriteria(criteria);

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

		for (TmsShipbillOperateLogModel tmsShipbillOperateLogModel : tmsShipbillOperateLogModels) {
			ShipbillOperateLogDTO dto = new ShipbillOperateLogDTO();
			BeanUtils.copyProperties(tmsShipbillOperateLogModel, dto);
			list.add(dto);
		}

		return list;
	}

	/**
	 * 运单操作日志新增
	 */
	@Override
	public void addShipbillOperateLogData(ShipbillOperateLogSaveVO vo) {
		ToolKits.isParamNull(vo.getShipbillId(), "运单ID不能为空");
		ToolKits.isParamNull(vo.getShipbillNo(), "运单编号不能为空");
		ToolKits.isParamNull(vo.getOperateEvent(), "操作事件不能为空");
		ToolKits.isParamNull(vo.getOldStatus(), "新运单状态不能为空");
		ToolKits.isParamNull(vo.getNewStatus(), "原运单状态不能为空");

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

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

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

		// 日志记录
		TmsShipbillOperateLogModel model = new TmsShipbillOperateLogModel();
		BeanUtils.copyProperties(vo, model);
		model.setId(id);
		model.setOperateDate(date);
		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);

		tmsShipbillOperateLogMapper.save(model);
	}

	/**
	 * 运单操作日志新增(新)
	 *
	 * @param vo
	 */
	@Override
	public void addShipbillOperateLogNewData(ShipbillOperateLogSaveNewVO vo) {
		ToolKits.isParamNull(vo.getShipbillId(), "运单ID不能为空");
		ToolKits.isParamNull(vo.getShipbillNo(), "运单编号不能为空");
		ToolKits.isParamNull(vo.getOperateEvent(), "操作事件不能为空");
		ToolKits.isParamNull(vo.getOldStatus(), "新运单状态不能为空");
		ToolKits.isParamNull(vo.getNewStatus(), "原运单状态不能为空");
		ToolKits.isParamNull(vo.getOperateDate(), "操作时间不能为空");

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

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

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

		// 日志记录
		TmsShipbillOperateLogModel model = new TmsShipbillOperateLogModel();
		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);
		tmsShipbillOperateLogMapper.save(model);
	}
}
