spotContract.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. package erms3
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "math"
  6. "mtp2_if/db"
  7. "mtp2_if/global/app"
  8. "mtp2_if/global/e"
  9. "mtp2_if/logger"
  10. "mtp2_if/models"
  11. "mtp2_if/utils"
  12. "net/http"
  13. "time"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // spotContract.go 现货合同
  17. // RecordUtcCount UTC计数(毫秒)
  18. var RecordUtcCount int = 0
  19. // SecondCount 秒计数
  20. var SecondCount int = 0
  21. // SpotPriceOrder 定价明细
  22. type SpotPriceOrder struct {
  23. Price float64 `json:"price" binding:"required"` // 价格
  24. Qty float64 `json:"qty" binding:"required"` // 数量
  25. Amount float64 `json:"amount" binding:"required"` // 金额
  26. }
  27. // SpotPointOrder 点价明细
  28. type SpotPointOrder struct {
  29. GoodsID int32 `json:"goodsid" binding:"required"` // 商品ID
  30. GoodsName string `json:"goodsname"` // 商品名称
  31. Qty float64 `json:"qty" binding:"required"` // 数量
  32. Basic float64 `json:"basic" binding:"required"` // 基差
  33. StartDate string `json:"startdate" binding:"required"` // 点价开始日期
  34. EndDate string `json:"enddate" binding:"required"` // 点价结束日期
  35. }
  36. // SoptContractDetail 合同明细信息
  37. type SoptContractDetail struct {
  38. WrStandardID int64 `json:"wrstandardid" binding:"required"` // 交易标的ID
  39. WrStandardName string `json:"wrstandardname" binding:"required"` // 交易标的名称
  40. ProductType int32 `json:"producttype" binding:"required"` // 产品类型 1:标准仓单 2:等标 3:非标
  41. ProductTypeName string `json:"producttypename"` // 产品类型名称
  42. DeliveryGoodsID int32 `json:"deliverygoodsid" binding:"required"` // 现货品种ID
  43. DeliveryGoodsName string `json:"deliverygoodsname"` // 现货品种名称
  44. DeliveryGoodsDesc string `json:"deliverygoodsdesc"` // 现货品种说明
  45. WarehouseID int32 `json:"warehouseid" binding:"required"` // 仓库ID
  46. WarehouseName int32 `json:"warehousename"` // 仓库名称
  47. UnitName string `json:"unitname" binding:"required"` // 单位名称
  48. PointDesc string `json:"pointdesc"` // 点价描述
  49. SpotPriceOrderList []SpotPriceOrder `json:"spotPriceOrderList"` // 定价列表
  50. SpotPointOrderVoList []SpotPointOrder `json:"spotPointOrderVoList"` // 点价列表
  51. }
  52. // SoptContractDetailSum 合同明细信息汇总(组织JSON使用)
  53. type SoptContractDetailSum struct {
  54. WrStandardID int64 `json:"wrstandardid" binding:"required"` // 交易标的ID
  55. WrStandardName string `json:"wrstandardname" binding:"required"` // 交易标的名称
  56. ProductType int32 `json:"producttype" binding:"required"` // 产品类型 1:标准仓单 2:等标 3:非标
  57. ProductTypeName string `json:"producttypename"` // 产品类型名称
  58. DeliveryGoodsID int32 `json:"deliverygoodsid" binding:"required"` // 现货品种ID
  59. DeliveryGoodsName string `json:"deliverygoodsname"` // 现货品种名称
  60. DeliveryGoodsDesc string `json:"deliverygoodsdesc"` // 现货品种说明
  61. WarehouseID int32 `json:"warehouseid" binding:"required"` // 仓库ID
  62. WarehouseName int32 `json:"warehousename"` // 仓库名称
  63. UnitName string `json:"unitname" binding:"required"` // 单位名称
  64. PointDesc string `json:"pointdesc"` // 点价描述
  65. SpotPriceOrderList []SpotPriceOrder `json:"spotPriceOrderList"` // 定价列表
  66. SpotPointOrderVoList []SpotPointOrder `json:"spotPointOrderVoList"` // 点价列表
  67. TotalPriceOrderQty float64 `json:"totalPriceOrderQty"` // 定价总数量
  68. TotalPriceOrderPrice float64 `json:"totalPriceOrderPrice"` // 定价总价格
  69. TotalPriceOrderAmount float64 `json:"totalPriceOrderAmount"` // 定价总金额
  70. TotalPointOrderQty float64 `json:"totalPointOrderQty"` // 点价总数量
  71. TotalQty float64 `json:"totalQty"` // 总数量
  72. }
  73. // AddSpotContractApplyReq 新增现货合同申请请求
  74. type AddSpotContractApplyReq struct {
  75. ContractNo string `json:"contractno" binding:"required"` // 现货合同编号
  76. ContractType int32 `json:"contracttype" binding:"required"` // 现货合同类型 - 1:采购 -1:销售
  77. AreaUserID int32 `json:"areauserid" binding:"required"` // 所属机构
  78. AccountID int64 `json:"accountid" binding:"required"` // 资金账户ID
  79. CustomerUserID int32 `json:"customeruserid" binding:"required"` // 客户ID
  80. CustomerAccountID int64 `json:"customeraccountid" binding:"required"` // 客户资金账户ID
  81. SignDate time.Time `json:"signdate" binding:"required"` // 签订日期
  82. LastDate time.Time `json:"lastdate" binding:"required"` // 交货时间
  83. ContractAttachment string `json:"contractattachment"` // 合同附件
  84. OriMarginPayer int32 `json:"orimarginpayer" binding:"required"` // 初始保证金支付方 -1:买方 2:卖方
  85. OriMargin float64 `json:"orimargin" binding:"required"` // 初始保证金
  86. Remark string `json:"remark"` // 备注
  87. MarketID int32 `json:"marketid" binding:"required"` // 市场ID
  88. CreatorID int32 `json:"creatorid"` // 申请人
  89. Details []SoptContractDetail `json:"details" binding:"required"` // 明细
  90. }
  91. // AddSpotContractApplyRsp 新增现货合同申请响应
  92. type AddSpotContractApplyRsp struct {
  93. SpotContractID int64 `json:"spotcontractid" binging:"required"` // 现货合同ID(345+Unix秒时间戳(10位)+xxxxxx)
  94. ContractNo string `json:"contractno" binding:"required""` // 现货合同编号
  95. }
  96. // AddSpotContractApply 新增现货合同申请
  97. // @Summary 新增现货合同申请
  98. // @Produce json
  99. // @Security ApiKeyAuth
  100. // @Param jsonBody body AddSpotContractApplyReq true "申请参数"
  101. // @Success 200 {object} AddSpotContractApplyRsp
  102. // @Failure 500 {object} app.Response
  103. // @Router /Erms3/AddSpotContractApply [post]
  104. // @Tags 风险管理v3
  105. func AddSpotContractApply(c *gin.Context) {
  106. appG := app.Gin{C: c}
  107. // 获取请求参数
  108. var req AddSpotContractApplyReq
  109. err := appG.C.ShouldBindJSON(&req)
  110. if err != nil {
  111. logger.GetLogger().Errorf("AddSpotContractApply failed: %s", err.Error())
  112. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  113. return
  114. }
  115. // 生成申请ID
  116. spotcontractid := GenSpotContractID()
  117. // 获取当前交易日
  118. tradedate, err := GetMarketTradeDate(int(req.MarketID))
  119. if err != nil {
  120. // 返回失败
  121. logger.GetLogger().Errorf("AddSpotContractApply get market trade date failed: %s", err.Error())
  122. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  123. return
  124. }
  125. // 根据明细,生成JSON信息
  126. detailjson, err := BuildDetailJSON(req.Details)
  127. if err != nil {
  128. // 返回失败
  129. logger.GetLogger().Errorf("AddSpotContractApply build detail json failed: %s", err.Error())
  130. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  131. return
  132. }
  133. // 组织sql,插入现货合同申请信息
  134. sql := `INSERT INTO ERMS3_SPOTCONTRACTAPPLY(SPOTCONTRACTID,
  135. TRADEDATE,
  136. CONTRACTNO,
  137. CONTRACTTYPE,
  138. AREAUSERID,
  139. ACCOUNTID,
  140. CUSTOMERUSERID,
  141. CUSTOMERACCOUNTID,
  142. SIGNDATE,
  143. LASTDATE,
  144. CONTRACTATTACHMENT,
  145. ORIMARGINPAYER,
  146. ORIMARGIN,
  147. REMARK,
  148. DETAILJSON,
  149. APPLYSTATUS,
  150. APPLYSRC,
  151. MARKETID,
  152. CREATORID,
  153. CREATETIME)
  154. VALUES(
  155. ?,
  156. ?,
  157. ?,
  158. ?,
  159. ?,
  160. ?,
  161. ?,
  162. ?,
  163. to_date(?, 'YYYY-MM-DD HH24:MI:SS'),
  164. to_date(?, 'YYYY-MM-DD HH24:MI:SS'),
  165. ?,
  166. ?,
  167. ?,
  168. ?,
  169. ?,
  170. 0,
  171. 2,
  172. ?,
  173. ?,
  174. sysdate)`
  175. engine := db.GetEngine()
  176. if _, err := engine.Exec(sql,
  177. spotcontractid,
  178. tradedate,
  179. req.ContractNo,
  180. req.ContractType,
  181. req.AreaUserID,
  182. req.AccountID,
  183. req.CustomerUserID,
  184. req.CustomerAccountID,
  185. req.SignDate,
  186. req.LastDate,
  187. req.ContractAttachment,
  188. req.OriMarginPayer,
  189. req.OriMargin,
  190. req.Remark,
  191. detailjson,
  192. req.MarketID,
  193. req.CreatorID); err != nil {
  194. // 插入失败
  195. logger.GetLogger().Errorf("AddSpotContractApply failed: %s", err.Error())
  196. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  197. return
  198. }
  199. rsp := AddSpotContractApplyRsp{
  200. SpotContractID: spotcontractid,
  201. ContractNo: req.ContractNo,
  202. }
  203. // 查询成功
  204. logger.GetLogger().Debugln("AddSpotContractApply successed: %v", rsp)
  205. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  206. }
  207. // GenSpotContractID 生成现货合同ID
  208. func GenSpotContractID() int64 {
  209. // 获取当前时间
  210. cur := time.Now()
  211. // UnitNano获取的是纳秒,除以1000000获取毫秒级的时间戳
  212. timestamp := cur.UnixNano() / 1000000
  213. // 除以1000获取秒级的时间戳
  214. var curutc int = int(timestamp / 1000)
  215. if RecordUtcCount == curutc {
  216. SecondCount = SecondCount + 1
  217. } else {
  218. RecordUtcCount = curutc
  219. SecondCount = 1
  220. }
  221. var id int64 = int64(345)*int64(math.Pow(10, 16)) + int64(RecordUtcCount)*int64(math.Pow(10, 6)) + int64(SecondCount)
  222. return id
  223. }
  224. // GetMarketTradeDate 获取市场交易日
  225. func GetMarketTradeDate(marketid int) (string, error) {
  226. marketrun, err := models.GetMarketRun(marketid)
  227. if err != nil {
  228. return "", err
  229. }
  230. return marketrun.Tradedate, nil
  231. }
  232. // BuildDetailJSON 组件合同明细JSON
  233. func BuildDetailJSON(details []SoptContractDetail) (string, error) {
  234. detailsums := make([]SoptContractDetailSum, 0)
  235. for _, detail := range details {
  236. var totalpriceorderqty float64 = 0
  237. var totalpriceorderprice float64 = 0
  238. var totalpriceorderamount float64 = 0
  239. var totalpointorderqty float64 = 0
  240. var totalqty float64 = 0
  241. for _, spotpriceorder := range detail.SpotPriceOrderList {
  242. totalpriceorderqty = totalpriceorderqty + spotpriceorder.Qty
  243. totalpriceorderprice = totalpriceorderprice + spotpriceorder.Price
  244. totalpriceorderamount = totalpriceorderamount + spotpriceorder.Amount
  245. }
  246. for _, spotpointorder := range detail.SpotPointOrderVoList {
  247. totalpointorderqty = totalpointorderqty + spotpointorder.Qty
  248. }
  249. totalqty = totalpriceorderqty + totalpointorderqty
  250. var detailsum SoptContractDetailSum
  251. detailsum = SoptContractDetailSum{
  252. WrStandardID: detail.WrStandardID,
  253. WrStandardName: detail.WrStandardName,
  254. ProductType: detail.ProductType,
  255. ProductTypeName: detail.ProductTypeName,
  256. DeliveryGoodsID: detail.DeliveryGoodsID,
  257. DeliveryGoodsName: detail.DeliveryGoodsName,
  258. DeliveryGoodsDesc: detail.DeliveryGoodsDesc,
  259. WarehouseID: detail.WarehouseID,
  260. WarehouseName: detail.WarehouseName,
  261. UnitName: detail.UnitName,
  262. PointDesc: detail.PointDesc,
  263. SpotPriceOrderList: detail.SpotPriceOrderList,
  264. SpotPointOrderVoList: detail.SpotPointOrderVoList,
  265. TotalPriceOrderQty: totalpriceorderqty,
  266. TotalPriceOrderPrice: totalpriceorderprice,
  267. TotalPriceOrderAmount: totalpriceorderamount,
  268. TotalPointOrderQty: totalpointorderqty,
  269. TotalQty: totalqty,
  270. }
  271. detailsums = append(detailsums, detailsum)
  272. }
  273. jsoninfo, err := json.Marshal(detailsums)
  274. if err != nil {
  275. return "", err
  276. }
  277. return string(jsoninfo), nil
  278. }
  279. // QuerySpotContractAppleFormReq 查询合同申请表单数据请求参数
  280. type QuerySpotContractAppleFormReq struct {
  281. UserID int `form:"userID" binding:"required"`
  282. }
  283. // CustomerInfo 申请单账号信息
  284. type CustomerInfo struct {
  285. Userid int64 `json:"userid" binding:"required"` // 用户ID
  286. Customername string `json:"customername"` // 名称(企业名称)
  287. Mobile string `json:"mobile"` // 手机号码
  288. AccountIDs []int `json:"accountids"` // 资金账户ID列表
  289. }
  290. // QuerySpotContractAppleFormRsp 查询合同申请表单数据返回模型
  291. type QuerySpotContractAppleFormRsp struct {
  292. OurUser CustomerInfo `json:"ouruser"` // 我方账号
  293. OppositeUsers []CustomerInfo `json:"oppositeusers"` // 对方账号列表
  294. WrStandards []models.Wrstandard `json:"wrstandards"` // 仓单标准列表
  295. WareHouseInfos []models.Warehouseinfo `json:"warehouseinfos"` // 仓库信息列表
  296. }
  297. // QuerySpotContractAppleForm 查询合同申请表单数据
  298. // @Summary 查询合同申请表单数据
  299. // @Produce json
  300. // @Security ApiKeyAuth
  301. // @Param userID query int true "用户ID"
  302. // @Success 200 {object} QuerySpotContractAppleFormRsp
  303. // @Failure 500 {object} app.Response
  304. // @Router /Erms3/QuerySpotContractAppleForm [get]
  305. // @Tags 风险管理v3
  306. func QuerySpotContractAppleForm(c *gin.Context) {
  307. appG := app.Gin{C: c}
  308. // 获取请求参数
  309. var req QuerySpotContractAppleFormReq
  310. err := appG.C.ShouldBindQuery(&req)
  311. if err != nil {
  312. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  313. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  314. return
  315. }
  316. rsp := QuerySpotContractAppleFormRsp{}
  317. // 获取我方用户信息
  318. ourUser, err := models.GetUserInfo(req.UserID)
  319. if err != nil {
  320. // 查询失败
  321. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  322. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  323. return
  324. }
  325. rsp.OurUser.Userid = ourUser.Userid
  326. rsp.OurUser.Customername = ourUser.Customername
  327. key, _ := hex.DecodeString(utils.AESSecretKey) // 手机号码解密
  328. if phonenum, err := hex.DecodeString(ourUser.Mobile); err == nil { // hex -> []byte
  329. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  330. rsp.OurUser.Mobile = string(mobile)
  331. }
  332. }
  333. taAccounts, err := models.GetTaAccountsByType(req.UserID, 3) // 获取内部资金账户,现货都使用内部资金账户
  334. if err != nil {
  335. // 查询失败
  336. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  337. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  338. return
  339. }
  340. rsp.OurUser.AccountIDs = make([]int, 0)
  341. for _, v := range taAccounts {
  342. rsp.OurUser.AccountIDs = append(rsp.OurUser.AccountIDs, int(v.Accountid))
  343. }
  344. // 获取客户信息
  345. oppositeUsers, err := models.GetUsersByUserType(6)
  346. if err != nil {
  347. // 查询失败
  348. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  349. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  350. return
  351. }
  352. rsp.OppositeUsers = make([]CustomerInfo, 0)
  353. for _, v := range oppositeUsers {
  354. // 把自己过滤掉
  355. if int(v.Userid) == req.UserID {
  356. continue
  357. }
  358. userInfo := CustomerInfo{}
  359. userInfo.Userid = v.Userid
  360. userInfo.Customername = v.Customername
  361. // 获取客户的资金账户列表
  362. opposTaAccounts, err := models.GetTaAccountsByType(int(v.Userid), 3) // 获取内部资金账户,现货都使用内部资金账户
  363. if err != nil {
  364. // 查询失败
  365. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  366. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  367. return
  368. }
  369. userInfo.AccountIDs = make([]int, 0)
  370. for _, v := range opposTaAccounts {
  371. userInfo.AccountIDs = append(userInfo.AccountIDs, int(v.Accountid))
  372. }
  373. rsp.OppositeUsers = append(rsp.OppositeUsers, userInfo)
  374. }
  375. // 获取交易标的(仓单标准)
  376. rsp.WrStandards = make([]models.Wrstandard, 0)
  377. wrStandards, err := models.GetWrstandards()
  378. if err != nil {
  379. // 查询失败
  380. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  381. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  382. return
  383. }
  384. rsp.WrStandards = wrStandards
  385. // 获取仓库信息
  386. rsp.WareHouseInfos = make([]models.Warehouseinfo, 0)
  387. wareHouseInfos, err := models.GetWareHouseinfos()
  388. if err != nil {
  389. // 查询失败
  390. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  391. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  392. return
  393. }
  394. rsp.WareHouseInfos = wareHouseInfos
  395. // 查询成功
  396. logger.GetLogger().Debugln("QuerySpotContractAppleForm successed: %v", rsp)
  397. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  398. }