spotContract.go 23 KB

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