package com.uas.platform.b2b.event;

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

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

/**
 * Created by dongbw
 * 17/07/26 16:34.
 */
public class SaleQuotationDecideReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, SaleQuotationItem>{


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

    public SaleQuotationDecideReleaseEvent(List<SaleQuotationItem> savedList) {
        super(savedList);
    }

    /**
     * 生成对应消息记录
     *
     * @param item
     * @return
     */
    @Override
    public PagingReleaseDetail release(SaleQuotationItem item) {
        SaleQuotation quotation = item.getQuotation();
        //TODO ERP做出是否采纳判断之后，B2B产生消息
        Long emUU = quotation.getCustUserUU();
        String emName = "操作员";
        if (item.getQuotation().getCustUser() != null) {
            emName = quotation.getCustUser().getUserName();
        }
        Long enUU = quotation.getCustUU();
        Date date = new Date();
        String codeValue = quotation.getCode();
        String from = "B2B";
        if (quotation.getErpId() != null) { // erp传到平台的单据，包含erpId字段记录来源
            from = "ERP";
        }
        String type = "回复主动报价单";
        String title = "B2B商务提醒";
        Short toVendor = Constant.YES;
        Short isReply = Constant.YES;
        String category = "sale";  // 对接收方来说，该单据类型
        String table = "sale$quotation";
        Long sourceId = quotation.getId();
        String url = "#/sale/quotation/" + quotation.getId();
        Short orderStatus = quotation.getStatus();
        String context;
        if (item.getAgreed() == Constant.YES) {
            context = quotation.getCust().getEnName() + "的" + emName + "采纳了您的报价（" + codeValue + ")！";
        } else {
            context = quotation.getCust().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 = quotation.getEnUU();
        Long vendUserUU = quotation.getUserUU();
        if (null == vendUserUU) {
            vendUserUU = 1L;
        }
        String vendUserName = "业务员";
        if (null != quotation.getUser()) {
            vendUserName = quotation.getUser().getUserName();
        }
        PagingReleaseDetail releaseDetail = new PagingReleaseDetail(vendUU, vendUserUU, vendUserName);
        releaseDetail.setPagingRelease(release);
        return releaseDetail;
    }
}
