qryContract.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2020/12/2 9:18
  4. * @Modify : 2020/12/2 9:18
  5. */
  6. package erms3
  7. import (
  8. "encoding/json"
  9. "mtp2_if/global/app"
  10. "mtp2_if/global/e"
  11. "mtp2_if/logger"
  12. "mtp2_if/models"
  13. "net/http"
  14. "strconv"
  15. "github.com/gin-gonic/gin"
  16. )
  17. // 去除多余的冒号
  18. func tripColon(str string) string {
  19. if len(str) >= 3 && str[0] == '"' {
  20. return str[1 : len(str)-1]
  21. }
  22. return str
  23. }
  24. type sFloat64 float64
  25. func (r *sFloat64) UnmarshalJSON(buf []byte) error {
  26. str := tripColon(string(buf))
  27. if d, err := strconv.ParseFloat(str, 64); err == nil {
  28. *r = sFloat64(d)
  29. } else {
  30. return err
  31. }
  32. return nil
  33. }
  34. type sInt32 int32
  35. func (r *sInt32) UnmarshalJSON(buf []byte) error {
  36. str := tripColon(string(buf))
  37. if d, err := strconv.ParseInt(str, 10, 64); err == nil {
  38. *r = sInt32(d)
  39. } else {
  40. return err
  41. }
  42. return nil
  43. }
  44. type sInt64 int64
  45. func (r *sInt64) UnmarshalJSON(buf []byte) error {
  46. str := tripColon(string(buf))
  47. if d, err := strconv.ParseInt(str, 10, 64); err == nil {
  48. *r = sInt64(d)
  49. } else {
  50. return err
  51. }
  52. return nil
  53. }
  54. // 解析定价明细Json字段
  55. type SpotPriceJson struct {
  56. Price sFloat64
  57. Qty sFloat64
  58. Amount sFloat64
  59. }
  60. // 解析点价明细Json字段
  61. type SpotPointJson struct {
  62. GoodsID sInt32
  63. GoodsName string
  64. Qty sFloat64
  65. Basic sFloat64
  66. StartDate string
  67. EndDate string
  68. }
  69. // 解析合同申请表Json字段
  70. type DetailJSON struct {
  71. WrStandardID sInt64
  72. WrStandardName string
  73. ProductType sInt32
  74. ProductTypeName string
  75. DeliveryGoodsID sInt32
  76. DeliveryGoodsName string
  77. DeliveryGoodsDesc string
  78. WarehouseID sInt32
  79. WarehouseName string
  80. UnitName string
  81. PointDesc string
  82. SpotPriceOrderList []SpotPriceJson
  83. SpotPointOrderVoList []SpotPointJson
  84. }
  85. // 查询待审核合同请求
  86. type QryAuditContractReq struct {
  87. AccountIDs string `form:"accountids" binding:"required"` // 请求账号ID列表
  88. ContractType int32 `form:"contracttype" binding:"required"` // 合同类型
  89. ContractMode uint32 `form:"contractmode" binding:"required"` // 合同模式
  90. }
  91. // 查询待审核合同应答
  92. type QryAuditContractRsp struct {
  93. SpotContractId string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'" binding:"required"` // 合同ID
  94. MatchCustomerName string `json:"matchcustomername" xorm:"'MATCHCUSTOMERNAME'" binding:"required"` // 销售方ID
  95. MatchAccountId string `json:"matchaccountid" xorm:"'MATCHACCOUNTID'"` // 业务员ID
  96. AccountId string `json:"accountid" xorm:"'ACCOUNTID'"` // 交易员ID
  97. CustomerName string `json:"customername" xorm:"'CUSTOMERNAME'"` // 采购方ID
  98. WrstandardName string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 商品名称
  99. Wrstandardcode string `json:"-"` // 商品代码
  100. PricedQty float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 定价量
  101. UnpricedQty float64 `json:"unpricedqty" xorm:"'UNPRICEDQTY'"` // 未定价量
  102. TotaldQty float64 `json:"totaldqty" xorm:"'TOTALDQTY'"` // 合同量
  103. DeliveryQty uint64 `json:"deliveryqty" xorm:"'DELIVERYQTY'"` // 交收量
  104. CurDeliveryQty uint64 `json:"curdeliveryqty" xorm:"'CURDELIVERYQTY'"` // 未交收量
  105. DeliveryGoodsID string `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 品种ID
  106. DeliveryGoodsName string `json:"-"` // 品种名称
  107. DeliveryGoodscode string `json:"-"` // 品种代码
  108. ApplyStatus uint32 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 0:未审核 1:审核通过 2:审核中 3:审核失败 4已撤销 5:审核拒绝
  109. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  110. SignDate string `json:"signdate" xorm:"'SIGNDATE'"` // 签订日期
  111. deliverGoods map[int32]models.Deliverygoods // 不参与json编码,作为id转code缓存
  112. wrStandards map[int64]models.WRStandardInfo // 不参与json编码,作为id转code缓存
  113. }
  114. // 转换交割商品ID为商品代码
  115. func (r *QryAuditContractRsp) DeliverGoodsId2Code(id int32) string {
  116. // init
  117. if r.deliverGoods == nil || len(r.deliverGoods) == 0 {
  118. r.deliverGoods = make(map[int32]models.Deliverygoods)
  119. if dg, err := models.GetDeliverGoods(); err == nil {
  120. for _, v := range dg {
  121. r.deliverGoods[v.Deliverygoodsid] = v
  122. }
  123. }
  124. }
  125. if v, ok := r.deliverGoods[id]; ok {
  126. return v.Deliverygoodscode
  127. }
  128. return ""
  129. }
  130. // 转换仓单商品ID为代code
  131. func (r *QryAuditContractRsp) WrStandardID2Code(id int64) string {
  132. // init
  133. if r.wrStandards == nil || len(r.wrStandards) == 0 {
  134. r.wrStandards = make(map[int64]models.WRStandardInfo)
  135. if wg, err := models.GetWrstandards(); err == nil {
  136. for _, v := range wg {
  137. r.wrStandards[v.Wrstandardid] = v
  138. }
  139. }
  140. }
  141. if v, ok := r.wrStandards[id]; ok {
  142. return v.Wrstandardcode
  143. }
  144. return ""
  145. }
  146. // 构建响应数据
  147. func (r *QryAuditContractRsp) ParseFromModel(val models.AuditContractModel) error {
  148. r.SpotContractId = val.SpotContractId
  149. r.MatchAccountId = val.MatchAccountId
  150. r.MatchCustomerName = val.MatchCustomerName
  151. r.AccountId = val.AccountId
  152. r.CustomerName = val.CustomerName
  153. r.ApplyStatus = val.ApplyStatus
  154. // 解析DetailJSON字段
  155. details := make([]DetailJSON, 0)
  156. if err := json.Unmarshal([]byte(val.DetailJSON), &details); err != nil {
  157. logger.GetLogger().Errorf("parse detailJson field fail:%v", err)
  158. return err
  159. }
  160. // 现在合同只有一张单
  161. detail := details[0]
  162. r.WrstandardName = detail.WrStandardName
  163. r.DeliveryGoodsName = detail.DeliveryGoodsName
  164. r.EnumdicName = detail.UnitName
  165. // 汇总未定价量
  166. r.UnpricedQty = 0
  167. for _, v := range detail.SpotPointOrderVoList {
  168. r.UnpricedQty += float64(v.Qty)
  169. }
  170. // 汇总定价量
  171. r.PricedQty = 0
  172. for _, v := range detail.SpotPriceOrderList {
  173. r.PricedQty += float64(v.Qty)
  174. }
  175. // 总量
  176. r.TotaldQty = r.UnpricedQty + r.PricedQty
  177. // 转换仓单商品ID为代code
  178. r.Wrstandardcode = r.WrStandardID2Code(int64(detail.WrStandardID))
  179. // 转换交割商品ID为商品代码
  180. r.DeliveryGoodscode = r.DeliverGoodsId2Code(int32(detail.DeliveryGoodsID))
  181. r.WrstandardName = r.WrstandardName + "/" + r.Wrstandardcode
  182. r.DeliveryGoodsID = r.DeliveryGoodsName + "/" + r.DeliveryGoodscode
  183. return nil
  184. }
  185. // 构建响应数据
  186. func (r *QryAuditContractRsp) ParseFromHGModel(val models.AutditContractHGModel) error {
  187. r.SpotContractId = val.SpotContractId
  188. r.MatchCustomerName = val.MatchCustomerName
  189. r.AccountId = val.AccountId
  190. r.CustomerName = val.CustomerName
  191. r.EnumdicName = val.EnumdicName
  192. r.WrstandardName = val.WrstandardName + "/" + val.Wrstandardcode
  193. r.Wrstandardcode = val.Wrstandardcode
  194. r.ApplyStatus = val.ApplyStatus
  195. r.SignDate = val.SignDate
  196. r.MatchAccountId = val.MatchAccountId
  197. r.TotaldQty = val.ContractQty
  198. return nil
  199. }
  200. // QueryPendingAuditContract 查询待审核合同
  201. // @Summary 查询待审核合同
  202. // @Produce json
  203. // @Security ApiKeyAuth
  204. // @Param accountids query string true "资金账号ID列表,逗号隔开"
  205. // @Param contracttype query int true "合同类型 1-采购 -1-销售"
  206. // @Param contractmode query int true "合同模式 1-普通 2-回购"
  207. // @Success 200 {array} QryAuditContractRsp
  208. // @Failure 500 {object} app.Response
  209. // @Router /Erms3/QueryPendingAuditContract [get]
  210. // @Tags 风险管理v3
  211. func QueryPendingAuditContract(c *gin.Context) {
  212. appG := app.Gin{C: c}
  213. var req QryAuditContractReq
  214. if err := c.ShouldBind(&req); err != nil {
  215. logger.GetLogger().Errorf("parse query pending audit contract, %v", err)
  216. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  217. return
  218. }
  219. // 参数检查
  220. if req.ContractMode != 1 && req.ContractMode != 2 {
  221. logger.GetLogger().Errorf("ContractMode not in(1, 2)")
  222. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  223. return
  224. }
  225. if req.ContractMode == 1 {
  226. if req.ContractType != 1 && req.ContractType != -1 {
  227. logger.GetLogger().Errorf("ContractType not in(1, -1) when ContractMode=1")
  228. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  229. return
  230. }
  231. }
  232. // 去除最后的逗号
  233. if len(req.AccountIDs) > 0 && req.AccountIDs[len(req.AccountIDs)-1] == ',' {
  234. req.AccountIDs = req.AccountIDs[0 : len(req.AccountIDs)-1]
  235. }
  236. switch req.ContractMode {
  237. case 1: // 普通合同
  238. var m models.AuditContractModel
  239. if d, err := m.GetData(req.AccountIDs, req.ContractType); err == nil {
  240. rsp := make([]QryAuditContractRsp, 0)
  241. for _, v := range d {
  242. var rv QryAuditContractRsp
  243. if err := rv.ParseFromModel(v); err == nil {
  244. rsp = append(rsp, rv)
  245. } else {
  246. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  247. return
  248. }
  249. }
  250. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  251. }
  252. case 2: // 回购合同
  253. var m models.AutditContractHGModel
  254. if d, err := m.GetData(req.AccountIDs); err == nil {
  255. rsp := make([]QryAuditContractRsp, 0)
  256. for _, v := range d {
  257. var rv QryAuditContractRsp
  258. if err := rv.ParseFromHGModel(v); err == nil {
  259. rsp = append(rsp, rv)
  260. } else {
  261. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  262. return
  263. }
  264. }
  265. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  266. }
  267. default:
  268. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  269. }
  270. }