package com.uas.platform.b2b.event;

import com.uas.platform.b2b.model.MakeOrder;
import com.uas.platform.b2b.model.PagingRelease;
import com.uas.platform.b2b.model.PagingReleaseDetail;
import com.uas.platform.core.model.Constant;

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

/**
 * 委外订单结案时生成消息事件
 *
 */
public class MakeOrderEndReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, MakeOrder> {

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

	public MakeOrderEndReleaseEvent(List<MakeOrder> savedList) {
        super(savedList);
    }

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