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

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

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


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

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

    /**
     * 生成对应消息记录
     *
     * @param inquiryItem
     * @return
     */
    @Override
    public PagingReleaseDetail release(PurchaseInquiryItem inquiryItem) {
        // 回复订单的话，跟新增订单是相反的，所以接收人是user，发出人是vendUser
        Long emUU = inquiryItem.getVendUserUU();
        String emName = "操作员";
        Long vendUU = inquiryItem.getVendUU();
        Date date = new Date();
        String codeValue = inquiryItem.getInquiry().getCode();
        String from = "B2B";
        String type = "回复询价单";
        String title = "B2B商务提醒";
        String enName = inquiryItem.getEnterprise().getEnName();
        String context;
        if (inquiryItem.getStatus() == Status.REPLIED.value()) {
            context = enName + "的" + emName + "修改了询价单（" + codeValue + "）的报价，请及时确认！";
        } else {

            context = enName + "的" + emName + "回复了询价单（" + codeValue + "）的报价，请及时确认！";
        }
        Short isReply = Constant.YES;
        String category = "purc";
        String table = "v$purc$inquiryitems";
        Long sourceId = inquiryItem.getId();
        Short orderStatus = inquiryItem.getStatus();
        Short toVendor = Constant.NO;
        String url = "#/purc/purcinquirydetail/" + inquiryItem.getInquiry().getId();
        PagingRelease release = new PagingRelease(emUU, emName, vendUU, date, codeValue, from, title, context, type, isReply, category, table, sourceId, orderStatus, toVendor, url);
        Long enUU = inquiryItem.getInquiry().getEnUU();
        Long userUU = inquiryItem.getUserUU();
        String userName = "操作员";
        if (null == userUU) {
           userUU = 1L;
        }
        if (null != inquiryItem.getInquiry().getRecorder()) {
            userName = inquiryItem.getInquiry().getRecorder();
        }
        PagingReleaseDetail releaseDetail = new PagingReleaseDetail(enUU, userUU, userName);
        releaseDetail.setPagingRelease(release);
        return releaseDetail;
    }
}
