package com.uas.platform.b2b.event;

import com.uas.platform.b2b.model.PagingRelease;
import com.uas.platform.b2b.model.PagingReleaseDetail;
import com.uas.platform.b2b.model.PurchaseOrderInfo;
import com.uas.platform.core.model.Constant;

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

/**
 * 采购订单保存时生成消息事件
 * Created by dongbw
 * 17/03/23 14:06.
 */
public class PurchaseOrderSaveReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, PurchaseOrderInfo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public PurchaseOrderSaveReleaseEvent(List<PurchaseOrderInfo> savedList) {
        super(savedList);
    }

    /**
     * 生成对应消息记录
     *
     * @param orderAll
     * @return
     */
    @Override
	public PagingReleaseDetail release(PurchaseOrderInfo orderAll) {
        //TODO 保存新订单之后，触发新增消息事件。
        Long emUU = orderAll.getUserUU();
        String emName = "操作员";
        if (null != orderAll.getUser()) {
            emName = orderAll.getUser().getUserName();
        }
        Long enUU = orderAll.getEnUU();
        Date date = new Date();
        String codeValue = orderAll.getCode();
        String from = "B2B";
        if (orderAll.getErpId() != null) { // erp传到平台的单据，包含erpId字段记录来源
            from = "ERP";
        }
        String type = "新增采购订单";
        String title = "B2B商务提醒";
        Short toVendor = Constant.YES;
        Short isReply = Constant.NO;
        String category = "sale";  // 对接收方来说，该单据类型
        String table = "purc$orders";
        Long sourceId = orderAll.getId();
        String url = "#/sale/order/" + orderAll.getId();
        Short orderStatus = orderAll.getStatus();
        String context = orderAll.getEnterprise().getEnName() + "的" + emName + "给您发送了一张新的采购单（" + codeValue + ")！";
        PagingRelease release = new PagingRelease(emUU, emName, enUU, date, codeValue, from, title, context, type, isReply, category, table, sourceId, orderStatus, toVendor, url);
        Long vendUU = orderAll.getVendUU();
        Long vendUserUU = orderAll.getVendUserUU();
        if (null == vendUserUU) {
            vendUserUU = 1L;
        }
        String vendUserName = orderAll.getVenduser();
        PagingReleaseDetail releaseDetail = new PagingReleaseDetail(vendUU, vendUserUU, vendUserName);
        releaseDetail.setPagingRelease(release);
        return releaseDetail;
    }
}
