qryContract.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. "github.com/gin-gonic/gin"
  10. "mtp2_if/global/app"
  11. "mtp2_if/global/e"
  12. "mtp2_if/logger"
  13. "mtp2_if/models"
  14. "net/http"
  15. "strconv"
  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. AccountId uint64 `form:"accountid" 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:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 商品代码
  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. DeliveryGoodsName string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  106. DeliveryGoodscode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 品种代码
  107. ApplyStatus uint32 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 状态
  108. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  109. SignDate string `json:"signdate" xorm:"'SIGNDATE'"` // 签订日期
  110. deliverGoods map[int32]models.Deliverygoods // 不参与json编码,作为id转code缓存
  111. wrStandards map[int64]models.Wrstandard // 不参与json编码,作为id转code缓存
  112. }
  113. // 转换交割商品ID为商品代码
  114. func (r *QryAuditContractRsp) DeliverGoodsId2Code(id int32) string {
  115. // init
  116. if r.deliverGoods == nil || len(r.deliverGoods) == 0 {
  117. r.deliverGoods = make(map[int32]models.Deliverygoods)
  118. if dg, err := models.GetDeliverGoods(); err == nil {
  119. for _, v := range dg {
  120. r.deliverGoods[v.Deliverygoodsid] = v
  121. }
  122. }
  123. }
  124. if v, ok := r.deliverGoods[id]; ok {
  125. return v.Deliverygoodscode
  126. }
  127. return ""
  128. }
  129. // 转换仓单商品ID为代code
  130. func (r *QryAuditContractRsp) WrStandardID2Code(id int64) string {
  131. // init
  132. if r.wrStandards == nil || len(r.wrStandards) == 0 {
  133. r.wrStandards = make(map[int64]models.Wrstandard)
  134. if wg, err := models.GetWrstandards(); err == nil {
  135. for _, v := range wg {
  136. r.wrStandards[v.Wrstandardid] = v
  137. }
  138. }
  139. }
  140. if v, ok := r.wrStandards[id]; ok {
  141. return v.Wrstandardcode
  142. }
  143. return ""
  144. }
  145. // 构建响应数据
  146. func (r *QryAuditContractRsp) ParseFromModel(val models.AuditContractModel) error {
  147. r.SpotContractId = val.SpotContractId
  148. r.MatchAccountId = val.MatchAccountId
  149. r.MatchCustomerName = val.MatchCustomerName
  150. r.AccountId = val.AccountId
  151. r.CustomerName = val.CustomerName
  152. r.ApplyStatus = val.ApplyStatus
  153. // 解析DetailJSON字段
  154. details := make([]DetailJSON, 0)
  155. if err := json.Unmarshal([]byte(val.DetailJSON), &details); err != nil {
  156. logger.GetLogger().Errorf("parse detailJson field fail:%v", err)
  157. return err
  158. }
  159. // 现在合同只有一张单
  160. detail := details[0]
  161. r.WrstandardName = detail.WrStandardName
  162. r.DeliveryGoodsName = detail.DeliveryGoodsName
  163. r.EnumdicName = detail.UnitName
  164. // 汇总未定价量
  165. r.UnpricedQty = 0
  166. for _, v := range detail.SpotPointOrderVoList {
  167. r.UnpricedQty += float64(v.Qty)
  168. }
  169. // 汇总定价量
  170. r.PricedQty = 0
  171. for _, v := range detail.SpotPriceOrderList {
  172. r.PricedQty += float64(v.Qty)
  173. }
  174. // 总量
  175. r.TotaldQty = r.UnpricedQty + r.PricedQty
  176. // 转换仓单商品ID为代code
  177. r.Wrstandardcode = r.WrStandardID2Code(int64(detail.WrStandardID))
  178. // 转换交割商品ID为商品代码
  179. r.DeliveryGoodscode = r.DeliverGoodsId2Code(int32(detail.DeliveryGoodsID))
  180. return nil
  181. }
  182. // 构建响应数据
  183. func (r *QryAuditContractRsp) ParseFromHGModel(val models.AutditContractHGModel) error {
  184. r.SpotContractId = val.SpotContractId
  185. r.MatchCustomerName = val.MatchCustomerName
  186. r.AccountId = val.AccountId
  187. r.CustomerName = val.CustomerName
  188. r.EnumdicName = val.EnumdicName
  189. r.WrstandardName = val.WrstandardName
  190. r.Wrstandardcode = val.Wrstandardcode
  191. r.ApplyStatus = val.ApplyStatus
  192. r.SignDate = val.SignDate
  193. r.MatchAccountId = val.MatchAccountId
  194. r.TotaldQty = val.ContractQty
  195. return nil
  196. }
  197. // QueryPendingAuditContract 查询待审核合同
  198. // @Summary 查询待审核合同
  199. // @Produce json
  200. // @Security ApiKeyAuth
  201. // @Param accountid query int true "账号ID"
  202. // @Param contracttype query int true "合同类型 1-采购 -1-销售"
  203. // @Param contractmode query int true "合同模式 1-普通 2-回购"
  204. // @Success 200 {array} QryAuditContractRsp
  205. // @Failure 500 {object} app.Response
  206. // @Router /Erms3/QueryPendingAuditContract [get]
  207. // @Tags 风险管理v3
  208. func QueryPendingAuditContract(c *gin.Context) {
  209. appG := app.Gin{C: c}
  210. var req QryAuditContractReq
  211. if err := c.ShouldBind(&req); err != nil {
  212. logger.GetLogger().Errorf("parse query pending audit contract, %v", err)
  213. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  214. return
  215. }
  216. // 参数检查
  217. if req.ContractMode != 1 && req.ContractMode != 2 {
  218. logger.GetLogger().Errorf("ContractMode not in(1, 2)")
  219. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  220. return
  221. }
  222. if req.ContractMode == 1 {
  223. if req.ContractType != 1 && req.ContractType != -1 {
  224. logger.GetLogger().Errorf("ContractType not in(1, -1) when ContractMode=1")
  225. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  226. return
  227. }
  228. }
  229. switch req.ContractMode {
  230. case 1: // 普通合同
  231. var m models.AuditContractModel
  232. if d, err := m.GetData(req.AccountId, req.ContractType); err == nil {
  233. rsp := make([]QryAuditContractRsp, 0)
  234. for _, v := range d {
  235. var rv QryAuditContractRsp
  236. if err := rv.ParseFromModel(v); err == nil {
  237. rsp = append(rsp, rv)
  238. } else {
  239. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  240. return
  241. }
  242. }
  243. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  244. }
  245. case 2: // 回购合同
  246. var m models.AutditContractHGModel
  247. if d, err := m.GetData(req.AccountId); err == nil {
  248. rsp := make([]QryAuditContractRsp, 0)
  249. for _, v := range d {
  250. var rv QryAuditContractRsp
  251. if err := rv.ParseFromHGModel(v); err == nil {
  252. rsp = append(rsp, rv)
  253. } else {
  254. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  255. return
  256. }
  257. }
  258. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  259. }
  260. default:
  261. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  262. }
  263. }