ermcp.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/1/5 15:15
  4. * @Modify : 2021/1/5 15:15
  5. */
  6. // 企业风险管理
  7. package models
  8. import (
  9. "fmt"
  10. "mtp2_if/db"
  11. "mtp2_if/logger"
  12. "mtp2_if/mtpcache"
  13. "mtp2_if/utils"
  14. )
  15. // ErmcpSpotContractModel 现货合同结构(对应现货合同菜单)
  16. type ErmcpSpotContractModel struct {
  17. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 现货合同ID(602+Unix秒时间戳(10位)+xxxxxx)
  18. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // [合同编号]现货合同编号
  19. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // [合同类型]现货合同类型 - 1:采购 -1:销售
  20. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  21. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方ID
  22. BUYUSERName string `json:"buyusername" xorm:"'BUYUSERName'"` // 采购方名称
  23. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方ID
  24. SELLUSERNAME string `json:"sellusername" xorm:"'SELLUSERNAME'"` // 销售方名称
  25. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  26. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'wrstandardid'"` // 现货商品ID
  27. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // [现货商品]现货商品名称
  28. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  29. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  30. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  31. PRODUCTTYPE int32 `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  32. CONVERTFACTOR float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数
  33. SPOTGOODSDESC string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` // 商品型号
  34. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  35. QTY float64 `json:"qty" xorm:"'QTY'"` // [合同数量]数量
  36. PRICE float64 `json:"price" xorm:"'PRICE'"` // 价格\暂定价 [1:一口价、3:暂定价]
  37. AMOUNT float64 `json:"amount" xorm:"'AMOUNT'"` // 金额 [1:一口价、3:暂定价]
  38. DELIVERYSTARTDATE string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` // 交收期(开始)
  39. DELIVERYENDDATE string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` // 交收期(结束)
  40. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 点价合约ID - 0:为现货,其它为期货商品合约ID [2:点价 3:暂定价]
  41. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 点价合约代码
  42. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 点价商品名称
  43. PRICEMOVE float64 `json:"pricemove" xorm:"'PRICEMOVE'"` // 升贴水 [2:点价 3:暂定价]
  44. STARTDATE string `json:"startdate" xorm:"'STARTDATE'"` // 点价开始日期 [2:点价 3:暂定价]
  45. ENDDATE string `json:"enddate" xorm:"'ENDDATE'"` // 点价结束日期 [2:点价 3:暂定价]
  46. MARGIN float64 `json:"margin" xorm:"'MARGIN'"` // 当前保证金
  47. CONTRACTMARGIN float64 `json:"contractmargin" xorm:"'CONTRACTMARGIN'"` // 合同保证金
  48. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  49. CONTRACCTSTATUS int32 `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  50. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  51. AUDITREMARK string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核意见
  52. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  53. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  54. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  55. SPOTGOODSMODELID int32 `json:"spotgoodsmodelid" xorm:"'SPOTGOODSMODELID'"` // 现货型号ID
  56. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  57. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  58. MODELNAME string `json:"modelname" xorm:"'modelname'"` // 型号名称
  59. ATTACHMENT string `json:"attachment" xorm:"'ATTACHMENT'"` // 附件
  60. PRICEDQTY float64 `json:"pricedqty" xorm:"PRICEDQTY"` // 已定价量
  61. PRICEDAMOUNT float64 `json:"pricedamount" xorm:"PRICEDAMOUNT"` // 已定价金额 = (点价价格+升贴水) * 数量
  62. SPOTAMOUNT float64 `json:"spotamount" xorm:"SPOTAMOUNT"` // 合同现货金额 [不含升贴水]
  63. TRADEUSERID int64 `json:"tradeuserid" xorm:"TRADEUSERID"` // 交易用户ID
  64. ISRELATED int32 `json:"isrelated" xorm:"ISRELATED"` // 是否已关联计划项目 - 0:否 1:是
  65. ISRELATED2 int32 `json:"isrelated2" xorm:"ISRELATED2"` // 是否已关联现货项目 - 0:否 1:是
  66. AccountName string `json:"accountName" xorm:"ACCOUNTNAME"` // 套期主体
  67. UNPRICEDQTY float64 `json:"unpricedqty" xorm:"UNPRICEDQTY"` // 未定价量 = 合同数量 - 已定价量
  68. AVGPRICE float64 `json:"avgprice" xorm:"AVGPRICE"` // 定价均价 = 已定价金额 / 已定价量
  69. SPOTAVGPRICE float64 `json:"spotavgprice" xorm:"SPOTAVGPRICE"` // 现货均价 = 合同现货金额 / 已定价量
  70. }
  71. func (r *ErmcpSpotContractModel) buildSql(nQueryType int32) string {
  72. str := "SELECT to_char(t.SPOTCONTRACTID) SPOTCONTRACTID," +
  73. " t.CONTRACTNO," +
  74. " t.CONTRACTTYPE," +
  75. " t.USERID," +
  76. " t.BUYUSERID," +
  77. " t.SELLUSERID," +
  78. " t.DELIVERYGOODSID," +
  79. " t.wrstandardid," +
  80. " t.PRODUCTTYPE," +
  81. " t.CONVERTFACTOR," +
  82. " t.SPOTGOODSDESC," +
  83. " t.PRICETYPE," +
  84. " t.QTY," +
  85. " t.PRICE," +
  86. " t.AMOUNT," +
  87. " to_char(t.DELIVERYSTARTDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYSTARTDATE," +
  88. " to_char(t.DELIVERYENDDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYENDDATE," +
  89. " t.GOODSID," +
  90. " t.PRICEMOVE," +
  91. " to_char(t.STARTDATE, 'yyyy-mm-dd hh24:mi:ss') STARTDATE," +
  92. " to_char(t.ENDDATE, 'yyyy-mm-dd hh24:mi:ss') ENDDATE," +
  93. " t.MARGIN," +
  94. " t.contractmargin," +
  95. " t.remark," +
  96. " t.auditremark," +
  97. " to_char(t.contractattachment) attachment," +
  98. " t.contractstatus," +
  99. " t.SPOTGOODSMODELID," +
  100. " t.SPOTGOODSBRANDID," +
  101. " to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime," +
  102. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  103. " to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime," +
  104. " u1.accountname BuyUserName," +
  105. " u2.accountname SellUserName," +
  106. " g.deliverygoodscode," +
  107. " g.deliverygoodsname," +
  108. " g2.goodscode," +
  109. " g2.goodsname," +
  110. " gb.brandname," +
  111. " gm.modelname," +
  112. " e.Enumdicname," +
  113. " w.wrstandardname," +
  114. " w.wrstandardcode" +
  115. " (t.QTY - t.PRICEDQTY) UNPRICEDQTY" +
  116. " DECODE(t.PRICEDQTY, 0, 0, t.PRICEDAMOUNT/t.PRICEDQTY) AVGPRICE" +
  117. " DECODE(t.PRICEDQTY, 0, 0, t.SPOTAMOUNT/t.PRICEDQTY) SPOTAVGPRICE" +
  118. " ua.ACCOUNTNAME," +
  119. " FROM ERMCP_SPOTCONTRACT t" +
  120. " left join useraccount u1" +
  121. " on t.buyuserid = u1.userid" +
  122. " left join useraccount u2" +
  123. " on t.selluserid = u2.userid" +
  124. " left join deliverygoods g" +
  125. " on t.deliverygoodsid = g.deliverygoodsid" +
  126. " left join goods g2" +
  127. " on t.goodsid = g2.goodsid" +
  128. " left join spotgoodsbrand gb on t.spotgoodsbrandid=gb.brandid" +
  129. " left join spotgoodsmodel gm on t.spotgoodsmodelid=gm.modelid" +
  130. " left join wrstandard w" +
  131. " on t.wrstandardid = w.wrstandardid" +
  132. " left join enumdicitem e" +
  133. " on w.unitid = e.enumitemname" +
  134. " and e.enumdiccode = 'goodsunit'" +
  135. " left join useraccount ua" +
  136. " on ua.userid = t.tradeuserid" +
  137. " where t.contractstatus in (%v)" +
  138. " and t.userid = %v"
  139. // 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
  140. // 合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  141. var status string
  142. switch nQueryType {
  143. case 1: // 未提交
  144. status = "0,4,6"
  145. str += " order by t.updatetime desc"
  146. case 2: // 待审核
  147. status = "1"
  148. str += " order by t.updatetime desc"
  149. case 3: // 履约中
  150. status = "2"
  151. str += " order by t.audittime desc"
  152. case 4: // 已完成
  153. status = "3,5"
  154. str += " order by t.updatetime desc"
  155. }
  156. return fmt.Sprintf(str, status, r.USERID)
  157. }
  158. // GetData 从数据库中查询现货合同
  159. func (r *ErmcpSpotContractModel) GetData(nQueryType int32) ([]ErmcpSpotContractModel, error) {
  160. sData := make([]ErmcpSpotContractModel, 0)
  161. e := db.GetEngine()
  162. s := e.SQL(r.buildSql(nQueryType))
  163. if err := s.Find(&sData); err != nil {
  164. logger.GetLogger().Errorf("ermcp query fail:%v", err)
  165. return sData, err
  166. }
  167. return sData, nil
  168. }
  169. // ErmcpModel 风险管理合同(对应采购和销售菜单)
  170. type ErmcpModel struct {
  171. UserID int64 `json:"userid" xorm:"'UserID'"` // 用户ID
  172. SpotContractId string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 合同ID
  173. AccountName string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 账户名称
  174. DeliveryGoodsId int `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品ID
  175. DeliveryGoodsCode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  176. DeliveryGoodsName string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  177. SpotGoodsdesc string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` // 商品型号(商品规格)
  178. GoodsId int `json:"goodsid" xorm:"'GOODSID'"` // 点价商品ID
  179. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 点价商品名称
  180. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 点价商品代码
  181. Pricemove float64 `json:"pricemove" xorm:"'PRICEMOVE'"` // 升贴水
  182. Qty float64 `json:"qty" xorm:"'QTY'"` // 合同量
  183. UnpricedQty float64 `json:"unpricedqty" xorm:"'UNPRICEDQTY'"` // 未定价量
  184. PricedQty float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  185. UnsureQty float64 `json:"unsureqty" xorm:"'UNSUREQTY'"` // 未确定量
  186. PayAmount float64 `json:"payamount" xorm:"'PAYAMOUNT'"` // 已收付额(收款或付款)
  187. UnpayAmount float64 `json:"unpayamount"` // 应收付款额(应支付或应收款)
  188. PrePayAmount float64 `json:"prepayamount"` // 预收付额
  189. InvoiceAmount float64 `json:"invoiceamount" xorm:"'INVOICEAMOUNT'"` // 已开票额
  190. DaikaiAmount float64 `json:"daikaiamount" xorm:"'DAIKAIAMOUNT'"` // 应收(开)票额
  191. PreInvoiceAmount float64 `json:"preinvoiceamount"` // 预收(开)票额
  192. StartDate string `json:"startdate" xorm:"'STARTDATE'"` // 点价开始日
  193. EndDate string `json:"enddate" xorm:"'ENDDATE'"` // 点价结束日
  194. DeliveryStartDate string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` // 交割开始日
  195. DeliveryendDate string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` // 交割结束日
  196. Convertfactor float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数
  197. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  198. Contracctstatus uint `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态- 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  199. PriceType int `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  200. ProductType int `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  201. Contracttype int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 合同类型 1-采购, -1-销售
  202. Pricedamount float64 `json:"pricedamount" xorm:"'PRICEDAMOUNT'"` // 已定价额
  203. PricedAvg float64 `json:"pricedavg" xorm:"'PRICEDAVG'"` // 已点均价
  204. Margin float64 `json:"margin" xorm:"'MARGIN'"` // 保证金
  205. CONTRACTMARGIN float64 `json:"contractmargin" xorm:"'CONTRACTMARGIN'"` // 合同保证金
  206. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  207. ATTACHMENT string `json:"attachment" xorm:"'ATTACHMENT'"` // 附件
  208. ReckonRealQty float64 `json:"reckonrealqty" xorm:"'ReckonRealQty'"` // 已确定量
  209. ReckonOtherAmount float64 `json:"reckonotheramount" xorm:"'ReckonOtherAmount'"` // 其它费用
  210. ReckonAdjustAmount float64 `json:"reckonadjustamount" xorm:"'ReckonAdjustAmount'"` // 调整金额
  211. Price float64 `json:"price" xorm:"'Price'"` // 价格
  212. LoanAmount float64 `json:"loanamount" xorm:"'LoanAmount'"` // 贷款总额=已定价额+调整金额
  213. Contractno string `json:"contractno" xorm:"'Contractno'"` // 合同编号
  214. TotalAmount float64 `json:"totalamount" xorm:"'-'"` // 合计总额
  215. ReckonedAmount float64 `json:"reckonedamount" xorm:"'ReckonedAmount'"` // 实际已收付额(已确定额,已收付总额-已退款总额)
  216. BUYUSERID int64 `json:"-" xorm:"'BUYUSERID'"` // 采购方ID
  217. SELLUSERID int64 `json:"-" xorm:"'SELLUSERID'"` // 销售方ID
  218. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  219. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  220. SPOTGOODSMODELID int32 `json:"spotgoodsmodelid" xorm:"'SPOTGOODSMODELID'"` // 现货型号ID
  221. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  222. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  223. MODELNAME string `json:"modelname" xorm:"'modelname'"` // 型号名称
  224. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  225. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  226. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  227. }
  228. func (r *ErmcpModel) calc() {
  229. // #94293 公式调整:合计总额不再加保证金
  230. r.TotalAmount = r.LoanAmount + r.ReckonOtherAmount
  231. r.UnpayAmount = r.TotalAmount - r.ReckonedAmount
  232. r.PrePayAmount = r.ReckonedAmount - r.TotalAmount
  233. r.PreInvoiceAmount = r.InvoiceAmount - r.TotalAmount
  234. if r.Contracttype == 1 {
  235. //采购合同, 取销售方名称
  236. r.AccountName = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  237. } else {
  238. //销售合同, 取采购方名称
  239. r.AccountName = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  240. }
  241. }
  242. // buildSql 组装查询的sql
  243. func (r *ErmcpModel) buildSql(nContractType, nQueryType int32) string {
  244. str := "select t.contractno," +
  245. " to_char(t.SpotContractId) SpotContractId," +
  246. " t.deliverygoodsid," +
  247. " t.spotgoodsdesc," +
  248. " t.goodsid," +
  249. " t.pricemove," +
  250. " t.userid," +
  251. " t.BUYUSERID," +
  252. " t.SELLUSERID," +
  253. " t.qty," +
  254. " t.qty - t.pricedqty unpricedqty," +
  255. " t.pricedqty," +
  256. " (case" +
  257. " when t.pricetype = 3 then" +
  258. " t.qty - t.RECKONREALQTY" +
  259. " else" +
  260. " t.pricedqty - t.ReckonRealQty" +
  261. " end) unsureqty," +
  262. " t.reckonedamount PayAmount," +
  263. " t.invoiceamount," +
  264. " (t.pricedamount + t.RECKONADJUSTAMOUNT + t.RECKONOTHERAMOUNT -" +
  265. " t.invoiceamount) as daikaiAmount," +
  266. " to_char(t.startdate, 'yyyy-mm-dd hh24:mi:ss') startdate," +
  267. " to_char(t.enddate, 'yyyy-mm-dd hh24:mi:ss') enddate," +
  268. " to_char(t.deliverystartdate, 'yyyy-mm-dd hh24:mi:ss') deliverystartdate," +
  269. " to_char(t.deliveryenddate, 'yyyy-mm-dd hh24:mi:ss') deliveryenddate," +
  270. " t.convertfactor," +
  271. " t.contractstatus," +
  272. " t.pricetype," +
  273. " t.producttype," +
  274. " t.contracttype," +
  275. " t.remark," +
  276. " to_char(t.contractattachment) attachment," +
  277. " t.margin," +
  278. " t.contractmargin," +
  279. " t.SPOTGOODSMODELID," +
  280. " t.SPOTGOODSBRANDID," +
  281. " u.accountname," +
  282. " g.deliverygoodscode," +
  283. " g.deliverygoodsname," +
  284. " wr.wrstandardid," +
  285. " wr.wrstandardname," +
  286. " wr.wrstandardcode," +
  287. " g2.goodscode," +
  288. " g2.goodsname," +
  289. " gb.brandname," +
  290. " gm.modelname," +
  291. " e.enumdicname," +
  292. " t.contracttype," +
  293. " t.pricedamount," +
  294. " case when t.pricedqty > 0 then (t.pricedamount)/t.pricedqty else 0 end pricedAvg," +
  295. " t.ReckonRealQty," +
  296. " t.ReckonOtherAmount," +
  297. " t.ReckonAdjustAmount," +
  298. " t.Price," +
  299. " t.ReckonedAmount," +
  300. " to_char(t.audittime,'yyyy-mm-dd hh24:mi:ss') audittime," +
  301. " to_char(t.createtime,'yyyy-mm-dd hh24:mi:ss') createtime," +
  302. " t.pricedamount + t.ReckonAdjustAmount as LoanAmount" +
  303. " from ermcp_spotcontract t" +
  304. " left join useraccount u" +
  305. " on t.userid = u.userid" +
  306. " left join deliverygoods g" +
  307. " on t.deliverygoodsid = g.deliverygoodsid" +
  308. " left join goods g2" +
  309. " on t.goodsid = g2.goodsid" +
  310. " left join spotgoodsbrand gb on t.spotgoodsbrandid=gb.brandid" +
  311. " left join spotgoodsmodel gm on t.spotgoodsmodelid=gm.modelid" +
  312. " left join wrstandard wr" +
  313. " on t.wrstandardid = wr.wrstandardid" +
  314. " left join enumdicitem e" +
  315. " on wr.unitid = e.enumitemname" +
  316. " and e.enumdiccode = 'goodsunit'" +
  317. " where t.contractstatus in (%v)" +
  318. " and t.contracttype in (%v) and t.userid=%v"
  319. if len(r.SpotContractId) > 0 {
  320. str += fmt.Sprintf(" and t.SpotContractId=%v", r.SpotContractId)
  321. }
  322. var orderBy string
  323. var status string
  324. if 1 == nQueryType {
  325. // 全部
  326. status = "2,3"
  327. orderBy = " order by t.audittime desc"
  328. } else if 2 == nQueryType {
  329. // 待点价
  330. status = "2"
  331. str = str + " and t.qty - t.pricedqty > 0 and t.pricetype !=1 "
  332. orderBy = " order by unpricedqty desc, t.audittime desc"
  333. } else {
  334. // 履约
  335. status = "2"
  336. orderBy = " order by t.audittime desc"
  337. }
  338. str += orderBy
  339. sqlId := fmt.Sprintf(str, status, nContractType, r.UserID)
  340. return sqlId
  341. }
  342. // GetData 从数据库中查询合同
  343. func (r *ErmcpModel) GetData(contractType, nQueryType int32) ([]ErmcpModel, error) {
  344. sData := make([]ErmcpModel, 0)
  345. e := db.GetEngine()
  346. s := e.SQL(r.buildSql(contractType, nQueryType))
  347. if err := s.Find(&sData); err != nil {
  348. logger.GetLogger().Errorf("ermcp query fail:%v", err)
  349. return sData, err
  350. }
  351. for i := range sData {
  352. sData[i].calc()
  353. }
  354. return sData, nil
  355. }
  356. // ErmcpTradeConfigTMP 交易模板配置
  357. type ErmcpTradeConfigTMP struct {
  358. TRADECONFIGTMPID int64 `json:"tradeconfigtmpid" xorm:"'TRADECONFIGTMPID'"` // 交易配置模板ID(SEQ_TRADECONFIGTMP)
  359. TRADECONFIGTMPTYPE int32 `json:"tradeconfigtmptype" xorm:"'TRADECONFIGTMPTYPE'"` // 交易配置模板类型 - 1:保证金 2:手续费 3:汇率
  360. TRADECONFIGTMPNAME string `json:"tradeconfigtmpname" xorm:"'TRADECONFIGTMPNAME'"` // 交易配置模板名称
  361. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  362. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  363. MODIFIERID int32 `json:"modifierid" xorm:"'MODIFIERID'"` // 修改人
  364. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  365. BASICCURRENCYID int64 `json:"basiccurrencyid" xorm:"'BASICCURRENCYID'"` // 基本币种ID[汇率] - 目标币种
  366. MAINACCOUNTID int64 `json:"mainaccountid" xorm:"'MAINACCOUNTID'"` // 主账户ID
  367. }
  368. func (r *ErmcpTradeConfigTMP) calc() {
  369. }
  370. func (r *ErmcpTradeConfigTMP) buildSql() string {
  371. var sqlId utils.SQLVal = "select t.areauserid," +
  372. " t.tradeconfigtmpid," +
  373. " t.tradeconfigtmptype," +
  374. " t.tradeconfigtmpname," +
  375. " t.basiccurrencyid," +
  376. " t.mainaccountid," +
  377. " t.modifierid," +
  378. " t.remark," +
  379. " to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime" +
  380. " from TRADECONFIGTMP t" +
  381. " where 1=1"
  382. if r.AREAUSERID > 0 {
  383. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  384. }
  385. return sqlId.String()
  386. }
  387. // GetDataEx 获取交易配置模板
  388. func (r *ErmcpTradeConfigTMP) GetDataEx() (interface{}, error) {
  389. sData := make([]ErmcpTradeConfigTMP, 0)
  390. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  391. for i := range sData {
  392. sData[i].calc()
  393. }
  394. return sData, err
  395. }