update_bill_detail.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //select t1.id as consume_id,t1.day,t5.data_api_name,t2.alias,t1.total_count,t1.valid_count,t1.charge_count,t1.amount,t2.unit_price from t_gd_consume as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id
  2. //select t1.id as consume_id,t1.day,t5.data_api_name,t2.alias,t1.total_count,t1.valid_count,t1.charge_count,t1.amount,t2.unit_price from t_gd_consume as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id where t1.month="2020-02" and t2.merchant_id=8
  3. //select t1.id as consume_id,t1.day,t5.data_api_name,t1.merchant_data_api_id,t2.alias,t1.total_count,t1.valid_count,t1.charge_count,t1.amount,t2.unit_price from t_gd_consume as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id where t1.month="2020-02" and t2.merchant_id=8
  4. // Copyright 2019 getensh.com. All rights reserved.
  5. // Use of this source code is governed by getensh.com.
  6. package accounting
  7. import (
  8. "context"
  9. "gd_management/apis"
  10. "gd_management/common.in/storage"
  11. "gd_management/common.in/utils"
  12. "gd_management/errors"
  13. "encoding/json"
  14. "fmt"
  15. "github.com/astaxie/beego/orm"
  16. "go.uber.org/zap"
  17. "strings"
  18. "time"
  19. )
  20. func UpdateBillDetail(ctx context.Context, req *apis.UpdateBillDetailReq, reply *apis.UpdateBillDetailReply) error {
  21. //o := orm.NewOrm()
  22. // select t1.id,t3.merchant_name,t2.merchant_id,t1.day,t5.data_api_name,t2.alias,t1.charge_count,t1.amount from t_gd_bill1 as t1 left join t_gd_merchant_data_api t2 on t1.merchant_data_api_id=t2.id left join t_gd_merchants as t3 on t2.merchant_id=t3.id left join t_gd_data_api_query_type as t4 on t4.id=t2.query_type_id left join t_gd_data_api as t5 on t5.id=t4.data_api_id
  23. if req.BillDetailId == 0 {
  24. return errors.ArgsError
  25. }
  26. if req.ChargeCount == 0 {
  27. return nil
  28. }
  29. task := func(o orm.Ormer) error {
  30. remark := ""
  31. //sql := "select merchant_data_api_id,merchant_id,month,charge_count,remark from t_gd_bill_detail where id=?"
  32. sql := "select merchant_data_api_id,merchant_id,month,charge_count,remark,unit_price from t_gd_bill_detail where id=?"
  33. beforChargeCount := 0
  34. merchantDataApiId := 0
  35. merchantId := int64(0)
  36. month := ""
  37. unitPrice := float64(0)
  38. err := o.Raw(sql, req.BillDetailId).QueryRow(&merchantDataApiId, &merchantId, &month, &beforChargeCount, &remark, &unitPrice)
  39. if err != nil {
  40. l.Error("func",
  41. zap.String("call", "UpdateBillDetail"),
  42. zap.String("args", utils.MarshalJsonString(req)),
  43. zap.String("error", err.Error()))
  44. return errors.DataBaseError
  45. }
  46. billStatus := 0
  47. sql = "select bill_status from t_gd_bill where bill_time=? and merchant_id=? and bill_type=2"
  48. err = o.Raw(sql, month,merchantId).QueryRow(&billStatus)
  49. if err != nil {
  50. l.Error("func",
  51. zap.String("call", "UpdateBillDetail"),
  52. zap.String("args", utils.MarshalJsonString(req)),
  53. zap.String("error", err.Error()))
  54. return errors.DataBaseError
  55. }
  56. if billStatus == 1{
  57. return errors.ConfirmError
  58. }
  59. // 获取单价
  60. /*unitPrice := float64(0)
  61. sql = "select unit_price from t_gd_merchant_data_api where id=?"
  62. err = o.Raw(sql, merchantDataApiId).QueryRow(&unitPrice)
  63. if err != nil {
  64. l.Error("func",
  65. zap.String("call", "UpdateBillDetail"),
  66. zap.String("args", utils.MarshalJsonString(req)),
  67. zap.String("error", err.Error()))
  68. return errors.DataBaseError
  69. }*/
  70. remarkList := []apis.BillDetailRemark{}
  71. if remark != "" {
  72. json.Unmarshal([]byte(remark), &remarkList)
  73. }
  74. tmp := apis.BillDetailRemark{}
  75. tmp.Befor = beforChargeCount
  76. tmp.After = req.ChargeCount + beforChargeCount
  77. tmp.Time = time.Now().Format("2006-01-02 15:04:05")
  78. tmp.UserName = req.UserName
  79. amountChange := unitPrice * float64(req.ChargeCount)
  80. remarkList = append(remarkList, tmp)
  81. remarkByte, _ := json.Marshal(remarkList)
  82. _, err = o.Raw("update t_gd_bill_detail set amount=amount+?,charge_count=?,remark=? where id=?", amountChange, tmp.After, string(remarkByte), req.BillDetailId).Exec()
  83. if err != nil {
  84. l.Error("func",
  85. zap.String("call", "UpdateBillDetail"),
  86. zap.String("args", utils.MarshalJsonString(req)),
  87. zap.String("error", err.Error()))
  88. return errors.DataBaseError
  89. }
  90. _, err = o.Raw("update t_gd_bill set amount=amount+?,balance=balance-? where merchant_id=? and bill_time=?", amountChange, amountChange, merchantId, month).Exec()
  91. if err != nil {
  92. l.Error("func",
  93. zap.String("call", "UpdateBillDetail"),
  94. zap.String("args", utils.MarshalJsonString(req)),
  95. zap.String("error", err.Error()))
  96. return errors.DataBaseError
  97. }
  98. sql = fmt.Sprintf("update t_gd_merchants set balance=balance-%f,bill_balance=bill_balance-%f where id=?", amountChange, amountChange)
  99. _, err = o.Raw(sql, merchantId).Exec()
  100. if err != nil {
  101. l.Error("func",
  102. zap.String("call", "UpdateBillDetail"),
  103. zap.String("args", utils.MarshalJsonString(req)),
  104. zap.String("error", err.Error()))
  105. return errors.DataBaseError
  106. }
  107. if amountChange < 0 {
  108. // 新增充值记录
  109. balance := float64(0)
  110. err = o.Raw("select balance from t_gd_merchants where id=?", merchantId).QueryRow(&balance)
  111. if err != nil {
  112. l.Error("func",
  113. zap.String("call", "UpdateBillDetail"),
  114. zap.String("args", utils.MarshalJsonString(req)),
  115. zap.String("error", err.Error()))
  116. return errors.DataBaseError
  117. }
  118. bill := &apis.GdBill{}
  119. bill.MerchantId = merchantId
  120. bill.BillType = 1
  121. bill.BillTime = time.Now().Format("2006-01")
  122. bill.BillCreateTime = time.Now().Format("2006-01-02")
  123. bill.Amount = -amountChange
  124. bill.UserName = req.UserName
  125. remarkTmp := []string{}
  126. newMonth := strings.Replace(month,"-","年",-1) + "月"
  127. remarkTmp = append(remarkTmp, fmt.Sprintf("%s账单核定返还%.2f元",newMonth, -amountChange))
  128. remarkTmpByte, _ := json.Marshal(remarkTmp)
  129. bill.Remark = string(remarkTmpByte)
  130. bill.Balance = balance
  131. bill.CreatedAt = time.Now().Unix()
  132. _, err = o.Insert(bill)
  133. if err != nil {
  134. l.Error("func",
  135. zap.String("call", "UpdateBillDetail"),
  136. zap.String("args", utils.MarshalJsonString(req)),
  137. zap.String("error", err.Error()))
  138. return errors.DataBaseError
  139. }
  140. }
  141. return nil
  142. }
  143. tasks := []storage.DbaTasker{}
  144. tasks = append(tasks, storage.GenerateDbaTask(task))
  145. storage.ExecTrans(tasks...)
  146. //l.Debug(utils.MarshalJsonString(req, reply))
  147. return nil
  148. }