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

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

/**
 * 打样申请单保存时生成消息事件
 *
 * Created by dongbw
 *
 */
public class PurchaseProofingItemSaveReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, PurchaseProofingItem> {

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

	public PurchaseProofingItemSaveReleaseEvent(List<PurchaseProofingItem> savedList) {
        super(savedList);
    }

    /**
     * 生成对应消息记录
     *
     * @param proofingItem
     * @return
     */
    @Override
    public PagingReleaseDetail release(PurchaseProofingItem proofingItem) {
        PurchaseProofing proofing = proofingItem.getProofing();
        Long emUU = proofing.getUserUU();
        String emName = proofing.getRecorder();
        Long enUU = proofing.getEnUU();
        Date date = new Date();
        String codeValue = proofing.getCode();
        String from = "B2B";
        if (proofing.getErpId() != null) { // erp传到平台的单据，包含erpId字段记录来源
            from = "ERP";
        }
        Short isReply = Constant.NO;
        String category = "sale";
        String table = "purc$proofingitems";
        Long sourceId = proofingItem.getId();
        Short orderStatus = proofingItem.getStatus();
        Short toVendor = Constant.YES;
        String type = "新增打样申请";
        String title = "B2B商务提醒";
        String url = "#/sale/sample/" + proofing.getId();
        String context = proofing.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 = proofingItem.getVendUU();
        Long vendUserUU = proofingItem.getVendUserUU();
        String vendUserName = "业务员";
        if (null == vendUserUU) {
            vendUserUU = 1L;
        }
        if (null != proofingItem.getVendUser()) {
            vendUserName = proofingItem.getVendUser().getUserName();
        }
        PagingReleaseDetail releaseDetail = new PagingReleaseDetail(vendUU, vendUserUU, vendUserName);
        releaseDetail.setPagingRelease(release);
        return releaseDetail;
    }
}
