ermcp.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. )
  14. // 现货合同结构(对应现货合同菜单)
  15. type ErmcpSpotContractModel struct {
  16. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` //现货合同ID(602+Unix秒时间戳(10位)+xxxxxx)
  17. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` //现货合同编号
  18. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` //现货合同类型 - 1:采购 -1:销售
  19. USERID int64 `json:"userid" xorm:"'USERID'"` //机构ID
  20. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` //采购方ID
  21. BUYUSERName string `json:"buyusername" xorm:"'BUYUSERName'"` //采购方名称
  22. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` //销售方ID
  23. SELLUSERNAME string `json:"sellusername" xorm:"'SELLUSERNAME'"` //销售方名称
  24. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` //现货品种ID
  25. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` //现货品种代码
  26. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` //现货品种名称
  27. PRODUCTTYPE int32 `json:"producttype" xorm:"'PRODUCTTYPE'"` //产品类型 - 1:标准仓单 2:等标 3:非标
  28. CONVERTFACTOR float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` //标仓系数
  29. SPOTGOODSDESC string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` //商品型号
  30. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` //定价类型 - 1:一口价 2:点价 3:暂定价
  31. QTY float64 `json:"qty" xorm:"'QTY'"` //数量
  32. PRICE float64 `json:"price" xorm:"'PRICE'"` //价格\暂定价 [1:一口价、3:暂定价]
  33. AMOUNT float64 `json:"amount" xorm:"'AMOUNT'"` //金额 [1:一口价、3:暂定价]
  34. DELIVERYSTARTDATE string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` //交收期(开始)
  35. DELIVERYENDDATE string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` //交收期(结束)
  36. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` //点价合约ID - 0:为现货,其它为期货商品合约ID [2:点价 3:暂定价]
  37. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` //点价合约代码
  38. PRICEMOVE float64 `json:"pricemove" xorm:"'PRICEMOVE'"` //升贴水 [2:点价 3:暂定价]
  39. STARTDATE string `json:"startdate" xorm:"'STARTDATE'"` //点价开始日期 [2:点价 3:暂定价]
  40. ENDDATE string `json:"enddate" xorm:"'ENDDATE'"` //点价结束日期 [2:点价 3:暂定价]
  41. MARGIN float64 `json:"margin" xorm:"'MARGIN'"` //当前保证金
  42. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` //单位名称
  43. CONTRACCTSTATUS int32 `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` //合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  44. Remark string `json:"remark" xorm:"'Remark'"` //备注
  45. }
  46. func (r *ErmcpSpotContractModel) buildSql(nQueryType int32) string {
  47. str := "SELECT to_char(t.SPOTCONTRACTID) SPOTCONTRACTID," +
  48. " t.CONTRACTNO," +
  49. " t.CONTRACTTYPE," +
  50. " t.USERID," +
  51. " t.BUYUSERID," +
  52. " t.SELLUSERID," +
  53. " t.DELIVERYGOODSID," +
  54. " t.PRODUCTTYPE," +
  55. " t.CONVERTFACTOR," +
  56. " t.SPOTGOODSDESC," +
  57. " t.PRICETYPE," +
  58. " t.QTY," +
  59. " t.PRICE," +
  60. " t.AMOUNT," +
  61. " to_char(t.DELIVERYSTARTDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYSTARTDATE," +
  62. " to_char(t.DELIVERYENDDATE,'yyyy-mm-dd hh24:mi:ss') DELIVERYENDDATE," +
  63. " t.GOODSID," +
  64. " t.PRICEMOVE," +
  65. " to_char(t.STARTDATE, 'yyyy-mm-dd hh24:mi:ss') STARTDATE," +
  66. " to_char(t.ENDDATE,'yyyy-mm-dd hh24:mi:ss') ENDDATE," +
  67. " t.MARGIN," +
  68. " t.remark," +
  69. " t.contractstatus," +
  70. " u1.accountname BuyUserName," +
  71. " u2.accountname SellUserName," +
  72. " g.deliverygoodscode," +
  73. " g.deliverygoodsname," +
  74. " g2.goodscode," +
  75. " e.Enumdicname" +
  76. " FROM ERMCP_SPOTCONTRACT t" +
  77. " left join useraccount u1" +
  78. " on t.buyuserid = u1.userid" +
  79. " left join useraccount u2" +
  80. " on t.selluserid = u2.userid" +
  81. " left join deliverygoods g" +
  82. " on t.deliverygoodsid = g.deliverygoodsid" +
  83. " left join goods g2" +
  84. " on t.goodsid = g2.goodsid" +
  85. " left join enumdicitem e on g.goodsunitid = e.enumitemname and e.enumdiccode = 'goodsunit'" +
  86. " where t.contractstatus in (%v) and t.userid=%v"
  87. // 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
  88. // 合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  89. var status string
  90. switch nQueryType {
  91. case 1:
  92. status = "0"
  93. case 2:
  94. status = "1"
  95. case 3:
  96. status = "2"
  97. case 4:
  98. status = "3,5,6"
  99. }
  100. return fmt.Sprintf(str, status, r.USERID)
  101. }
  102. // 从数据库中查询现货合同
  103. func (r *ErmcpSpotContractModel) GetData(nQueryType int32) ([]ErmcpSpotContractModel, error) {
  104. sData := make([]ErmcpSpotContractModel, 0)
  105. e := db.GetEngine()
  106. s := e.SQL(r.buildSql(nQueryType))
  107. if err := s.Find(&sData); err != nil {
  108. logger.GetLogger().Errorf("ermcp query fail:%v", err)
  109. return sData, err
  110. }
  111. return sData, nil
  112. }
  113. // 风险管理合同(对应采购和销售菜单)
  114. type ErmcpModel struct {
  115. UserID int64 `json:"userid" xorm:"'UserID'"` // 用户ID
  116. SpotContractId string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 合同ID
  117. AccountId string `json:"accountid" xorm:"'ACCOUNTID'"` // 账户ID
  118. AccountName string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 账户名称
  119. DeliveryGoodsId int `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品ID
  120. DeliveryGoodsCode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  121. DeliveryGoodsName string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  122. SpotGoodsdesc string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` // 商品型号(商品规格)
  123. GoodsId int `json:"goodsid" xorm:"'GOODSID'"` // 点价商品ID
  124. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 点价商品代码
  125. Pricemove float64 `json:"pricemove" xorm:"'PRICEMOVE'"` // 升贴水
  126. Qty float64 `json:"qty" xorm:"'QTY'"` // 合同量
  127. UnpricedQty float64 `json:"unpricedqty" xorm:"'UNPRICEDQTY'"` // 未定价量
  128. PricedQty float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  129. UnsureQty float64 `json:"unsureqty" xorm:"'UNSUREQTY'"` // 未确定量
  130. PayAmount float64 `json:"payamount" xorm:"'PAYAMOUNT'"` // 已收付额(收款或付款)
  131. UnpayAmount float64 `json:"unpayamount" xorm:"'UNPAYAMOUNT'"` // 待支收额(支付或收款)
  132. InvoiceAmount float64 `json:"invoiceamount" xorm:"'INVOICEAMOUNT'"` // 已开票额
  133. DaikaiAmount float64 `json:"daikaiamount" xorm:"'DAIKAIAMOUNT'"` // 待开票额
  134. StartDate string `json:"startdate" xorm:"'STARTDATE'"` // 点价开始日
  135. EndDate string `json:"enddate" xorm:"'ENDDATE'"` // 点价结束日
  136. DeliveryStartDate string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` // 交割开始日
  137. DeliveryendDate string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` // 交割结束日
  138. Convertfactor float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数
  139. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  140. Contracctstatus uint `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态- 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  141. PriceType int `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  142. ProductType int `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  143. Contracttype int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 合同类型 1-采购, -1-销售
  144. Pricedamount float64 `json:"pricedamount" xorm:"'PRICEDAMOUNT'"` // 已定价额
  145. PricedAvg float64 `json:"pricedavg" xorm:"'PRICEDAVG'"` // 已点均价
  146. Margin float64 `json:"margin" xorm:"'MARGIN'"` // 保证金
  147. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  148. ReckonRealQty float64 `json:"reckonrealqty" xorm:"'ReckonRealQty'"` // 已确定量
  149. ReckonOtherAmount float64 `json:"reckonotheramount" xorm:"'ReckonOtherAmount'"` // 其它费用
  150. ReckonAdjustAmount float64 `json:"reckonadjustamount" xorm:"'ReckonAdjustAmount'"` // 调整金额
  151. Price float64 `json:"price" xorm:"'Price'"` // 价格
  152. LoanAmount float64 `json:"loanamount" xorm:"'LoanAmount'"` // 贷款总额=已定价额+调整金额
  153. Contractno string `json:"contractno" xorm:"'Contractno'"` // 合同编号
  154. TotalAmount float64 `json:"totalamount" xorm:"'-'"` // 合计总额
  155. ReckonedAmount float64 `json:"reckonedamount" xorm:"'ReckonedAmount'"` // 已收付额(已确定额)
  156. BUYUSERID int64 `json:"-" xorm:"'BUYUSERID'"` //采购方ID
  157. SELLUSERID int64 `json:"-" xorm:"'SELLUSERID'"` //销售方ID
  158. }
  159. func (r *ErmcpModel) calc() {
  160. r.TotalAmount = r.LoanAmount + r.ReckonOtherAmount - r.ReckonedAmount
  161. if r.Contracttype == 1 {
  162. //采购合同, 取销售方名称
  163. r.AccountName = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  164. } else {
  165. //销售合同, 取采购方名称
  166. r.AccountName = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  167. }
  168. }
  169. // 组装查询的sql
  170. func (r *ErmcpModel) buildSql(nContractType, nQueryType int32) string {
  171. str := "select t.contractno," +
  172. " to_char(t.SpotContractId) SpotContractId," +
  173. " t.deliverygoodsid," +
  174. " t.spotgoodsdesc," +
  175. " t.goodsid," +
  176. " t.pricemove," +
  177. " t.userid," +
  178. " t.BUYUSERID," +
  179. " t.SELLUSERID," +
  180. " t.qty," +
  181. " t.qty - t.pricedqty unpricedqty," +
  182. " t.pricedqty," +
  183. " (case" +
  184. " when t.pricetype = 3 then" +
  185. " t.qty - t.RECKONREALQTY" +
  186. " else" +
  187. " t.qty - t.pricedqty" +
  188. " end) unsureqty," +
  189. " t.reckonedamount PayAmount," +
  190. " t.pricedamount + t.reckonadjustamount - t.RECKONEDAMOUNT unpayAmount," +
  191. " t.invoiceamount," +
  192. " (t.pricedamount + t.RECKONADJUSTAMOUNT + t.RECKONOTHERAMOUNT -" +
  193. " t.invoiceamount) as daikaiAmount," +
  194. " to_char(t.startdate, 'yyyy-mm-dd hh24:mi:ss') startdate," +
  195. " to_char(t.enddate, 'yyyy-mm-dd hh24:mi:ss') enddate," +
  196. " to_char(t.deliverystartdate, 'yyyy-mm-dd hh24:mi:ss') deliverystartdate," +
  197. " to_char(t.deliveryenddate, 'yyyy-mm-dd hh24:mi:ss') deliveryenddate," +
  198. " t.convertfactor," +
  199. " t.contractstatus," +
  200. " t.pricetype," +
  201. " t.producttype," +
  202. " t.contracttype," +
  203. " t.remark," +
  204. " t.margin," +
  205. " to_char(ta.accountid) accountid," +
  206. " ta.accountname," +
  207. " g.deliverygoodscode," +
  208. " g.deliverygoodsname," +
  209. " g2.goodscode," +
  210. " e.enumdicname," +
  211. " t.contracttype," +
  212. " t.pricedamount," +
  213. " case when t.pricedqty > 0 then t.pricedamount/t.pricedqty else 0 end pricedAvg," +
  214. " t.ReckonRealQty," +
  215. " t.ReckonOtherAmount," +
  216. " t.ReckonAdjustAmount," +
  217. " t.Price," +
  218. " t.ReckonedAmount," +
  219. " t.pricedamount + t.ReckonAdjustAmount as LoanAmount" +
  220. " from ermcp_spotcontract t" +
  221. " left join taaccount ta" +
  222. " on t.%v = ta.userid" +
  223. " left join deliverygoods g" +
  224. " on t.deliverygoodsid = g.deliverygoodsid" +
  225. " left join goods g2" +
  226. " on t.goodsid = g2.goodsid" +
  227. " left join wrstandard wr" +
  228. " on t.wrstandardid = wr.wrstandardid" +
  229. " left join enumdicitem e" +
  230. " on wr.unitid = e.enumitemname" +
  231. " and e.enumdiccode = 'goodsunit'" +
  232. " where t.contractstatus in (%v)" +
  233. " and t.contracttype in (%v) and t.userid=%v"
  234. var status string
  235. if 1 == nQueryType {
  236. // 全部
  237. status = "0,1,2,3,4,5,6"
  238. str = str + " order by t.audittime desc"
  239. } else if 2 == nQueryType {
  240. // 待点价
  241. status = "2"
  242. str = str + " and t.qty - t.pricedqty > 0 " +
  243. "order by unpricedqty, t.audittime desc"
  244. } else {
  245. // 履约
  246. status = "2"
  247. str = str + " order by t.audittime desc"
  248. }
  249. var usrId string
  250. switch nContractType {
  251. case 1: // 采购
  252. usrId = "buyuserid"
  253. case -1: // 销售
  254. usrId = "selluserid"
  255. }
  256. sqlId := fmt.Sprintf(str, usrId, status, nContractType, r.UserID)
  257. return sqlId
  258. }
  259. // 从数据库中查询合同
  260. func (r *ErmcpModel) GetData(contractType, nQueryType int32) ([]ErmcpModel, error) {
  261. sData := make([]ErmcpModel, 0)
  262. e := db.GetEngine()
  263. s := e.SQL(r.buildSql(contractType, nQueryType))
  264. if err := s.Find(&sData); err != nil {
  265. logger.GetLogger().Errorf("ermcp query fail:%v", err)
  266. return sData, err
  267. }
  268. for i := range sData {
  269. sData[i].calc()
  270. }
  271. return sData, nil
  272. }