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.PurchaseOrderItem;
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 PurchaseOrderEndReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, PurchaseOrderItem> {

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

	public PurchaseOrderEndReleaseEvent(List<PurchaseOrderItem> savedList) {
        super(savedList);
    }

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