package com.uas.platform.b2b.event;

import com.uas.platform.b2b.model.*;
import com.uas.platform.core.model.Constant;
import com.uas.platform.core.model.Status;

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

/**
 * 委外变更单保存时生成消息事件
 *
 */
public class MakeOrderChangeSaveReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, MakeOrderChangeItem> {

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

	public MakeOrderChangeSaveReleaseEvent(List<MakeOrderChangeItem> savedList) {
        super(savedList);
    }

    /**
     * 生成对应消息记录
     *
     * @param item
     * @return
     */
    @Override
	public PagingReleaseDetail release(MakeOrderChangeItem item) {
        MakeOrderChange change = item.getOrderChange();
        // 保存委外变更单之后，触发新增消息事件。
        Long emUU = change.getUserUU();
        String emName = change.getRecorder();
        Long enUU = change.getEnUU();
        Date date = new Date();
        String codeValue = change.getCode();
        String from = "B2B";
        if (change.getErpId() != null) { // erp传到平台的单据，包含erpId字段记录来源
            from = "ERP";
        }
        String type = "新增委外变更单";
        String title = "B2B商务提醒";
        Short toVendor = Constant.YES;
        Short isReply = Constant.NO;
        String category = "sale";  // 对接收方来说，该单据类型
        String table = "make$changes";
        Long sourceId = change.getId();
        Short orderStatus = (short)Status.NOT_REPLY.value();
        String url = "#/sale/makechange/" + change.getId();
        String context = change.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 = item.getVendUU();
        Long vendUserUU = item.getVendUserUU();
        if (null == vendUserUU) {
            vendUserUU = 1L;
        }
        String vendUserName = "业务员";
        PagingReleaseDetail releaseDetail = new PagingReleaseDetail(vendUU, vendUserUU, vendUserName);
        releaseDetail.setPagingRelease(release);
        return releaseDetail;
    }
}
