spotContract.go 23 KB

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