spotContract.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/gin-gonic/gin"
  17. )
  18. // spotContract.go 现货合同
  19. // RecordUtcCount UTC计数(毫秒)
  20. var RecordUtcCount int = 0
  21. // SecondCount 秒计数
  22. var SecondCount int = 0
  23. // SpotPriceOrder 定价明细
  24. type SpotPriceOrder struct {
  25. Price float64 `json:"price" binding:"required"` // 价格
  26. Qty float64 `json:"qty" binding:"required"` // 数量
  27. Amount float64 `json:"amount" binding:"required"` // 金额
  28. DeliveryStartDate string `json:"deliverystartdate" binding:"required"` // 交收开始日期
  29. DeliveryEndDate string `json:"deliveryendtdate" binding:"required"` // 交收结束日期
  30. }
  31. // SpotPointOrder 点价明细
  32. type SpotPointOrder struct {
  33. GoodsID int32 `json:"goodsid" binding:"required"` // 商品ID
  34. GoodsName string `json:"goodsname"` // 商品名称
  35. Qty float64 `json:"qty" binding:"required"` // 数量
  36. Basic float64 `json:"basic" binding:"required"` // 基差
  37. StartDate string `json:"startdate" binding:"required"` // 点价开始日期
  38. EndDate string `json:"enddate" binding:"required"` // 点价结束日期
  39. }
  40. // SoptContractDetail 合同明细信息
  41. type SoptContractDetail struct {
  42. WrStandardID int64 `json:"wrstandardid" binding:"required"` // 交易标的ID
  43. WrStandardName string `json:"wrstandardname" binding:"required"` // 交易标的名称
  44. ProductType int32 `json:"producttype" binding:"required"` // 产品类型 1:标准仓单 2:等标 3:非标
  45. ProductTypeName string `json:"producttypename"` // 产品类型名称
  46. DeliveryGoodsID int32 `json:"deliverygoodsid" binding:"required"` // 现货品种ID
  47. DeliveryGoodsName string `json:"deliverygoodsname"` // 现货品种名称
  48. DeliveryGoodsDesc string `json:"deliverygoodsdesc"` // 现货品种说明
  49. WarehouseID int32 `json:"warehouseid" binding:"required"` // 仓库ID
  50. WarehouseName int32 `json:"warehousename"` // 仓库名称
  51. UnitName string `json:"unitname" binding:"required"` // 单位名称
  52. PointDesc string `json:"pointdesc"` // 点价描述
  53. SpotPriceOrderList []SpotPriceOrder `json:"spotPriceOrderList"` // 定价列表
  54. SpotPointOrderVoList []SpotPointOrder `json:"spotPointOrderVoList"` // 点价列表
  55. }
  56. // SoptContractDetailSum 合同明细信息汇总(组织JSON使用)
  57. type SoptContractDetailSum struct {
  58. WrStandardID int64 `json:"wrstandardid" binding:"required"` // 交易标的ID
  59. WrStandardName string `json:"wrstandardname" binding:"required"` // 交易标的名称
  60. ProductType int32 `json:"producttype" binding:"required"` // 产品类型 1:标准仓单 2:等标 3:非标
  61. ProductTypeName string `json:"producttypename"` // 产品类型名称
  62. DeliveryGoodsID int32 `json:"deliverygoodsid" binding:"required"` // 现货品种ID
  63. DeliveryGoodsName string `json:"deliverygoodsname"` // 现货品种名称
  64. DeliveryGoodsDesc string `json:"deliverygoodsdesc"` // 现货品种说明
  65. WarehouseID int32 `json:"warehouseid" binding:"required"` // 仓库ID
  66. WarehouseName int32 `json:"warehousename"` // 仓库名称
  67. UnitName string `json:"unitname" binding:"required"` // 单位名称
  68. PointDesc string `json:"pointdesc"` // 点价描述
  69. SpotPriceOrderList []SpotPriceOrder `json:"spotPriceOrderList"` // 定价列表
  70. SpotPointOrderVoList []SpotPointOrder `json:"spotPointOrderVoList"` // 点价列表
  71. TotalPriceOrderQty float64 `json:"totalPriceOrderQty"` // 定价总数量
  72. TotalPriceOrderPrice float64 `json:"totalPriceOrderPrice"` // 定价总价格
  73. TotalPriceOrderAmount float64 `json:"totalPriceOrderAmount"` // 定价总金额
  74. TotalPointOrderQty float64 `json:"totalPointOrderQty"` // 点价总数量
  75. TotalQty float64 `json:"totalQty"` // 总数量
  76. }
  77. // AddSpotContractApplyReq 新增现货合同申请请求
  78. type AddSpotContractApplyReq struct {
  79. ContractNo string `json:"contractno" binding:"required"` // 现货合同编号
  80. ContractType int32 `json:"contracttype" binding:"required"` // 现货合同类型 - 1:采购 -1:销售
  81. AreaUserID int32 `json:"areauserid" binding:"required"` // 所属机构
  82. AccountID int64 `json:"accountid" binding:"required"` // 资金账户ID
  83. CustomerUserID int32 `json:"customeruserid" binding:"required"` // 客户ID
  84. CustomerAccountID int64 `json:"customeraccountid" binding:"required"` // 客户资金账户ID
  85. SignDate time.Time `json:"signdate" binding:"required"` // 签订日期
  86. LastDate time.Time `json:"lastdate" binding:"required"` // 交货时间
  87. ContractAttachment string `json:"contractattachment"` // 合同附件
  88. OriMarginPayer int32 `json:"orimarginpayer" binding:"required"` // 初始保证金支付方 -1:买方 2:卖方
  89. OriMargin float64 `json:"orimargin" binding:"required"` // 初始保证金
  90. Remark string `json:"remark"` // 备注
  91. MarketID int32 `json:"marketid" binding:"required"` // 市场ID
  92. CreatorID int32 `json:"creatorid"` // 申请人
  93. Details []SoptContractDetail `json:"details" binding:"required"` // 明细
  94. }
  95. // AddSpotContractApplyRsp 新增现货合同申请响应
  96. type AddSpotContractApplyRsp struct {
  97. SpotContractID int64 `json:"spotcontractid" binging:"required"` // 现货合同ID(345+Unix秒时间戳(10位)+xxxxxx)
  98. ContractNo string `json:"contractno" binding:"required"` // 现货合同编号
  99. }
  100. // AddSpotContractApply 新增现货合同申请
  101. // @Summary 新增现货合同申请
  102. // @Produce json
  103. // @Security ApiKeyAuth
  104. // @Param jsonBody body AddSpotContractApplyReq true "申请参数"
  105. // @Success 200 {object} AddSpotContractApplyRsp
  106. // @Failure 500 {object} app.Response
  107. // @Router /Erms3/AddSpotContractApply [post]
  108. // @Tags 风险管理v3
  109. func AddSpotContractApply(c *gin.Context) {
  110. appG := app.Gin{C: c}
  111. // 获取请求参数
  112. var req AddSpotContractApplyReq
  113. err := appG.C.ShouldBindJSON(&req)
  114. if err != nil {
  115. logger.GetLogger().Errorf("AddSpotContractApply failed: %s", err.Error())
  116. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  117. return
  118. }
  119. // 生成申请ID
  120. spotcontractid := GenSpotContractID()
  121. // 获取当前交易日
  122. tradedate, err := GetMarketTradeDate(int(req.MarketID))
  123. if err != nil {
  124. // 返回失败
  125. logger.GetLogger().Errorf("AddSpotContractApply get market trade date failed: %s", err.Error())
  126. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  127. return
  128. }
  129. // 根据明细,生成JSON信息
  130. detailjson, err := BuildDetailJSON(req.Details)
  131. if err != nil {
  132. // 返回失败
  133. logger.GetLogger().Errorf("AddSpotContractApply build detail json failed: %s", err.Error())
  134. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  135. return
  136. }
  137. // 组建现货合同申请内容
  138. spotContractApply := models.Erms3SpotContractApply{
  139. SpotContractID: spotcontractid,
  140. TradeDate: tradedate,
  141. ContractNo: req.ContractNo,
  142. ContractType: req.ContractType,
  143. AreaUserID: req.AreaUserID,
  144. AccountID: req.AccountID,
  145. CustomerUserID: req.CustomerUserID,
  146. CustomerAccountID: req.CustomerAccountID,
  147. SignDate: req.SignDate,
  148. LastDate: req.LastDate,
  149. ContractAttachment: req.ContractAttachment,
  150. OriMarginPayer: req.OriMarginPayer,
  151. OriMargin: req.OriMargin,
  152. Remark: req.Remark,
  153. DetailJSON: detailjson,
  154. ApplyStatus: 0,
  155. ApplySrc: 2,
  156. MarketID: req.MarketID,
  157. CreatorID: req.CreatorID,
  158. }
  159. // 调用接口,插入现货合同申请信息
  160. if err := models.AddSpotContractApply(spotContractApply); err != nil {
  161. // 插入失败
  162. logger.GetLogger().Errorf("AddSpotContractApply failed: %s", err.Error())
  163. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  164. return
  165. }
  166. // 组织响应信息
  167. rsp := AddSpotContractApplyRsp{
  168. SpotContractID: spotcontractid,
  169. ContractNo: req.ContractNo,
  170. }
  171. // 插入成功
  172. logger.GetLogger().Debugln("AddSpotContractApply successed: %v", rsp)
  173. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  174. }
  175. // GenSpotContractID 生成现货合同ID
  176. func GenSpotContractID() int64 {
  177. // 获取当前时间
  178. cur := time.Now()
  179. // UnitNano获取的是纳秒,除以1000000获取毫秒级的时间戳
  180. timestamp := cur.UnixNano() / 1000000
  181. // 除以1000获取秒级的时间戳
  182. var curutc int = int(timestamp / 1000)
  183. if RecordUtcCount == curutc {
  184. SecondCount = SecondCount + 1
  185. } else {
  186. RecordUtcCount = curutc
  187. SecondCount = 1
  188. }
  189. var id int64 = int64(345)*int64(math.Pow(10, 16)) + int64(RecordUtcCount)*int64(math.Pow(10, 6)) + int64(SecondCount)
  190. return id
  191. }
  192. // GetMarketTradeDate 获取市场交易日
  193. func GetMarketTradeDate(marketid int) (string, error) {
  194. marketrun, err := models.GetMarketRun(marketid)
  195. if err != nil {
  196. return "", err
  197. }
  198. return marketrun.Tradedate, nil
  199. }
  200. // BuildDetailJSON 组件合同明细JSON
  201. func BuildDetailJSON(details []SoptContractDetail) (string, error) {
  202. detailsums := make([]SoptContractDetailSum, 0)
  203. for _, detail := range details {
  204. var totalpriceorderqty float64 = 0
  205. var totalpriceorderprice float64 = 0
  206. var totalpriceorderamount float64 = 0
  207. var totalpointorderqty float64 = 0
  208. var totalqty float64 = 0
  209. for _, spotpriceorder := range detail.SpotPriceOrderList {
  210. totalpriceorderqty = totalpriceorderqty + spotpriceorder.Qty
  211. totalpriceorderprice = totalpriceorderprice + spotpriceorder.Price
  212. totalpriceorderamount = totalpriceorderamount + spotpriceorder.Amount
  213. }
  214. for _, spotpointorder := range detail.SpotPointOrderVoList {
  215. totalpointorderqty = totalpointorderqty + spotpointorder.Qty
  216. }
  217. totalqty = totalpriceorderqty + totalpointorderqty
  218. var detailsum SoptContractDetailSum
  219. detailsum = SoptContractDetailSum{
  220. WrStandardID: detail.WrStandardID,
  221. WrStandardName: detail.WrStandardName,
  222. ProductType: detail.ProductType,
  223. ProductTypeName: detail.ProductTypeName,
  224. DeliveryGoodsID: detail.DeliveryGoodsID,
  225. DeliveryGoodsName: detail.DeliveryGoodsName,
  226. DeliveryGoodsDesc: detail.DeliveryGoodsDesc,
  227. WarehouseID: detail.WarehouseID,
  228. WarehouseName: detail.WarehouseName,
  229. UnitName: detail.UnitName,
  230. PointDesc: detail.PointDesc,
  231. SpotPriceOrderList: detail.SpotPriceOrderList,
  232. SpotPointOrderVoList: detail.SpotPointOrderVoList,
  233. TotalPriceOrderQty: totalpriceorderqty,
  234. TotalPriceOrderPrice: totalpriceorderprice,
  235. TotalPriceOrderAmount: totalpriceorderamount,
  236. TotalPointOrderQty: totalpointorderqty,
  237. TotalQty: totalqty,
  238. }
  239. detailsums = append(detailsums, detailsum)
  240. }
  241. jsoninfo, err := json.Marshal(detailsums)
  242. if err != nil {
  243. return "", err
  244. }
  245. return string(jsoninfo), nil
  246. }
  247. // QuerySpotContractAppleFormReq 查询合同申请表单数据请求参数
  248. type QuerySpotContractAppleFormReq struct {
  249. UserID int `form:"userID" binding:"required"`
  250. }
  251. // CustomerInfo 申请单账号信息
  252. type CustomerInfo struct {
  253. Userid int64 `json:"userid" binding:"required"` // 用户ID
  254. Customername string `json:"customername"` // 名称(企业名称)
  255. Mobile string `json:"mobile"` // 手机号码
  256. AccountIDs []int `json:"accountids"` // 资金账户ID列表
  257. }
  258. // QuerySpotContractAppleFormRsp 查询合同申请表单数据返回模型
  259. type QuerySpotContractAppleFormRsp struct {
  260. OurUser CustomerInfo `json:"ouruser"` // 我方账号
  261. OppositeUsers []CustomerInfo `json:"oppositeusers"` // 对方账号列表
  262. WrStandards []models.Wrstandard `json:"wrstandards"` // 仓单标准列表
  263. WareHouseInfos []models.Warehouseinfo `json:"warehouseinfos"` // 仓库信息列表
  264. Goodses []models.GoodsIDAndName `json:"goodses"` // 合约列表
  265. }
  266. // QuerySpotContractAppleForm 查询合同申请表单数据
  267. // @Summary 查询合同申请表单数据
  268. // @Produce json
  269. // @Security ApiKeyAuth
  270. // @Param userID query int true "用户ID"
  271. // @Success 200 {object} QuerySpotContractAppleFormRsp
  272. // @Failure 500 {object} app.Response
  273. // @Router /Erms3/QuerySpotContractAppleForm [get]
  274. // @Tags 风险管理v3
  275. func QuerySpotContractAppleForm(c *gin.Context) {
  276. appG := app.Gin{C: c}
  277. // 获取请求参数
  278. var req QuerySpotContractAppleFormReq
  279. err := appG.C.ShouldBindQuery(&req)
  280. if err != nil {
  281. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  282. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  283. return
  284. }
  285. rsp := QuerySpotContractAppleFormRsp{}
  286. // 获取我方用户信息
  287. ourUser, err := models.GetUserInfo(req.UserID)
  288. if err != nil {
  289. // 查询失败
  290. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  291. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  292. return
  293. }
  294. rsp.OurUser.Userid = ourUser.Userid
  295. rsp.OurUser.Customername = ourUser.Customername
  296. key, _ := hex.DecodeString(utils.AESSecretKey)
  297. if len(ourUser.Mobile) > 0 {
  298. // 手机号码解密
  299. if phonenum, err := hex.DecodeString(ourUser.Mobile); err == nil { // hex -> []byte
  300. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  301. rsp.OurUser.Mobile = string(mobile)
  302. }
  303. }
  304. }
  305. taAccounts, err := models.GetTaAccountsByType(req.UserID, 3) // 获取内部资金账户,现货都使用内部资金账户
  306. if err != nil {
  307. // 查询失败
  308. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  309. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  310. return
  311. }
  312. rsp.OurUser.AccountIDs = make([]int, 0)
  313. for _, v := range taAccounts {
  314. rsp.OurUser.AccountIDs = append(rsp.OurUser.AccountIDs, int(v.Accountid))
  315. }
  316. // 获取客户信息
  317. oppositeUsers, err := models.GetUsersByUserType(6)
  318. if err != nil {
  319. // 查询失败
  320. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  321. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  322. return
  323. }
  324. rsp.OppositeUsers = make([]CustomerInfo, 0)
  325. for _, v := range oppositeUsers {
  326. // 把自己过滤掉
  327. if int(v.Userid) == req.UserID {
  328. continue
  329. }
  330. userInfo := CustomerInfo{}
  331. userInfo.Userid = v.Userid
  332. userInfo.Customername = v.Customername
  333. if len(v.Mobile) > 0 {
  334. // 手机号码解密
  335. if phonenum, err := hex.DecodeString(v.Mobile); err == nil { // hex -> []byte
  336. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  337. userInfo.Mobile = string(mobile)
  338. }
  339. }
  340. }
  341. // 获取客户的资金账户列表
  342. opposTaAccounts, err := models.GetTaAccountsByType(int(v.Userid), 3) // 获取内部资金账户,现货都使用内部资金账户
  343. if err != nil {
  344. // 查询失败
  345. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  346. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  347. return
  348. }
  349. userInfo.AccountIDs = make([]int, 0)
  350. for _, v := range opposTaAccounts {
  351. userInfo.AccountIDs = append(userInfo.AccountIDs, int(v.Accountid))
  352. }
  353. rsp.OppositeUsers = append(rsp.OppositeUsers, userInfo)
  354. }
  355. // 获取交易标的(仓单标准)
  356. rsp.WrStandards = make([]models.Wrstandard, 0)
  357. wrStandards, err := models.GetWrstandards()
  358. if err != nil {
  359. // 查询失败
  360. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  361. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  362. return
  363. }
  364. rsp.WrStandards = wrStandards
  365. // 获取仓库信息
  366. rsp.WareHouseInfos = make([]models.Warehouseinfo, 0)
  367. wareHouseInfos, err := models.GetWareHouseinfos()
  368. if err != nil {
  369. // 查询失败
  370. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  371. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  372. return
  373. }
  374. rsp.WareHouseInfos = wareHouseInfos
  375. // 获取合约信息
  376. rsp.Goodses = make([]models.GoodsIDAndName, 0)
  377. goodses, err := models.GetGoodsInfosByTradeModes("15")
  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.Goodses = goodses
  385. // 查询成功
  386. logger.GetLogger().Debugln("QuerySpotContractAppleForm successed: %v", rsp)
  387. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  388. }
  389. // QuerySpotContractInfoReq 查询合同明细请求.
  390. type QuerySpotContractInfoReq struct {
  391. Accountids string `form:"accountids" binding:"required"` // 资金账号ID列表,逗号分隔.
  392. ContractType int32 `form:"contracttype" binding:"required"` // 合同类型,1为采购合同 -1为销售合同.
  393. ContractMode int32 `form:"contractmode" binding:"required"` // 合同模式,1为普通合同 2为回购销售合同.
  394. Status int32 `form:"status"` // 合同状态,0-履约中 1-已完成.
  395. }
  396. // QuerySpotContractInfoRsp 查询合同明细响应.
  397. type QuerySpotContractInfoRsp struct {
  398. SpotContractID string `json:"spotcontractid"` // 合同ID
  399. CustomerName string `json:"customername"` // 若合同类型为采购合同,表示采购方ID;若合同类型为销售合同,表示销售方ID.
  400. AccountID int64 `json:"accountid"` // 表示交易员ID.
  401. MatchCustomerName string `json:"matchcustomername"` // 若合同类型为采购合同,表示销售方;若合同类型为销售合同,表示采购方ID.
  402. MatchAccountID int64 `json:"matchaccountid"` // 表示业务员ID.
  403. WRStandardName string `json:"wrstandardname"` // 表示商品ID.
  404. TotalQty float64 `json:"totalqty"` // 表示合同量.
  405. PricedQty float64 `json:"priceqty"` // 表示定价量.
  406. UnPricedQty float64 `json:"unpricedqty"` // 表示未定价量.
  407. DeliveryQty float64 `json:"deliveryqty"` // 表示交收量.
  408. CurDeliveryQty float64 `json:"curdeliveryqty"` // 表示未交收量.
  409. SignDate string `json:"signdate"` // 表示签订日期.
  410. DeliveryGoodsID string `json:"deliverygoodsid"` // 表示品种ID.
  411. RelatedBizID string `json:"relatedbizid"` // 表示业务ID.
  412. Status int32 `json:"status"` // 表示状态,0-履约中 1-已完成.
  413. }
  414. // QuerySpotContractDetail 查询合同详细信息.
  415. // @Summary 查询合同详细信息
  416. // @Produce json
  417. // @Security ApiKeyAuth
  418. // @Param accountids query string true "资金账号ID列表,用逗号分隔"
  419. // @Param contracttype query int true "合同类型,1为采购合同 -1为销售合同"
  420. // @Param contractmode query int true "合同模式,1为普通合同 2为回购销售合同"
  421. // @Param status query int true "状态,0为履约中 1为已完成"
  422. // @Success 200 {array} QuerySpotContractInfoRsp
  423. // @Failure 500 {object} app.Response
  424. // @Router /Erms3/QuerySpotContractDetail [get]
  425. // @Tags 风险管理v3
  426. func QuerySpotContractDetail(c *gin.Context) {
  427. appG := app.Gin{C: c}
  428. // 获取请求参数
  429. var req QuerySpotContractInfoReq
  430. err := appG.C.ShouldBindQuery(&req)
  431. if err != nil {
  432. logger.GetLogger().Errorf("QuerySpotContractDetail failed: %s", err.Error())
  433. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  434. return
  435. }
  436. strids := strings.Split(strings.TrimSpace(req.Accountids), ",")
  437. accountids := make([]int64, len(strids))
  438. for i := range strids {
  439. accountids[i], err = strconv.ParseInt(strids[i], 10, 64)
  440. if err != nil {
  441. logger.GetLogger().Errorf("ParseInt failed: %s", err.Error())
  442. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  443. return
  444. }
  445. }
  446. // 查询数据.
  447. type spotContractDetail struct {
  448. models.Erms3Spotcontractdetail `xorm:"extends"`
  449. Areauserid int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构
  450. Accountid int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账户ID
  451. Customeruserid int64 `json:"customeruserid" xorm:"CUSTOMERUSERID"` // 客户ID
  452. Customeraccountid int64 `json:"customeraccountid" xorm:"CUSTOMERACCOUNTID"` // 客户资金账户ID
  453. Signdate time.Time `json:"signdate" xorm:"SIGNDATE"` // 签订日期
  454. Closestatus int32 `json:"closestatus" xorm:"CLOSESTATUS"` // 完结状态 - 0:未完结 1:已完结
  455. Relatedbizid string `json:"relatedbizid" xorm:"RELATEDBIZID"` // 关联业务ID
  456. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  457. Wrstandardcode string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 仓单标准代码
  458. Wrstandardname string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 仓单标准名称
  459. Deliverygoodscode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 交割商品代码
  460. Deliverygoodsname string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 交割商品名称
  461. }
  462. datas := make([]spotContractDetail, 0)
  463. engine := db.GetEngine()
  464. s := engine.Table("ERMS3_SPOTCONTRACTDETAIL").
  465. Join("LEFT", "ERMS3_SPOTCONTRACT", "ERMS3_SPOTCONTRACTDETAIL.SPOTCONTRACTID = ERMS3_SPOTCONTRACT.SPOTCONTRACTID").
  466. Join("LEFT", "ERMS3_BIZTRADEDETAIL", "ERMS3_BIZTRADEDETAIL.SPOTDETAILID = ERMS3_SPOTCONTRACTDETAIL.SPOTDETAILID").
  467. Join("LEFT", "WRSTANDARD", "WRSTANDARD.WRSTANDARDID = ERMS3_SPOTCONTRACTDETAIL.WRSTANDARDID").
  468. Join("LEFT", "DELIVERYGOODS", "DELIVERYGOODS.DELIVERYGOODSID = WRSTANDARD.DELIVERYGOODSID").
  469. Join("LEFT", "ENUMDICITEM", "WRSTANDARD.UNITID = ENUMDICITEM.ENUMITEMNAME AND ENUMDICITEM.ENUMDICCODE = 'goodsunit'").
  470. Select(`to_char(ERMS3_SPOTCONTRACTDETAIL.SPOTCONTRACTID) SPOTCONTRACTID, ERMS3_SPOTCONTRACT.AREAUSERID, ERMS3_SPOTCONTRACT.ACCOUNTID, ERMS3_SPOTCONTRACT.CUSTOMERUSERID,
  471. ERMS3_SPOTCONTRACT.CUSTOMERACCOUNTID, ERMS3_SPOTCONTRACT.CLOSESTATUS, ERMS3_SPOTCONTRACT.SIGNDATE, ERMS3_SPOTCONTRACTDETAIL.*, WRSTANDARD.WRSTANDARDNAME, WRSTANDARD.WRSTANDARDCODE, DELIVERYGOODS.DELIVERYGOODSNAME, DELIVERYGOODS.DELIVERYGOODSCODE,
  472. to_char(ERMS3_BIZTRADEDETAIL.RELATEDBIZID) RELATEDBIZID, ENUMDICITEM.ENUMDICNAME GOODUNIT`).
  473. Where("ERMS3_SPOTCONTRACT.CONTRACTTYPE = ? AND ERMS3_SPOTCONTRACT.CONTRACTMODE = ? AND ERMS3_SPOTCONTRACT.CLOSESTATUS = ?", req.ContractType, req.ContractMode, req.Status).
  474. In("ERMS3_SPOTCONTRACT.ACCOUNTID", accountids).
  475. Desc("ERMS3_SPOTCONTRACTDETAIL.SPOTCONTRACTID")
  476. if err := s.Find(&datas); err != nil {
  477. // 查询失败
  478. logger.GetLogger().Errorf("QuerySpotContract failed: %s", err.Error())
  479. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  480. return
  481. }
  482. if len(datas) == 0 {
  483. appG.Response(http.StatusOK, e.SUCCESS, nil)
  484. return
  485. }
  486. userids := make(map[int64]struct{})
  487. for i := range datas {
  488. userids[datas[i].Areauserid] = struct{}{}
  489. userids[datas[i].Customeruserid] = struct{}{}
  490. }
  491. arrayids := make([]int64, 0, len(userids))
  492. for k, _ := range userids {
  493. arrayids = append(arrayids, k)
  494. }
  495. // 获取所有用户信息.
  496. userinfos, err := models.GetUserInfoByIDS(arrayids)
  497. if err != nil {
  498. // 查询失败
  499. logger.GetLogger().Errorf("QueryUserInfo failed: %s", err.Error())
  500. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  501. return
  502. }
  503. userid2names := make(map[int64]string, len(userinfos))
  504. for i := range userinfos {
  505. userid2names[userinfos[i].Userid] = userinfos[i].Customername
  506. }
  507. infos := make([]QuerySpotContractInfoRsp, 0, len(datas))
  508. for i := range datas {
  509. infos = append(infos, QuerySpotContractInfoRsp{
  510. SpotContractID: datas[i].Spotcontractid,
  511. CustomerName: userid2names[datas[i].Areauserid],
  512. AccountID: datas[i].Accountid,
  513. MatchCustomerName: userid2names[datas[i].Customeruserid],
  514. MatchAccountID: datas[i].Customeraccountid,
  515. WRStandardName: strings.Join([]string{datas[i].Wrstandardname, datas[i].Wrstandardcode}, "/"),
  516. TotalQty: datas[i].Pricedqty - datas[i].Pricedcancelledqty + datas[i].Unpricedqty - datas[i].Unpricedcancelledqty,
  517. PricedQty: datas[i].Pricedqty - datas[i].Pricedcancelledqty,
  518. UnPricedQty: datas[i].Unpricedqty - datas[i].Unpricedcancelledqty,
  519. DeliveryQty: datas[i].Deliveryqty,
  520. CurDeliveryQty: datas[i].Curdeliveryqty,
  521. SignDate: datas[i].Signdate.Format("2006-01-02"),
  522. DeliveryGoodsID: strings.Join([]string{datas[i].Deliverygoodsname, datas[i].Deliverygoodscode}, "/"),
  523. RelatedBizID: datas[i].Relatedbizid,
  524. Status: datas[i].Closestatus,
  525. })
  526. }
  527. // 查询成功
  528. logger.GetLogger().Debugf("QuerySpotContractDetail successed: %v", infos)
  529. appG.Response(http.StatusOK, e.SUCCESS, infos)
  530. }