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 PurchaseNoticeSaveReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, PurchaseNotice> {

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

	public PurchaseNoticeSaveReleaseEvent(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 = order.getRecorder();
        Long enUU = notice.getEnUU();
        Date date = new Date();
        String codeValue = order.getCode();
        String from = "B2B";
        if (notice.getSourceId() != null) { // erp传到平台的单据，包含erpId字段记录来源
            from = "ERP";
        }
        String type = "新增发货提醒";
        String title = "B2B商务提醒";
        Short toVendor = Constant.YES;
        Short isReply = Constant.NO;
        String category = "sale";  // 对接收方来说，该单据类型
        String table = "purc$notice";
        Long sourceId = notice.getId();
        String url = "#/sale/notice";
        Short orderStatus = notice.getStatus();
        String context = order.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 = notice.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;
    }
}
