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

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

/**
 * 回复采购订单时生成消息事件
 * Created by dongbw
 *
 */
public class PurchaseOrderReplyReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, PurchaseOrder> {

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

	public PurchaseOrderReplyReleaseEvent(List<PurchaseOrder> savedList) {
        super(savedList);
    }

    /**
     * 生成对应消息记录
     *
     * @param orderAll
     * @return
     */
    @Override
    public PagingReleaseDetail release(PurchaseOrder orderAll) {
        // 回复订单的话，跟新增订单是相反的，所以接收人是user，发出人是venduser
        Long emUU = orderAll.getVendUserUU();
        String emName = "操作员";  // 默认一个值，避免为空
        if (null != orderAll.getVenduser()) {
            emName = orderAll.getVenduser();
        }
        Long vendUU = orderAll.getVendUU();
        Date date = new Date();
        String codeValue = orderAll.getCode();
        String from = "B2B";
        String type = "回复采购订单";
        String title = "B2B商务提醒";
        Short toVendor = Constant.NO;
        Short isReply = Constant.YES;
        String category = "purc";
        String table = "purc$orders";
        Long sourceId = orderAll.getId();
        String url = "#/purc/order/" + orderAll.getId();
        Short orderStatus = (short)Status.REPLIED.value(); // 回复的单，直接设置为已回复状态
        String context = orderAll.getReceiveName() + "的" + emName + "回复了采购单（" + codeValue + "）的交期，请及时确认！";
        PagingRelease release = new PagingRelease(emUU, emName, vendUU, date, codeValue, from, title, context, type, isReply, category, table, sourceId, orderStatus, toVendor, url);
        Long enUU = orderAll.getEnUU();
        Long userUU = orderAll.getUserUU();
        if (userUU == null) {
            userUU = 1L;
        }
        String userName = "操作员";
        if (null != orderAll.getUser()) {
            userName = orderAll.getUser().getUserName();
        }
        PagingReleaseDetail releaseDetail = new PagingReleaseDetail(enUU, userUU, userName);
        releaseDetail.setPagingRelease(release);
        return releaseDetail;
    }
}