generate_bill.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package accounting
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/common.in/storage"
  6. "gd_management/errors"
  7. "fmt"
  8. "github.com/astaxie/beego/orm"
  9. "strings"
  10. "time"
  11. )
  12. func GenerateBill(ctx context.Context, req *apis.GenerateBillReq, reply *apis.GenerateBillReply) error {
  13. if req.Month == "" {
  14. req.Month = time.Now().AddDate(0, -1, 0).Format("2006-01")
  15. }
  16. task := func(o orm.Ormer) error {
  17. //o.Using()
  18. // select month,merchant_data_api_id,merchant_id,sum(total_count) as total_count,sum(valid_count) as valid_count,sum(charge_count) as charge_count ,sum(amount) as amount from t_gd_consume where month="2022-10" GROUP BY merchant_data_api_id;
  19. // 获取账单详情
  20. /*var id int
  21. err := o.Raw("select id from t_gd_bill where bill_time=? limit 1", req.Month).QueryRow(&id)
  22. if err == nil {
  23. return nil
  24. }*/
  25. billDetailList := []apis.GdBillDetail{}
  26. //sql := "select month,merchant_data_api_id,merchant_id,sum(total_count) as total_count,sum(valid_count) as valid_count,sum(charge_count) as charge_count ,sum(amount) as amount from t_gd_consume where month=? GROUP BY merchant_data_api_id;"
  27. sql := `select t1.month ,t1.merchant_data_api_id,t1.merchant_id,sum(t1.total_count) as total_count,sum(t1.valid_count) as valid_count,sum(t1.charge_count) as charge_count ,sum(t1.amount) as amount,if(t2.alias is null or t2.alias = "",t5.data_api_name,t2.alias) as alias,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 month=? GROUP BY merchant_data_api_id`
  28. _, err := o.Raw(sql, req.Month).QueryRows(&billDetailList)
  29. if err != nil {
  30. return err
  31. }
  32. billLen := len(billDetailList)
  33. if billLen > 0 {
  34. _, err := o.InsertMulti(billLen, billDetailList)
  35. if err != nil {
  36. if strings.Contains(err.Error(), "Duplicate") {
  37. return errors.BillGeneratedError
  38. }
  39. return err
  40. }
  41. }
  42. billList := []apis.GdBill{}
  43. sql = "select month as bill_time,merchant_id,sum(amount) as amount from t_gd_consume where month=? GROUP BY merchant_id"
  44. _, err = o.Raw(sql, req.Month).QueryRows(&billList)
  45. if err != nil {
  46. return err
  47. }
  48. // 生成账单
  49. billLen = len(billList)
  50. timeNow := time.Now()
  51. timeNowSecoond := timeNow.Unix()
  52. timeNowStr := timeNow.Format("2006-01-02")
  53. if billLen > 0 {
  54. for index, _ := range billList {
  55. // 查询账余额
  56. err = o.Raw("select bill_balance from t_gd_merchants where id=?", billList[index].MerchantId).QueryRow(&billList[index].Balance)
  57. if err != nil {
  58. // 被删除账户
  59. if err == orm.ErrNoRows {
  60. continue
  61. }
  62. return err
  63. }
  64. // 账单余额为余额减去账单金额
  65. billList[index].Balance = billList[index].Balance - billList[index].Amount
  66. sql := fmt.Sprintf("update t_gd_merchants set bill_balance=bill_balance-%f where id=?", billList[index].Amount)
  67. _, err = o.Raw(sql, billList[index].MerchantId).Exec()
  68. if err != nil {
  69. return err
  70. }
  71. billList[index].BillCreateTime = timeNowStr
  72. billList[index].CreatedAt = timeNowSecoond
  73. billList[index].BillType = 2
  74. }
  75. // 批量插入账单
  76. _, err := o.InsertMulti(billLen, billList)
  77. if err != nil {
  78. return err
  79. }
  80. }
  81. return nil
  82. }
  83. tasks := []storage.DbaTasker{}
  84. tasks = append(tasks, storage.GenerateDbaTask(task))
  85. storage.ExecTrans(tasks...)
  86. //l.Debug(utils.MarshalJsonString(req, reply))
  87. return nil
  88. }