spotContract.go 23 KB

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