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

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

import cn.hutool.core.util.ObjectUtil;
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.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillOperateLogMapper;
import com.prolog.rdc.tms.model.dto.ShipbillOperateLogDTO;
import com.prolog.rdc.tms.model.entity.TmsShipbillOperateLogModel;
import com.prolog.rdc.tms.model.vo.ShipbillOperateLogListVO;
import com.prolog.rdc.tms.model.vo.ShipbillOperateLogSaveNewVO;
import com.prolog.rdc.tms.model.vo.ShipbillOperateLogSaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.ShipbillOperateLogService;

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, UserEntity user) {
		ToolKits.isParamNull(vo.getShipbillId(), "运单ID不能为空");
		ToolKits.isParamNull(vo.getShipbillNo(), "运单编号不能为空");
		ToolKits.isParamNull(vo.getOperateEvent(), "操作事件不能为空");
		ToolKits.isParamNull(vo.getOldStatus(), "新运单状态不能为空");
		ToolKits.isParamNull(vo.getNewStatus(), "原运单状态不能为空");

		if (ObjectUtil.isNull(user)) {
			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);
	}

	@Override
	public void addShipbillOperateLogData(ShipbillOperateLogSaveVO vo) {
		addShipbillOperateLogData(vo, null);
	}

	/**
	 * 运单操作日志新增(新)
	 *
	 * @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(), "操作时间不能为空");
		ToolKits.isParamNull(vo.getUserId(), "操作用户ID不能为空");
		ToolKits.isParamNull(vo.getUserName(), "操作用户名称不能为空");
		ToolKits.isParamNull(vo.getEnterpriseId(), "企业ID不能为空");
		ToolKits.isParamNull(vo.getEnterpriseName(), "企业名称不能为空");

		// 获取日志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.setCreateperson(vo.getUserName());
		model.setCreatetime(vo.getOperateDate());
		model.setUpdateperson(vo.getUserName());
		model.setUpdatetime(vo.getOperateDate());

		tmsShipbillOperateLogMapper.save(model);
	}
}
