ermcp.go 14 KB

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