//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 //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 //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 // Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package accounting import ( "context" "gd_management/apis" "gd_management/common.in/storage" "gd_management/common.in/utils" "gd_management/errors" "encoding/json" "fmt" "github.com/astaxie/beego/orm" "go.uber.org/zap" "strings" "time" ) func UpdateBillDetail(ctx context.Context, req *apis.UpdateBillDetailReq, reply *apis.UpdateBillDetailReply) error { //o := orm.NewOrm() // 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 if req.BillDetailId == 0 { return errors.ArgsError } if req.ChargeCount == 0 { return nil } task := func(o orm.Ormer) error { remark := "" //sql := "select merchant_data_api_id,merchant_id,month,charge_count,remark from t_gd_bill_detail where id=?" sql := "select merchant_data_api_id,merchant_id,month,charge_count,remark,unit_price from t_gd_bill_detail where id=?" beforChargeCount := 0 merchantDataApiId := 0 merchantId := int64(0) month := "" unitPrice := float64(0) err := o.Raw(sql, req.BillDetailId).QueryRow(&merchantDataApiId, &merchantId, &month, &beforChargeCount, &remark, &unitPrice) if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } billStatus := 0 sql = "select bill_status from t_gd_bill where bill_time=? and merchant_id=? and bill_type=2" err = o.Raw(sql, month,merchantId).QueryRow(&billStatus) if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } if billStatus == 1{ return errors.ConfirmError } // 获取单价 /*unitPrice := float64(0) sql = "select unit_price from t_gd_merchant_data_api where id=?" err = o.Raw(sql, merchantDataApiId).QueryRow(&unitPrice) if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError }*/ remarkList := []apis.BillDetailRemark{} if remark != "" { json.Unmarshal([]byte(remark), &remarkList) } tmp := apis.BillDetailRemark{} tmp.Befor = beforChargeCount tmp.After = req.ChargeCount + beforChargeCount tmp.Time = time.Now().Format("2006-01-02 15:04:05") tmp.UserName = req.UserName amountChange := unitPrice * float64(req.ChargeCount) remarkList = append(remarkList, tmp) remarkByte, _ := json.Marshal(remarkList) _, err = o.Raw("update t_gd_bill_detail set amount=amount+?,charge_count=?,remark=? where id=?", amountChange, tmp.After, string(remarkByte), req.BillDetailId).Exec() if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } _, err = o.Raw("update t_gd_bill set amount=amount+?,balance=balance-? where merchant_id=? and bill_time=?", amountChange, amountChange, merchantId, month).Exec() if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } sql = fmt.Sprintf("update t_gd_merchants set balance=balance-%f,bill_balance=bill_balance-%f where id=?", amountChange, amountChange) _, err = o.Raw(sql, merchantId).Exec() if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } if amountChange < 0 { // 新增充值记录 balance := float64(0) err = o.Raw("select balance from t_gd_merchants where id=?", merchantId).QueryRow(&balance) if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } bill := &apis.GdBill{} bill.MerchantId = merchantId bill.BillType = 1 bill.BillTime = time.Now().Format("2006-01") bill.BillCreateTime = time.Now().Format("2006-01-02") bill.Amount = -amountChange bill.UserName = req.UserName remarkTmp := []string{} newMonth := strings.Replace(month,"-","年",-1) + "月" remarkTmp = append(remarkTmp, fmt.Sprintf("%s账单核定返还%.2f元",newMonth, -amountChange)) remarkTmpByte, _ := json.Marshal(remarkTmp) bill.Remark = string(remarkTmpByte) bill.Balance = balance bill.CreatedAt = time.Now().Unix() _, err = o.Insert(bill) if err != nil { l.Error("func", zap.String("call", "UpdateBillDetail"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } } return nil } tasks := []storage.DbaTasker{} tasks = append(tasks, storage.GenerateDbaTask(task)) storage.ExecTrans(tasks...) //l.Debug(utils.MarshalJsonString(req, reply)) return nil }