ermcp.go 15 KB

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