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.PurchaseNotice;
import com.uas.platform.b2b.model.PurchaseOrder;
import com.uas.platform.core.model.Constant;

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

/**
 * 时生成消息事件
 *
 */
public class PurchaseNoticeEndReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, PurchaseNotice> {

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

	public PurchaseNoticeEndReleaseEvent(List<PurchaseNotice> savedList) {
        super(savedList);
    }

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