spotContract.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 string `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 string `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. 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()
  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. CreatorID: req.CreatorID,
  157. }
  158. // 调用接口,插入现货合同申请信息
  159. if err := models.AddSpotContractApply(spotContractApply); err != nil {
  160. // 插入失败
  161. logger.GetLogger().Errorf("AddSpotContractApply failed: %s", err.Error())
  162. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  163. return
  164. }
  165. // 组织响应信息
  166. rsp := AddSpotContractApplyRsp{
  167. SpotContractID: spotcontractid,
  168. ContractNo: req.ContractNo,
  169. }
  170. // 插入成功
  171. logger.GetLogger().Debugln("AddSpotContractApply successed: %v", rsp)
  172. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  173. }
  174. // GenSpotContractID 生成现货合同ID
  175. func GenSpotContractID() int64 {
  176. // 获取当前时间
  177. cur := time.Now()
  178. // UnitNano获取的是纳秒,除以1000000获取毫秒级的时间戳
  179. timestamp := cur.UnixNano() / 1000000
  180. // 除以1000获取秒级的时间戳
  181. var curutc int = int(timestamp / 1000)
  182. if RecordUtcCount == curutc {
  183. SecondCount = SecondCount + 1
  184. } else {
  185. RecordUtcCount = curutc
  186. SecondCount = 1
  187. }
  188. var id int64 = int64(345)*int64(math.Pow(10, 16)) + int64(RecordUtcCount)*int64(math.Pow(10, 6)) + int64(SecondCount)
  189. return id
  190. }
  191. // GetMarketTradeDate 获取市场交易日
  192. func GetMarketTradeDate() (string, error) {
  193. marketrun, err := models.GetMarketRun(0)
  194. if err != nil {
  195. return "", err
  196. }
  197. return marketrun.Tradedate, nil
  198. }
  199. // BuildDetailJSON 组件合同明细JSON
  200. func BuildDetailJSON(details []SoptContractDetail) (string, error) {
  201. detailsums := make([]SoptContractDetailSum, 0)
  202. for _, detail := range details {
  203. var totalpriceorderqty float64 = 0
  204. var totalpriceorderprice float64 = 0
  205. var totalpriceorderamount float64 = 0
  206. var totalpointorderqty float64 = 0
  207. var totalqty float64 = 0
  208. for _, spotpriceorder := range detail.SpotPriceOrderList {
  209. totalpriceorderqty = totalpriceorderqty + spotpriceorder.Qty
  210. totalpriceorderprice = totalpriceorderprice + spotpriceorder.Price
  211. totalpriceorderamount = totalpriceorderamount + spotpriceorder.Amount
  212. }
  213. for _, spotpointorder := range detail.SpotPointOrderVoList {
  214. totalpointorderqty = totalpointorderqty + spotpointorder.Qty
  215. }
  216. totalqty = totalpriceorderqty + totalpointorderqty
  217. var detailsum SoptContractDetailSum
  218. detailsum = SoptContractDetailSum{
  219. WrStandardID: detail.WrStandardID,
  220. WrStandardName: detail.WrStandardName,
  221. ProductType: detail.ProductType,
  222. ProductTypeName: detail.ProductTypeName,
  223. DeliveryGoodsID: detail.DeliveryGoodsID,
  224. DeliveryGoodsName: detail.DeliveryGoodsName,
  225. DeliveryGoodsDesc: detail.DeliveryGoodsDesc,
  226. WarehouseID: detail.WarehouseID,
  227. WarehouseName: detail.WarehouseName,
  228. UnitName: detail.UnitName,
  229. PointDesc: detail.PointDesc,
  230. SpotPriceOrderList: detail.SpotPriceOrderList,
  231. SpotPointOrderVoList: detail.SpotPointOrderVoList,
  232. TotalPriceOrderQty: totalpriceorderqty,
  233. TotalPriceOrderPrice: totalpriceorderprice,
  234. TotalPriceOrderAmount: totalpriceorderamount,
  235. TotalPointOrderQty: totalpointorderqty,
  236. TotalQty: totalqty,
  237. }
  238. detailsums = append(detailsums, detailsum)
  239. }
  240. jsoninfo, err := json.Marshal(detailsums)
  241. if err != nil {
  242. return "", err
  243. }
  244. return string(jsoninfo), nil
  245. }
  246. // QuerySpotContractAppleFormReq 查询合同申请表单数据请求参数
  247. type QuerySpotContractAppleFormReq struct {
  248. UserID int `form:"userID" binding:"required"`
  249. }
  250. // CustomerInfo 申请单账号信息
  251. type CustomerInfo struct {
  252. Userid int64 `json:"userid" binding:"required"` // 用户ID
  253. Customername string `json:"customername"` // 名称(企业名称)
  254. Mobile string `json:"mobile"` // 手机号码
  255. AccountIDs []int `json:"accountids"` // 资金账户ID列表
  256. }
  257. // QuerySpotContractAppleFormRsp 查询合同申请表单数据返回模型
  258. type QuerySpotContractAppleFormRsp struct {
  259. OurUser CustomerInfo `json:"ouruser"` // 我方账号
  260. OppositeUsers []CustomerInfo `json:"oppositeusers"` // 对方账号列表
  261. WrStandards []models.WRStandardInfo `json:"wrstandards"` // 仓单标准列表
  262. WareHouseInfos []models.Warehouseinfo `json:"warehouseinfos"` // 仓库信息列表
  263. Goodses []models.GoodsIDAndName `json:"goodses"` // 合约列表
  264. }
  265. // QuerySpotContractAppleForm 查询合同申请表单数据
  266. // @Summary 查询合同申请表单数据
  267. // @Produce json
  268. // @Security ApiKeyAuth
  269. // @Param userID query int true "用户ID"
  270. // @Success 200 {object} QuerySpotContractAppleFormRsp
  271. // @Failure 500 {object} app.Response
  272. // @Router /Erms3/QuerySpotContractAppleForm [get]
  273. // @Tags 风险管理v3
  274. func QuerySpotContractAppleForm(c *gin.Context) {
  275. appG := app.Gin{C: c}
  276. // 获取请求参数
  277. var req QuerySpotContractAppleFormReq
  278. err := appG.C.ShouldBindQuery(&req)
  279. if err != nil {
  280. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  281. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  282. return
  283. }
  284. rsp := QuerySpotContractAppleFormRsp{}
  285. // 获取我方用户信息
  286. ourUser, err := models.GetUserInfo(req.UserID)
  287. if err != nil {
  288. // 查询失败
  289. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  290. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  291. return
  292. }
  293. rsp.OurUser.Userid = ourUser.Userid
  294. rsp.OurUser.Customername = ourUser.Customername
  295. key, _ := hex.DecodeString(utils.AESSecretKey)
  296. if len(ourUser.Mobile) > 0 {
  297. // 手机号码解密
  298. if phonenum, err := hex.DecodeString(ourUser.Mobile); err == nil { // hex -> []byte
  299. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  300. rsp.OurUser.Mobile = string(mobile)
  301. }
  302. }
  303. }
  304. taAccounts, err := models.GetTaAccountsByType(req.UserID, 3) // 获取内部资金账户,现货都使用内部资金账户
  305. if err != nil {
  306. // 查询失败
  307. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  308. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  309. return
  310. }
  311. rsp.OurUser.AccountIDs = make([]int, 0)
  312. for _, v := range taAccounts {
  313. rsp.OurUser.AccountIDs = append(rsp.OurUser.AccountIDs, int(v.Accountid))
  314. }
  315. // 获取客户信息
  316. oppositeUsers, err := models.GetUsersByUserType(6)
  317. if err != nil {
  318. // 查询失败
  319. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  320. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  321. return
  322. }
  323. rsp.OppositeUsers = make([]CustomerInfo, 0)
  324. for _, v := range oppositeUsers {
  325. // 把自己过滤掉
  326. if int(v.Userid) == req.UserID {
  327. continue
  328. }
  329. userInfo := CustomerInfo{}
  330. userInfo.Userid = v.Userid
  331. userInfo.Customername = v.Customername
  332. if len(v.Mobile) > 0 {
  333. // 手机号码解密
  334. if phonenum, err := hex.DecodeString(v.Mobile); err == nil { // hex -> []byte
  335. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  336. userInfo.Mobile = string(mobile)
  337. }
  338. }
  339. }
  340. // 获取客户的资金账户列表
  341. opposTaAccounts, err := models.GetTaAccountsByType(int(v.Userid), 3) // 获取内部资金账户,现货都使用内部资金账户
  342. if err != nil {
  343. // 查询失败
  344. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  345. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  346. return
  347. }
  348. userInfo.AccountIDs = make([]int, 0)
  349. for _, v := range opposTaAccounts {
  350. userInfo.AccountIDs = append(userInfo.AccountIDs, int(v.Accountid))
  351. }
  352. rsp.OppositeUsers = append(rsp.OppositeUsers, userInfo)
  353. }
  354. // 获取交易标的(仓单标准)
  355. rsp.WrStandards = make([]models.WRStandardInfo, 0)
  356. wrStandards, err := models.GetWrstandards()
  357. if err != nil {
  358. // 查询失败
  359. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  360. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  361. return
  362. }
  363. rsp.WrStandards = wrStandards
  364. // 获取仓库信息
  365. rsp.WareHouseInfos = make([]models.Warehouseinfo, 0)
  366. wareHouseInfos, err := models.GetWareHouseinfos()
  367. if err != nil {
  368. // 查询失败
  369. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  370. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  371. return
  372. }
  373. rsp.WareHouseInfos = wareHouseInfos
  374. // 获取合约信息
  375. rsp.Goodses = make([]models.GoodsIDAndName, 0)
  376. goodses, err := models.GetGoodsInfosByTradeModes("15")
  377. if err != nil {
  378. // 查询失败
  379. logger.GetLogger().Errorf("QuerySpotContractAppleForm failed: %s", err.Error())
  380. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  381. return
  382. }
  383. rsp.Goodses = goodses
  384. // 查询成功
  385. logger.GetLogger().Debugln("QuerySpotContractAppleForm successed: %v", rsp)
  386. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  387. }
  388. // QuerySpotContractInfoReq 查询合同明细请求.
  389. type QuerySpotContractInfoReq struct {
  390. Accountids string `form:"accountids" binding:"required"` // 资金账号ID列表,逗号分隔.
  391. ContractType int32 `form:"contracttype" binding:"required"` // 合同类型,1为采购合同 -1为销售合同.
  392. ContractMode int32 `form:"contractmode" binding:"required"` // 合同模式,1为普通合同 2为回购销售合同.
  393. Status int32 `form:"status"` // 合同状态,0-履约中 1-已完成.
  394. }
  395. // QuerySpotContractInfoRsp 查询合同明细响应.
  396. type QuerySpotContractInfoRsp struct {
  397. SpotContractID string `json:"spotcontractid"` // 合同ID
  398. CustomerName string `json:"customername"` // 若合同类型为采购合同,表示采购方ID;若合同类型为销售合同,表示销售方ID.
  399. AccountID int64 `json:"accountid"` // 表示交易员ID.
  400. MatchCustomerName string `json:"matchcustomername"` // 若合同类型为采购合同,表示销售方;若合同类型为销售合同,表示采购方ID.
  401. MatchAccountID int64 `json:"matchaccountid"` // 表示业务员ID.
  402. WRStandardName string `json:"wrstandardname"` // 表示商品ID.
  403. TotalQty float64 `json:"totalqty"` // 表示合同量.
  404. PricedQty float64 `json:"priceqty"` // 表示定价量.
  405. UnPricedQty float64 `json:"unpricedqty"` // 表示未定价量.
  406. DeliveryQty float64 `json:"deliveryqty"` // 表示交收量.
  407. CurDeliveryQty float64 `json:"curdeliveryqty"` // 表示未交收量.
  408. SignDate string `json:"signdate"` // 表示签订日期.
  409. DeliveryGoodsID string `json:"deliverygoodsid"` // 表示品种ID.
  410. RelatedBizID string `json:"relatedbizid"` // 表示业务ID.
  411. Status int32 `json:"status"` // 表示状态,0-履约中 1-已完成.
  412. }
  413. // QuerySpotContractDetail 查询合同详细信息.
  414. // @Summary 查询合同详细信息
  415. // @Produce json
  416. // @Security ApiKeyAuth
  417. // @Param accountids query string true "资金账号ID列表,用逗号分隔"
  418. // @Param contracttype query int true "合同类型,1为采购合同 -1为销售合同"
  419. // @Param contractmode query int true "合同模式,1为普通合同 2为回购销售合同"
  420. // @Param status query int true "状态,0为履约中 1为已完成"
  421. // @Success 200 {array} QuerySpotContractInfoRsp
  422. // @Failure 500 {object} app.Response
  423. // @Router /Erms3/QuerySpotContractDetail [get]
  424. // @Tags 风险管理v3
  425. func QuerySpotContractDetail(c *gin.Context) {
  426. appG := app.Gin{C: c}
  427. // 获取请求参数
  428. var req QuerySpotContractInfoReq
  429. err := appG.C.ShouldBindQuery(&req)
  430. if err != nil {
  431. logger.GetLogger().Errorf("QuerySpotContractDetail failed: %s", err.Error())
  432. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  433. return
  434. }
  435. strids := strings.Split(strings.TrimSpace(req.Accountids), ",")
  436. accountids := make([]int64, len(strids))
  437. for i := range strids {
  438. accountids[i], err = strconv.ParseInt(strids[i], 10, 64)
  439. if err != nil {
  440. logger.GetLogger().Errorf("ParseInt failed: %s", err.Error())
  441. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  442. return
  443. }
  444. }
  445. // 查询数据.
  446. datas, err := models.QueryErms3SpotContractInfo(accountids, req.ContractType, req.ContractMode, req.Status)
  447. if err != nil {
  448. // 查询失败
  449. logger.GetLogger().Errorf("QuerySpotContract failed: %s", err.Error())
  450. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  451. return
  452. }
  453. if len(datas) == 0 {
  454. appG.Response(http.StatusOK, e.SUCCESS, nil)
  455. return
  456. }
  457. userids := make(map[int64]struct{})
  458. for i := range datas {
  459. userids[datas[i].Areauserid] = struct{}{}
  460. userids[datas[i].Customeruserid] = struct{}{}
  461. }
  462. arrayids := make([]int64, 0, len(userids))
  463. for k := range userids {
  464. arrayids = append(arrayids, k)
  465. }
  466. // 获取所有用户信息.
  467. userinfos, err := models.GetUserInfoByIDS(arrayids)
  468. if err != nil {
  469. // 查询失败
  470. logger.GetLogger().Errorf("QueryUserInfo failed: %s", err.Error())
  471. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  472. return
  473. }
  474. userid2names := make(map[int64]string, len(userinfos))
  475. for i := range userinfos {
  476. userid2names[userinfos[i].Userid] = userinfos[i].Customername
  477. }
  478. infos := make([]QuerySpotContractInfoRsp, 0, len(datas))
  479. for i := range datas {
  480. infos = append(infos, QuerySpotContractInfoRsp{
  481. SpotContractID: datas[i].Spotcontractid,
  482. CustomerName: userid2names[datas[i].Areauserid],
  483. AccountID: datas[i].Accountid,
  484. MatchCustomerName: userid2names[datas[i].Customeruserid],
  485. MatchAccountID: datas[i].Customeraccountid,
  486. WRStandardName: strings.Join([]string{datas[i].Wrstandardname, datas[i].Wrstandardcode}, "/"),
  487. TotalQty: datas[i].Pricedqty - datas[i].Pricedcancelledqty + datas[i].Unpricedqty - datas[i].Unpricedcancelledqty,
  488. PricedQty: datas[i].Pricedqty - datas[i].Pricedcancelledqty,
  489. UnPricedQty: datas[i].Unpricedqty - datas[i].Unpricedcancelledqty,
  490. DeliveryQty: datas[i].Deliveryqty,
  491. CurDeliveryQty: datas[i].Curdeliveryqty,
  492. SignDate: datas[i].Signdate.Format("2006-01-02"),
  493. DeliveryGoodsID: strings.Join([]string{datas[i].Deliverygoodsname, datas[i].Deliverygoodscode}, "/"),
  494. RelatedBizID: datas[i].Relatedbizid,
  495. Status: datas[i].Closestatus,
  496. })
  497. }
  498. // 查询成功
  499. logger.GetLogger().Debugf("QuerySpotContractDetail successed: %v", infos)
  500. appG.Response(http.StatusOK, e.SUCCESS, infos)
  501. }