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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 询价单保存时生成消息事件
 *
 * Created by dongbw
 *
 */
public class PurchaseInquiryItemSaveReleaseEvent extends SaveReleaseEvent<PagingReleaseDetail, PurchaseInquiryItem> {

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

	public PurchaseInquiryItemSaveReleaseEvent(List<PurchaseInquiryItem> savedList) {
        super(savedList);
    }

    /**
     * 生成对应消息记录
     *
     * @param inquiryItem
     * @return
     */
    @Override
    public PagingReleaseDetail release(PurchaseInquiryItem inquiryItem) {
        //TODO 保存erp新询价单之后，触发新增消息事件。
        Long emUU = inquiryItem.getUserUU();
        String emName = inquiryItem.getInquiry().getRecorder();
        Long enUU = inquiryItem.getInquiry().getEnUU();
        Date date = new Date();
        String codeValue = inquiryItem.getInquiry().getCode();
        String from = "ERP";
        if ("B2B".equals(inquiryItem.getInquiry().getSourceApp())) { // erp传到平台的单据，包含erpId字段记录来源
            from = "B2B";
        }
        Short isReply = Constant.NO;
        String category = "sale";
        String table = "v$purc$inquiryitems";
        Long sourceId = inquiryItem.getId();
        Short orderStatus = inquiryItem.getStatus();
        Short toVendor = Constant.YES;
        String type = "新增询价单";
        String title = "B2B商务提醒";
        // 收到的客户采购询价单的详情url
        String url = "#/sale/inquiry/" + inquiryItem.getInquiry().getId();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String orderDate = formatter.format(inquiryItem.getInquiry().getDate());
        String context = "您的合作伙伴\"" + inquiryItem.getInquiry().getEnterprise().getEnName() + "\"发布新的询价单(" + codeValue + ")，请快速报价！";
        PagingRelease release = new PagingRelease(emUU, emName, enUU, date, codeValue, from, title, context, type, isReply, category, table, sourceId, orderStatus, toVendor, url);
        Long vendUU = inquiryItem.getVendUU();
        Long vendUserUU = inquiryItem.getVendUserUU();
        String vendUserName = "操作员";
        if (null == vendUserUU) {
            vendUserUU = 1L;
        }
        PagingReleaseDetail releaseDetail = new PagingReleaseDetail(vendUU, vendUserUU, vendUserName);
        releaseDetail.setPagingRelease(release);
        return releaseDetail;
    }
}
