123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package accounting
- import (
- "context"
- "gd_management/apis"
- "gd_management/common.in/storage"
- "gd_management/errors"
- "fmt"
- "github.com/astaxie/beego/orm"
- "strings"
- "time"
- )
- func GenerateBill(ctx context.Context, req *apis.GenerateBillReq, reply *apis.GenerateBillReply) error {
- if req.Month == "" {
- req.Month = time.Now().AddDate(0, -1, 0).Format("2006-01")
- }
- task := func(o orm.Ormer) error {
- //o.Using()
- // 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;
- // 获取账单详情
- /*var id int
- err := o.Raw("select id from t_gd_bill where bill_time=? limit 1", req.Month).QueryRow(&id)
- if err == nil {
- return nil
- }*/
- billDetailList := []apis.GdBillDetail{}
- //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;"
- 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`
- _, err := o.Raw(sql, req.Month).QueryRows(&billDetailList)
- if err != nil {
- return err
- }
- billLen := len(billDetailList)
- if billLen > 0 {
- _, err := o.InsertMulti(billLen, billDetailList)
- if err != nil {
- if strings.Contains(err.Error(), "Duplicate") {
- return errors.BillGeneratedError
- }
- return err
- }
- }
- billList := []apis.GdBill{}
- sql = "select month as bill_time,merchant_id,sum(amount) as amount from t_gd_consume where month=? GROUP BY merchant_id"
- _, err = o.Raw(sql, req.Month).QueryRows(&billList)
- if err != nil {
- return err
- }
- // 生成账单
- billLen = len(billList)
- timeNow := time.Now()
- timeNowSecoond := timeNow.Unix()
- timeNowStr := timeNow.Format("2006-01-02")
- if billLen > 0 {
- for index, _ := range billList {
- // 查询账余额
- err = o.Raw("select bill_balance from t_gd_merchants where id=?", billList[index].MerchantId).QueryRow(&billList[index].Balance)
- if err != nil {
- // 被删除账户
- if err == orm.ErrNoRows {
- continue
- }
- return err
- }
- // 账单余额为余额减去账单金额
- billList[index].Balance = billList[index].Balance - billList[index].Amount
- sql := fmt.Sprintf("update t_gd_merchants set bill_balance=bill_balance-%f where id=?", billList[index].Amount)
- _, err = o.Raw(sql, billList[index].MerchantId).Exec()
- if err != nil {
- return err
- }
- billList[index].BillCreateTime = timeNowStr
- billList[index].CreatedAt = timeNowSecoond
- billList[index].BillType = 2
- }
- // 批量插入账单
- _, err := o.InsertMulti(billLen, billList)
- if err != nil {
- return err
- }
- }
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(task))
- storage.ExecTrans(tasks...)
- //l.Debug(utils.MarshalJsonString(req, reply))
- return nil
- }
|