Browse Source

商机下载轮询程序调整

will.chen 7 years ago
parent
commit
a792e094ea

+ 1 - 1
src/main/java/com/uas/erp/schedular/b2b/task/QuotationDownTask.java

@@ -175,7 +175,7 @@ public class QuotationDownTask extends AbstractTask {
      * @return
      */
     private List<QuotationDown> getQuotationReply() {
-        List<QuotationDown> downs = jdbcTemplate.queryForBeanList("select b2b_id_id,qu_id,qu_custuu,qu_fromdate,qu_todate,qu_minbuyqty,qu_minqty,qu_leadtime,qu_brand,qu_prodcode from QuotationDown where QU_SENDSTATUS='待上传' order by qu_code,qu_detno",
+        List<QuotationDown> downs = jdbcTemplate.queryForBeanList("select b2b_id_id,qu_id,qu_custuu,qu_fromdate,qu_todate,qu_minbuyqty,qu_minqty,qu_leadtime,qu_brand,qu_prodcode from QuotationDown where QU_SENDSTATUS='待上传' and qu_source<>'平台商机' order by qu_code,qu_detno",
                 QuotationDown.class);
         for (QuotationDown down : downs) {
             List<QuotationDownDetail> details = jdbcTemplate.queryForBeanList("select * from QuotationDownDetail where qd_quid=?",

+ 310 - 0
src/main/java/com/uas/erp/schedular/publicinquiry/domain/PublicInquiry.java

@@ -0,0 +1,310 @@
+package com.uas.erp.schedular.publicinquiry.domain;
+
+import java.util.Date;
+import java.util.Set;
+
+public class PublicInquiry {
+
+    /**
+     * id
+     */
+    private Long id;
+
+    /**
+     * 询价单所属企业UU
+     */
+    private Long enUU;
+
+    /**
+     * 企业名称(方便搜索)
+     */
+    private String enName;
+
+    /**
+     * 询价单所属用户UU
+     */
+    private Long recorderUU;
+
+    /**
+     * 流水号
+     */
+    private String code;
+
+    /**
+     * 单据归属日期
+     */
+    private Date date;
+
+    /**
+     * 录入人
+     */
+    private String recorder;
+
+    /**
+     * 审核人
+     */
+    private String auditor;
+
+    /**
+     * 报价截止日期
+     */
+    private Date endDate;
+
+    /**
+     * 备注
+     */
+    private String remark;
+
+    /**
+     * 环保要求
+     */
+    private String environment;
+
+    /**
+     * 价格类型
+     */
+    private String priceType;
+
+    /**
+     * 询价明细
+     */
+    private Set<PublicInquiryItem> inquiryItems;
+
+    /**
+     * 来源系统单据ID
+     */
+    private Long sourceId;
+
+    /**
+     * 附件
+     */
+    private Set<Attach> attachs;
+
+    /**
+     * 客户已提交 1 为已提交 否则为未提交
+     */
+    private Short check;
+
+    /**
+     * 是否过期 1已过期 非1 未过期
+     */
+    private Short overdue;
+
+    /**
+     * 是否公开<br>
+     * 1、 公共<br>
+     * 0、 不用开
+     */
+    private Short isOpen;
+
+    /**
+     * 应用来源,主要是为了平台公共询价做处理
+     */
+    private String sourceApp;
+
+    /**
+     * 收货地址
+     */
+    private String ship;
+
+    /**
+     * 是否开票<br>
+     * 1. 是<br>
+     * 0. 否
+     */
+    private Short invoice;
+
+    /**
+     * 询价类型
+     */
+    private String inquirytype;
+
+    /**
+     * 询价种类(公共或其他)
+     */
+    private String kind;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Long getEnUU() {
+        return enUU;
+    }
+
+    public void setEnUU(Long enUU) {
+        this.enUU = enUU;
+    }
+
+    public String getEnName() {
+        return enName;
+    }
+
+    public void setEnName(String enName) {
+        this.enName = enName;
+    }
+
+    public Long getRecorderUU() {
+        return recorderUU;
+    }
+
+    public void setRecorderUU(Long recorderUU) {
+        this.recorderUU = recorderUU;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public Date getDate() {
+        return date;
+    }
+
+    public void setDate(Date date) {
+        this.date = date;
+    }
+
+    public String getRecorder() {
+        return recorder;
+    }
+
+    public void setRecorder(String recorder) {
+        this.recorder = recorder;
+    }
+
+    public String getAuditor() {
+        return auditor;
+    }
+
+    public void setAuditor(String auditor) {
+        this.auditor = auditor;
+    }
+
+    public Date getEndDate() {
+        return endDate;
+    }
+
+    public void setEndDate(Date endDate) {
+        this.endDate = endDate;
+    }
+
+    public String getRemark() {
+        return remark;
+    }
+
+    public void setRemark(String remark) {
+        this.remark = remark;
+    }
+
+    public String getEnvironment() {
+        return environment;
+    }
+
+    public void setEnvironment(String environment) {
+        this.environment = environment;
+    }
+
+    public String getPriceType() {
+        return priceType;
+    }
+
+    public void setPriceType(String priceType) {
+        this.priceType = priceType;
+    }
+
+    public Set<PublicInquiryItem> getInquiryItems() {
+        return inquiryItems;
+    }
+
+    public void setInquiryItems(Set<PublicInquiryItem> inquiryItems) {
+        this.inquiryItems = inquiryItems;
+    }
+
+    public Long getSourceId() {
+        return sourceId;
+    }
+
+    public void setSourceId(Long sourceId) {
+        this.sourceId = sourceId;
+    }
+
+    public Set<Attach> getAttachs() {
+        return attachs;
+    }
+
+    public void setAttachs(Set<Attach> attachs) {
+        this.attachs = attachs;
+    }
+
+    public Short getCheck() {
+        return check;
+    }
+
+    public void setCheck(Short check) {
+        this.check = check;
+    }
+
+    public Short getOverdue() {
+        return overdue;
+    }
+
+    public void setOverdue(Short overdue) {
+        this.overdue = overdue;
+    }
+
+    public Short getIsOpen() {
+        return isOpen;
+    }
+
+    public void setIsOpen(Short isOpen) {
+        this.isOpen = isOpen;
+    }
+
+    public String getSourceApp() {
+        return sourceApp;
+    }
+
+    public void setSourceApp(String sourceApp) {
+        this.sourceApp = sourceApp;
+    }
+
+    public String getShip() {
+        return ship;
+    }
+
+    public void setShip(String ship) {
+        this.ship = ship;
+    }
+
+    public Short getInvoice() {
+        return invoice;
+    }
+
+    public void setInvoice(Short invoice) {
+        this.invoice = invoice;
+    }
+
+    public String getInquirytype() {
+        return inquirytype;
+    }
+
+    public void setInquirytype(String inquirytype) {
+        this.inquirytype = inquirytype;
+    }
+
+    public String getKind() {
+        return kind;
+    }
+
+    public void setKind(String kind) {
+        this.kind = kind;
+    }
+}

+ 819 - 0
src/main/java/com/uas/erp/schedular/publicinquiry/domain/PublicInquiryItem.java

@@ -0,0 +1,819 @@
+package com.uas.erp.schedular.publicinquiry.domain;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Set;
+
+public class PublicInquiryItem {
+
+    private Long id;
+
+    /**
+     * 来源(买家ERP采购询价明细)的ID
+     */
+    private Long sourceId;
+
+    /**
+     * 序号
+     */
+    private Short number;
+
+    /**
+     * 询价单
+     */
+    private PublicInquiry inquiry;
+
+    /**
+     * 买家采购员UU
+     */
+    private Long userUU;
+
+    /**
+     * 联系人姓名
+     */
+    private String userName;
+
+    /**
+     * 联系人电话
+     */
+    private String userTel;
+
+    /**
+     * 联系人编号
+     */
+    private String userCode;
+
+    /**
+     * 产品id
+     */
+    private Long productId;
+
+    /**
+     * 币种
+     */
+    private String currency;
+
+    /**
+     * 税率
+     */
+    private Float taxrate;
+
+    /**
+     * 备注
+     */
+    private String remark;
+
+    /**
+     * 供应商UU
+     */
+    private Long vendUU;
+
+    /**
+     * 供应商联系人UU
+     */
+    private Long vendUserUU;
+
+    /**
+     * (买家预先提供的)有效期始
+     */
+    private Date fromDate;
+
+    /**
+     * (买家预先提供的)有效期止
+     */
+    private Date toDate;
+
+    /**
+     * (卖家报的)有效期始
+     */
+    private Date vendFromDate;
+
+    /**
+     * (卖家报的)有效期止
+     */
+    private Date vendToDate;
+
+    /**
+     * (卖家报的)最小订购量
+     */
+    private Double minOrderQty;
+
+    /**
+     * (卖家报的)最小包装量
+     */
+    private Double minPackQty;
+
+    /**
+     * (卖家报的)物料品牌
+     */
+    private String brand;
+
+    /**
+     * (卖家报的)供应商物料编号
+     */
+    private String vendorprodcode;
+
+    /**
+     * (卖家报的)交货周期(天数)
+     */
+    private Long leadtime;
+
+    /**
+     * 分段报价明细
+     */
+    private List<PublicInquiryReply> replies;
+
+    /**
+     *  状态 200 待回复 、201 已回复 、314 已作废
+     */
+    private Short status;
+    /**
+     * (针对卖家的)询价传输状态{待上传、已下载}
+     */
+    private Short sendStatus;
+
+    /**
+     * (针对买家的)报价信息传输状态{待上传、已下载}
+     */
+    private Short backStatus;
+
+    /**
+     * (针对卖家的)报价信息传输状态{待上传、已下载}
+     */
+    private Short replySendStatus;
+
+    /**
+     * 是否采纳
+     */
+    private Short agreed;
+
+
+    /**
+     * 拒绝采纳理由
+     */
+    private String refusereason;
+
+    /**
+     * (针对卖家的)是否采纳信息传输状态{待上传、已下载}
+     */
+    private Short decideStatus;
+
+    /**
+     * (针对卖家的)作废信息传输状态{待上传、已下载}
+     */
+    private Short invalidStatus;
+
+    /**
+     * 是否买家已设置分段数
+     */
+    private Short custLap;
+
+    /**
+     * 保存erp传入数据的时间
+     *
+     * @return
+     */
+    private Date erpDate;
+
+    /**
+     * 录入时间(取主表日期字段)
+     *
+     * @return
+     */
+    private Date date;
+
+    /**
+     *   (1为已过期 0为未过期)
+     */
+    private Short overdue;
+
+    /**
+     * 保存询价的应用
+     */
+    private String source;
+
+    /**
+     * 报价的应用
+     */
+    private String qutoApp;
+
+    /**
+     * 报价附件
+     */
+    private Set<Attach> attaches;
+
+    /**
+     * 需求数量
+     */
+    private Double needquantity;
+
+    /**
+     * erp传输状态
+     *
+     * <pre>erp发出数据传输请求时,赋给状态,完成后更新状态</pre>
+     *
+     * 1、 传输完成<br>
+     * 0、 正在传输
+     *
+     */
+    private Short erpstatus;
+
+    /**
+     * 询价种类
+     */
+    private String kind;
+
+/*这下面是供应商报价时存的相关信息;
+* 因为存在非客户报价,而且公共服务里面没有企业信息,现存入相关字段,后续处理*/
+    /**
+     * 供应商名称
+     */
+    private String vendName;
+
+    /**
+     * 供应商营业执照
+     */
+    private String businessCode;
+
+    /**
+     * 报价时间
+     */
+    private Date offerTime;
+
+    /**
+     * 单价预算
+     */
+    private Double unitPrice;
+
+    /**
+     * 产品生产日期
+     */
+    private String produceDate;
+
+    /**
+     * 封装
+     */
+    private String encapsulation;
+
+    /**
+     * 报价截止日期
+     */
+    private Date endDate;
+
+    /**
+     * ************* 上传的物料信息,物料冗余字段 *************
+     */
+    /**
+     * 物料编号
+     */
+    private String prodCode;
+
+    /**
+     * 名称
+     */
+    private String prodTitle;
+
+    /**
+     * 规格
+     */
+    private String spec;
+
+    /**
+     * 单位
+     */
+    private String unit;
+
+    /**
+     * 型号
+     */
+    private String cmpCode;
+
+    /**
+     * 品牌
+     */
+    private String inbrand;
+
+    /**
+     * ******end*******
+     */
+
+    /**
+     * 审批状态,公共询价单采用明细单独做审批状态
+     */
+    private Short checked;
+
+    /**
+     * 买家发出公共询价单时录入的币别
+     */
+    private String custCurrency;
+
+    /******** 替代物料信息  */
+
+    /**
+     * 替代型号
+     */
+    private String replaceCmpCode;
+
+    /**
+     * 替代规格
+     */
+    private String replaceSpec;
+
+    /**
+     * 替代品牌
+     */
+    private String replaceBrand;
+
+    /**
+     * 是否替代物料报价  1 是替代料报价, 0为普通报价
+     */
+    private Short isReplace = 0;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Long getSourceId() {
+        return sourceId;
+    }
+
+    public void setSourceId(Long sourceId) {
+        this.sourceId = sourceId;
+    }
+
+    public Short getNumber() {
+        return number;
+    }
+
+    public void setNumber(Short number) {
+        this.number = number;
+    }
+
+    public PublicInquiry getInquiry() {
+        return inquiry;
+    }
+
+    public void setInquiry(PublicInquiry inquiry) {
+        this.inquiry = inquiry;
+    }
+
+    public Long getUserUU() {
+        return userUU;
+    }
+
+    public void setUserUU(Long userUU) {
+        this.userUU = userUU;
+    }
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getUserTel() {
+        return userTel;
+    }
+
+    public void setUserTel(String userTel) {
+        this.userTel = userTel;
+    }
+
+    public String getUserCode() {
+        return userCode;
+    }
+
+    public void setUserCode(String userCode) {
+        this.userCode = userCode;
+    }
+
+    public Long getProductId() {
+        return productId;
+    }
+
+    public void setProductId(Long productId) {
+        this.productId = productId;
+    }
+
+    public String getCurrency() {
+        return currency;
+    }
+
+    public void setCurrency(String currency) {
+        this.currency = currency;
+    }
+
+    public Float getTaxrate() {
+        return taxrate;
+    }
+
+    public void setTaxrate(Float taxrate) {
+        this.taxrate = taxrate;
+    }
+
+    public String getRemark() {
+        return remark;
+    }
+
+    public void setRemark(String remark) {
+        this.remark = remark;
+    }
+
+    public Long getVendUU() {
+        return vendUU;
+    }
+
+    public void setVendUU(Long vendUU) {
+        this.vendUU = vendUU;
+    }
+
+    public Long getVendUserUU() {
+        return vendUserUU;
+    }
+
+    public void setVendUserUU(Long vendUserUU) {
+        this.vendUserUU = vendUserUU;
+    }
+
+    public Date getFromDate() {
+        return fromDate;
+    }
+
+    public void setFromDate(Date fromDate) {
+        this.fromDate = fromDate;
+    }
+
+    public Date getToDate() {
+        return toDate;
+    }
+
+    public void setToDate(Date toDate) {
+        this.toDate = toDate;
+    }
+
+    public Date getVendFromDate() {
+        return vendFromDate;
+    }
+
+    public void setVendFromDate(Date vendFromDate) {
+        this.vendFromDate = vendFromDate;
+    }
+
+    public Date getVendToDate() {
+        return vendToDate;
+    }
+
+    public void setVendToDate(Date vendToDate) {
+        this.vendToDate = vendToDate;
+    }
+
+    public Double getMinOrderQty() {
+        return minOrderQty;
+    }
+
+    public void setMinOrderQty(Double minOrderQty) {
+        this.minOrderQty = minOrderQty;
+    }
+
+    public Double getMinPackQty() {
+        return minPackQty;
+    }
+
+    public void setMinPackQty(Double minPackQty) {
+        this.minPackQty = minPackQty;
+    }
+
+    public String getBrand() {
+        return brand;
+    }
+
+    public void setBrand(String brand) {
+        this.brand = brand;
+    }
+
+    public String getVendorprodcode() {
+        return vendorprodcode;
+    }
+
+    public void setVendorprodcode(String vendorprodcode) {
+        this.vendorprodcode = vendorprodcode;
+    }
+
+    public Long getLeadtime() {
+        return leadtime;
+    }
+
+    public void setLeadtime(Long leadtime) {
+        this.leadtime = leadtime;
+    }
+
+    public List<PublicInquiryReply> getReplies() {
+        return replies;
+    }
+
+    public void setReplies(List<PublicInquiryReply> replies) {
+        this.replies = replies;
+    }
+
+    public Short getStatus() {
+        return status;
+    }
+
+    public void setStatus(Short status) {
+        this.status = status;
+    }
+
+    public Short getSendStatus() {
+        return sendStatus;
+    }
+
+    public void setSendStatus(Short sendStatus) {
+        this.sendStatus = sendStatus;
+    }
+
+    public Short getBackStatus() {
+        return backStatus;
+    }
+
+    public void setBackStatus(Short backStatus) {
+        this.backStatus = backStatus;
+    }
+
+    public Short getReplySendStatus() {
+        return replySendStatus;
+    }
+
+    public void setReplySendStatus(Short replySendStatus) {
+        this.replySendStatus = replySendStatus;
+    }
+
+    public Short getAgreed() {
+        return agreed;
+    }
+
+    public void setAgreed(Short agreed) {
+        this.agreed = agreed;
+    }
+
+    public String getRefusereason() {
+        return refusereason;
+    }
+
+    public void setRefusereason(String refusereason) {
+        this.refusereason = refusereason;
+    }
+
+    public Short getDecideStatus() {
+        return decideStatus;
+    }
+
+    public void setDecideStatus(Short decideStatus) {
+        this.decideStatus = decideStatus;
+    }
+
+    public Short getInvalidStatus() {
+        return invalidStatus;
+    }
+
+    public void setInvalidStatus(Short invalidStatus) {
+        this.invalidStatus = invalidStatus;
+    }
+
+    public Short getCustLap() {
+        return custLap;
+    }
+
+    public void setCustLap(Short custLap) {
+        this.custLap = custLap;
+    }
+
+    public Date getErpDate() {
+        return erpDate;
+    }
+
+    public void setErpDate(Date erpDate) {
+        this.erpDate = erpDate;
+    }
+
+    public Date getDate() {
+        return date;
+    }
+
+    public void setDate(Date date) {
+        this.date = date;
+    }
+
+    public Short getOverdue() {
+        return overdue;
+    }
+
+    public void setOverdue(Short overdue) {
+        this.overdue = overdue;
+    }
+
+    public String getSource() {
+        return source;
+    }
+
+    public void setSource(String source) {
+        this.source = source;
+    }
+
+    public String getQutoApp() {
+        return qutoApp;
+    }
+
+    public void setQutoApp(String qutoApp) {
+        this.qutoApp = qutoApp;
+    }
+
+    public Set<Attach> getAttaches() {
+        return attaches;
+    }
+
+    public void setAttaches(Set<Attach> attaches) {
+        this.attaches = attaches;
+    }
+
+    public Double getNeedquantity() {
+        return needquantity;
+    }
+
+    public void setNeedquantity(Double needquantity) {
+        this.needquantity = needquantity;
+    }
+
+    public Short getErpstatus() {
+        return erpstatus;
+    }
+
+    public void setErpstatus(Short erpstatus) {
+        this.erpstatus = erpstatus;
+    }
+
+    public String getKind() {
+        return kind;
+    }
+
+    public void setKind(String kind) {
+        this.kind = kind;
+    }
+
+    public String getVendName() {
+        return vendName;
+    }
+
+    public void setVendName(String vendName) {
+        this.vendName = vendName;
+    }
+
+    public String getBusinessCode() {
+        return businessCode;
+    }
+
+    public void setBusinessCode(String businessCode) {
+        this.businessCode = businessCode;
+    }
+
+    public Date getOfferTime() {
+        return offerTime;
+    }
+
+    public void setOfferTime(Date offerTime) {
+        this.offerTime = offerTime;
+    }
+
+    public Double getUnitPrice() {
+        return unitPrice;
+    }
+
+    public void setUnitPrice(Double unitPrice) {
+        this.unitPrice = unitPrice;
+    }
+
+    public String getProduceDate() {
+        return produceDate;
+    }
+
+    public void setProduceDate(String produceDate) {
+        this.produceDate = produceDate;
+    }
+
+    public String getEncapsulation() {
+        return encapsulation;
+    }
+
+    public void setEncapsulation(String encapsulation) {
+        this.encapsulation = encapsulation;
+    }
+
+    public Date getEndDate() {
+        return endDate;
+    }
+
+    public void setEndDate(Date endDate) {
+        this.endDate = endDate;
+    }
+
+    public String getProdCode() {
+        return prodCode;
+    }
+
+    public void setProdCode(String prodCode) {
+        this.prodCode = prodCode;
+    }
+
+    public String getProdTitle() {
+        return prodTitle;
+    }
+
+    public void setProdTitle(String prodTitle) {
+        this.prodTitle = prodTitle;
+    }
+
+    public String getSpec() {
+        return spec;
+    }
+
+    public void setSpec(String spec) {
+        this.spec = spec;
+    }
+
+    public String getUnit() {
+        return unit;
+    }
+
+    public void setUnit(String unit) {
+        this.unit = unit;
+    }
+
+    public String getCmpCode() {
+        return cmpCode;
+    }
+
+    public void setCmpCode(String cmpCode) {
+        this.cmpCode = cmpCode;
+    }
+
+    public String getInbrand() {
+        return inbrand;
+    }
+
+    public void setInbrand(String inbrand) {
+        this.inbrand = inbrand;
+    }
+
+    public Short getChecked() {
+        return checked;
+    }
+
+    public void setChecked(Short checked) {
+        this.checked = checked;
+    }
+
+    public String getCustCurrency() {
+        return custCurrency;
+    }
+
+    public void setCustCurrency(String custCurrency) {
+        this.custCurrency = custCurrency;
+    }
+
+    public String getReplaceCmpCode() {
+        return replaceCmpCode;
+    }
+
+    public void setReplaceCmpCode(String replaceCmpCode) {
+        this.replaceCmpCode = replaceCmpCode;
+    }
+
+    public String getReplaceSpec() {
+        return replaceSpec;
+    }
+
+    public void setReplaceSpec(String replaceSpec) {
+        this.replaceSpec = replaceSpec;
+    }
+
+    public String getReplaceBrand() {
+        return replaceBrand;
+    }
+
+    public void setReplaceBrand(String replaceBrand) {
+        this.replaceBrand = replaceBrand;
+    }
+
+    public Short getIsReplace() {
+        return isReplace;
+    }
+
+    public void setIsReplace(Short isReplace) {
+        this.isReplace = isReplace;
+    }
+}

+ 43 - 0
src/main/java/com/uas/erp/schedular/publicinquiry/domain/PublicInquiryReply.java

@@ -0,0 +1,43 @@
+package com.uas.erp.schedular.publicinquiry.domain;
+
+public class PublicInquiryReply {
+
+    /**
+     * id
+     */
+    private Long id;
+
+    /**
+     * (买家或卖家定义的)分段数量
+     */
+    private Double lapQty;
+
+    /**
+     * (卖家报的)单价
+     */
+    private Double price;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Double getLapQty() {
+        return lapQty;
+    }
+
+    public void setLapQty(Double lapQty) {
+        this.lapQty = lapQty;
+    }
+
+    public Double getPrice() {
+        return price;
+    }
+
+    public void setPrice(Double price) {
+        this.price = price;
+    }
+}

+ 229 - 3
src/main/java/com/uas/erp/schedular/publicinquiry/task/EnterInquiryTask.java

@@ -1,11 +1,20 @@
 package com.uas.erp.schedular.publicinquiry.task;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.uas.erp.schedular.b2b.domain.QuotationDownDecide;
 import com.uas.erp.schedular.database.RestJdbcTemplate;
+import com.uas.erp.schedular.publicinquiry.domain.PublicInquiryItem;
+import com.uas.erp.schedular.publicinquiry.domain.PublicInquiryReply;
 import com.uas.erp.schedular.task.support.TaskMapping;
+import com.uas.erp.schedular.util.CollectionUtil;
+import com.uas.erp.schedular.util.ContextHolder;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
 
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
@@ -16,11 +25,221 @@ import java.util.*;
 
 @Component
 @TaskMapping(title = "商机")
-public class EnterInquiryTask {
+public class EnterInquiryTask extends AbstractTask{
 
     @Autowired
     private RestJdbcTemplate jdbcTemplate;
 
+    @TaskMapping(title = "从公共服务下载买家对报价信息是否采纳的决策", fixedDelay = 60000)
+    public void dowmInquiryDecide(){
+        int enUU = jdbcTemplate.getInt("select en_uu from enterprise");
+        List<PublicInquiryItem> downs = getForList("/inquiry/public/down/decide?enUU="+enUU, PublicInquiryItem.class);
+        if(!CollectionUtils.isEmpty(downs)){
+            ContextHolder.setDataSize(downs.size());
+            saveQuotationDecide(downs);
+            // 回执
+            onDownloadReplyDecide(downs, enUU);
+        }
+    }
+
+    /**
+     * 保存从公共服务下载的采纳结果
+     */
+    private void saveQuotationDecide(List<PublicInquiryItem> downs) {
+        List<String> sqls = new ArrayList<String>();
+        for (PublicInquiryItem decide : downs) {
+            Integer id = jdbcTemplate.getInt("select * from quotationdown where b2b_id_id = ?", decide.getId());
+            if (null != id) {
+                sqls.add("update QuotationDown set qu_agreed=" + decide.getAgreed() + " where qu_id=" + id);
+            }
+        }
+        jdbcTemplate.batchExecute(sqls);
+    }
+
+    /**
+     * 将从公共服务下载的采纳结果保存成功之后,回执给平台
+     * @param downs
+     */
+    private void onDownloadReplyDecide(List<PublicInquiryItem> downs, Integer enUU) {
+        String idStr = CollectionUtil.getKeyString(downs, new CollectionUtil.KeyIterator<PublicInquiryItem>() {
+            @Override
+            public Object getKey(PublicInquiryItem obj) {
+                return obj.getId();
+            }
+        });
+        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
+        map.set("enUU", String.valueOf(enUU));
+        map.set("ids", idStr);
+        post("/inquiry/public/down/decide/back", map);
+    }
+
+    /**
+     * 将报价方在商城(或B2B)报价的单据同步到报价方UAS
+     */
+    @TaskMapping(title = "将报价方在商城(或B2B)报价的单据同步到报价方UAS", fixedDelay = 60000)
+    public void downInquiryReply(){
+        String enUU = jdbcTemplate.getString("select en_uu from enterprise");
+        String userUU = jdbcTemplate.getString("select en_adminuu from enterprise");
+        List<PublicInquiryItem> data = getForList("/inquiry/public/down/reply?enUU="+enUU, PublicInquiryItem.class);
+        for(PublicInquiryItem publicInquiryItem : data){
+            int count = jdbcTemplate.getInt("select count(*) from businesschance where bc_fromid = ?", publicInquiryItem.getSourceId());
+            if(count == 0){     //若UAS未转企业商机
+                turnBusinesschange(publicInquiryItem, enUU, userUU);
+                //回传给平台,进行商机分配
+                MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
+                map.set("userUU", userUU);
+                map.set("enUU", enUU);
+                map.set("itemId", String.valueOf(publicInquiryItem.getId()));
+                post("inquiry/sale/allot/enremind", map);
+            }else{      //若UAS已转企业商机
+                Map<String, Object> userMap = jdbcTemplate.queryForMap("select em_code,em_name from employee where em_uu=?", publicInquiryItem.getVendUserUU());
+                if(userMap != null){
+                    count = jdbcTemplate.getInt("select count(*) from businesschance where bc_fromid = ? and bc_domancode=? and bc_doman=?",
+                            publicInquiryItem.getSourceId(),userMap.get("EM_CODE"),userMap.get("EM_NAME"));
+                    if(count == 0){     //商机跟进人不同
+                        turnBusinesschange(publicInquiryItem, enUU, userUU);
+                    }
+                }
+            }
+            //生成UAS的客户采购询价单
+            count = jdbcTemplate.getInt("select count(*) from QuotationDown where b2b_id_id=?", data.get(0).getSourceId());
+            if(count == 0){
+                turnQuotationDown(publicInquiryItem);
+            }
+        }
+
+    }
+
+    /**
+     * 生成UAS的客户采购询价单
+     * @param publicInquiryItem
+     */
+    private void turnQuotationDown(PublicInquiryItem publicInquiryItem) {
+        StringBuilder sb = new StringBuilder();
+        int id = jdbcTemplate.generate("QuotationDown_seq");
+        String code = jdbcTemplate.generateCode("QuotationDown", 2);
+        String taxrate = String.valueOf(publicInquiryItem.getTaxrate());
+        if(taxrate == null || "null".equals(taxrate) || "".equals(taxrate)){
+            taxrate = "0.0";
+        }
+        String prodTitle = publicInquiryItem.getProdTitle();
+        String prodCode = publicInquiryItem.getProdCode();
+        String prodSpec = publicInquiryItem.getSpec();
+        String custLap = String.valueOf(publicInquiryItem.getCustLap());
+        String fromDate = "to_date('"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(publicInquiryItem.getFromDate())+"','YYYY-MM-DD HH24:MI:SS')";
+        String endDate = "to_date('"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(publicInquiryItem.getToDate())+"','YYYY-MM-DD HH24:MI:SS')";
+        String custCode = jdbcTemplate.getString("select cu_code from customer where cu_uu=?", publicInquiryItem.getInquiry().getEnUU());
+        sb.append("insert into QuotationDown(qu_id,b2b_id_id,qu_code,qu_detno,qu_date,qu_enddate,qu_custuu,qu_custcode,qu_custname,qu_status,qu_statuscode,qu_custprodcode,qu_custproddetail,qu_custprodspec,qu_custlap," +
+                "qu_currency,qu_taxrate,qu_source,qu_isreplace,qu_prodcode) values("+id+","+publicInquiryItem.getSourceId()+","+code+",1,"+fromDate+","+endDate+",'"+publicInquiryItem.getInquiry().getEnUU()+"'," +
+                "'"+custCode+"','"+publicInquiryItem.getInquiry().getEnName()+"','已审核','AUDITED',");
+        if(prodCode == null || "null".equals(prodCode)){
+            sb.append("null,");
+        }else{
+            sb.append("'"+prodCode+"',");
+        }
+        if(prodTitle == null || "null".equals(prodTitle)){
+            sb.append("null,");
+        }else{
+            sb.append("'"+prodTitle+"',");
+        }
+        if(prodSpec == null || "null".equals(prodSpec)){
+            sb.append("null,");
+        }else{
+            sb.append("'"+prodSpec+"',");
+        }
+        if(custLap == null || "null".equals(custLap)){
+            sb.append("null,");
+        }else{
+            sb.append("'"+custLap+"',");
+        }
+        String vendorProdCode = publicInquiryItem.getVendorprodcode();
+        sb.append("'"+publicInquiryItem.getCurrency()+"',"+taxrate+",'平台商机',0,");
+        if(vendorProdCode == null || "null".equals(vendorProdCode)){
+            sb.append("null)");
+        }else{
+            sb.append("'"+vendorProdCode+"')");
+        }
+        //System.out.println(sb.toString());
+        jdbcTemplate.execute(sb.toString());
+        //插入报价明细
+        List<PublicInquiryReply> replies = publicInquiryItem.getReplies();
+        List<String> sqls = new ArrayList<String>();
+        for(PublicInquiryReply reply : replies){
+            String sql = "insert into QuotationDownDetail(qd_id,qd_quid,qd_lapqty,qd_price) values(QuotationDownDetail_seq.nextval,"+id+",'"+reply.getLapQty()+"','"+reply.getPrice()+"')";
+            sqls.add(sql);
+        }
+        //System.out.println(sqls);
+        jdbcTemplate.batchExecute(sqls);
+    }
+
+    /**
+     * 生成企业内部商机
+     * @param publicInquiryItem
+     * @param enUU
+     * @param userUU
+     */
+    private void turnBusinesschange(PublicInquiryItem publicInquiryItem, String enUU, String userUU) {
+        StringBuilder sb = new StringBuilder();
+        String code = jdbcTemplate.generateCode("businesschance",2);
+        String needQty ="";
+        String inbrand = "";
+        inbrand = publicInquiryItem.getInbrand();
+        needQty = String.valueOf(publicInquiryItem.getNeedquantity());
+        if(needQty == null || "null".equals(needQty)){
+            needQty = "";
+        }
+        if(inbrand == null || "null".equals(inbrand)){
+            inbrand = "";
+        }
+        //商机名称
+        String description = "";
+        String prodTitle = publicInquiryItem.getProdTitle();
+        if(prodTitle != null && !"null".equals(prodTitle) && !"".equals(prodTitle)){
+            prodTitle = prodTitle.replaceAll("'","''").replaceAll("&","'||'&'||'");
+            description += publicInquiryItem.getProdTitle();
+        }else{
+            prodTitle = "";
+        }
+        String cmpCode = publicInquiryItem.getCmpCode();
+        if(cmpCode != null && !"null".equals(cmpCode) && !"".equals(cmpCode)){
+            cmpCode = cmpCode.replaceAll("&","'||'&'||'");
+            description += publicInquiryItem.getCmpCode();
+        }else{
+            cmpCode = "";
+        }
+        if(publicInquiryItem.getSpec() != null && !"null".equals(publicInquiryItem.getSpec()) && !"".equals(publicInquiryItem.getSpec())){
+            description += publicInquiryItem.getSpec() +" ";
+        }else{
+            publicInquiryItem.setSpec("");
+        }
+        description += inbrand;
+        if(needQty != null && !"null".equals(needQty) && !"".equals(needQty)){
+            description += ",需求:"+needQty;
+        }
+        //客户编号、客户uu
+        Long cuUU = publicInquiryItem.getInquiry().getEnUU();
+        String custCode = jdbcTemplate.getString("select cu_code from customer where cu_uu=?", cuUU);
+        if(custCode == null || "null".equals(custCode)){
+            custCode = "";
+        }
+        String date = "to_date('" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(publicInquiryItem.getFromDate()) + "','YYYY-MM-DD HH24:MI:SS')";
+        String endDate = "to_date('" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(publicInquiryItem.getToDate()) + "','YYYY-MM-DD HH24:MI:SS')";
+        //跟进人、跟进人编号
+        Map<String, Object> userMap = jdbcTemplate.queryForMap("select em_code,em_name from employee where em_uu=?", publicInquiryItem.getVendUserUU());
+        sb.append("insert into businesschance(bc_id,bc_fromid,bc_code,bc_description,bc_from,bc_nichehouse,bc_currentprocess,bc_date7,bc_status,bc_statuscode,bc_recorder,bc_recorddate,"
+                + "bc_custname,bc_contact,bc_tel,bc_desc7,bc_desc9,bc_desc10,bc_desc11,bc_desc13,bc_date13,bc_desc14,bc_custcode,bc_domancode,bc_doman) values(businesschance_seq.nextval,"+publicInquiryItem.getSourceId()+",'"+code+"','"+description.replaceAll("'","''").replaceAll("&","'||'&'||'")+"','平台商机','平台商机库','样品报价',"+date+","
+                + "'已审核','AUDITED','自动转入',sysdate,'"+publicInquiryItem.getInquiry().getEnName()+"','"+publicInquiryItem.getInquiry().getRecorder()+"',null,'"+prodTitle+"','"+publicInquiryItem.getSpec()+"',"
+                + "'"+cmpCode+"','"+inbrand+"','"+needQty+"',"+endDate+",'"+cuUU+"','"+custCode);
+        if(userMap != null){
+            sb.append("','"+userMap.get("EM_CODE")+"','"+userMap.get("EM_NAME")+"')");
+        }else{
+            sb.append("',null,null)");
+        }
+        jdbcTemplate.execute(sb.toString());
+        //System.out.println(sb.toString());
+    }
+
+
     @TaskMapping(title = "商机下载", fixedDelay = 60000)
     public void downloadInquiry(){
         long total = 0L;
@@ -112,6 +331,10 @@ public class EnterInquiryTask {
                 inbrand = "";
             }
             //商机名称
+            String prodTitle = json.getString("prodTitle");
+            if(prodTitle != null && !"null".equals(prodTitle) && !"".equals(prodTitle)){
+                prodTitle = prodTitle.replaceAll("'","''").replaceAll("&","'||'&'||'");
+            }
             String description = json.getString("prodTitle")+" "+ json.getString("cmpCode")+" ";
             if(json.getString("spec") != null && !"null".equals(json.getString("spec")) && !"".equals(json.getString("spec"))){
                 description += json.getString("spec") +" ";
@@ -123,14 +346,17 @@ public class EnterInquiryTask {
             //客户编号、客户uu
             String cuUU = json.getString("enUU");
             String custCode = jdbcTemplate.getString("select cu_code from customer where cu_uu=?", cuUU);
+            if(custCode == null || "null".equals(custCode)){
+                custCode = "";
+            }
             Calendar c = Calendar.getInstance();
             c.setTime(new Date(json.getLong("date")));
             c.add(Calendar.MONTH, 2);
             String date = "to_date('" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime())+"','YYYY-MM-DD HH24:MI:SS')";
             String endDate = "to_date('"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(json.getLong("endDate")))+"','YYYY-MM-DD HH24:MI:SS')";
             sb.append("insert into businesschance(bc_id,bc_fromid,bc_code,bc_description,bc_from,bc_nichehouse,bc_currentprocess,bc_date7,bc_status,bc_statuscode,bc_recorder,bc_recorddate,"
-                    + "bc_custname,bc_contact,bc_tel,bc_desc7,bc_desc9,bc_desc10,bc_desc11,bc_desc13,bc_date13,bc_desc14,bc_custcode) values(businesschance_seq.nextval,"+json.getInteger("id")+",'"+code+"','"+description.replaceAll("'","''").replaceAll("&","'||'&'||'")+"','平台商机','"+bdName+"','"+bsName+"',"+date+","
-                    + "'已审核','AUDITED','自动转入',sysdate,'"+json.getString("enName")+"','"+json.getString("userName")+"','"+json.getString("userTel")+"','"+json.getString("prodTitle").replaceAll("'","''").replaceAll("&","'||'&'||'")+"','"+json.getString("spec")+"',"
+                    + "bc_custname,bc_contact,bc_tel,bc_desc7,bc_desc9,bc_desc10,bc_desc11,bc_desc13,bc_date13,bc_desc14,bc_custcode) values(businesschance_seq.nextval,"+json.getInteger("itemId")+",'"+code+"','"+description.replaceAll("'","''").replaceAll("&","'||'&'||'")+"','平台商机','"+bdName+"','"+bsName+"',"+date+","
+                    + "'已审核','AUDITED','自动转入',sysdate,'"+json.getString("enName")+"','"+json.getString("userName")+"','"+json.getString("userTel")+"','"+prodTitle+"','"+json.getString("spec")+"',"
                     + "'"+json.getString("cmpCode").replaceAll("&","'||'&'||'")+"','"+json.getString("inbrand")+"','"+needQty+"',"+endDate+",'"+cuUU+"','"+custCode+"');");
             sb.append(" end if;");
         }