spotContract.go 22 KB

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