|
@@ -0,0 +1,143 @@
|
|
|
+package order
|
|
|
+
|
|
|
+import (
|
|
|
+ "app.yhyue.com/moapp/jybase/common"
|
|
|
+ "context"
|
|
|
+ "github.com/gogf/gf/v2/database/gdb"
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/pkg/errors"
|
|
|
+ "jyOrderManager/internal/consts"
|
|
|
+ "jyOrderManager/internal/model"
|
|
|
+ "log"
|
|
|
+)
|
|
|
+
|
|
|
+func DeleteBatchReturnMoneyInfo(ctx context.Context, param model.OrdersDeleteReturn) error {
|
|
|
+
|
|
|
+ //先查看该回款是否存在
|
|
|
+ returnData, _ := g.DB().Ctx(ctx).GetOne(ctx, "select * from return_money_record where id =? and state=1", param.Id)
|
|
|
+ if returnData == nil || returnData.IsEmpty() {
|
|
|
+ return errors.New("查不到该条回款记录信息")
|
|
|
+ }
|
|
|
+ returnDataMap := returnData.Map()
|
|
|
+ err := g.DB().Transaction(context.TODO(), func(ctx context.Context, tx gdb.TX) error {
|
|
|
+ //判断是cbs还是手动录入hffv
|
|
|
+ if common.Int64All(returnDataMap["flow_type"]) == 0 {
|
|
|
+ //cbs数据需要特殊处理
|
|
|
+ cbsData, err := g.DB(consts.DBCBS).GetOne(ctx, "select * from `transaction` where FIND_IN_SET(?,return_id)", param.Id)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("transaction 查询失败:", param.Id, err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if cbsData != nil {
|
|
|
+ //修改回款流水记录
|
|
|
+ updateTransactionData := map[string]interface{}{
|
|
|
+ "return_id": "",
|
|
|
+ "ISRELATION": 0,
|
|
|
+ }
|
|
|
+ _, err := g.DB(consts.DBCBS).Update(ctx, "transaction", updateTransactionData, map[string]interface{}{"id": (cbsData)["id"]})
|
|
|
+ if err != nil {
|
|
|
+ log.Println("transaction 更新失败", updateTransactionData, map[string]interface{}{"id": (cbsData)["id"]}, err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ returnListData := []map[string]interface{}{}
|
|
|
+ if common.ObjToString(returnDataMap["bank_flow"]) != "" {
|
|
|
+ var err error
|
|
|
+ returnListData_, err := tx.Query("select * from return_money_record where bank_flow =? and state=1", returnDataMap["bank_flow"])
|
|
|
+ if err != nil {
|
|
|
+ log.Println("查询回款记录失败 return_money_record", returnDataMap["bank_flow"], " bank_flow =? and state=1")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ returnListData = returnListData_.List()
|
|
|
+ } else {
|
|
|
+ returnListData = append(returnListData, returnDataMap)
|
|
|
+ }
|
|
|
+ for _, value := range returnListData {
|
|
|
+ //查看该订单是否还存在别的回款信息
|
|
|
+ returnData, err := tx.Query("select * from return_money_record where order_code =? and state=1", value["order_code"])
|
|
|
+ if returnData == nil || returnData.Len() == 0 || err != nil {
|
|
|
+ log.Println("根据订单号查询回款记录失败:", err, value["order_code"], " order_code =? and state=1")
|
|
|
+ return errors.New("没查到")
|
|
|
+ }
|
|
|
+ updateData := map[string]interface{}{
|
|
|
+ "state": 0,
|
|
|
+ }
|
|
|
+ _, err = tx.Update("return_money_record", updateData, map[string]interface{}{"id": value["id"]})
|
|
|
+ if err != nil {
|
|
|
+ log.Println("更新回款记录失败:", err, updateData, map[string]interface{}{"id": value["id"]})
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ orderData := map[string]interface{}{}
|
|
|
+ if returnData.Len() > 1 {
|
|
|
+ //说明还有别的订单,订单回款需要改为部分回款
|
|
|
+ orderData["return_status"] = 2
|
|
|
+ } else if returnData.Len() == 1 {
|
|
|
+ //回款改为未回款
|
|
|
+ orderData["return_status"] = 0
|
|
|
+ }
|
|
|
+ _, err = tx.Update("dataexport_order", orderData, map[string]interface{}{"order_code": value["order_code"]})
|
|
|
+ if err != nil {
|
|
|
+ log.Println("更新dataexport_order失败:", orderData, map[string]interface{}{"order_code": value["order_code"]}, err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ returnListData := []map[string]interface{}{}
|
|
|
+ //手动录入处理简单,也会存在多个订单
|
|
|
+ if common.Int64All(returnDataMap["return_type"]) == 3 {
|
|
|
+ //回款方式,3-对公转账 通过银行流水号查找相同的
|
|
|
+ returnListData_, err := tx.Query("select * from return_money_record where bank_flow =? and state=1 and return_type=3", returnDataMap["bank_flow"])
|
|
|
+ if err != nil {
|
|
|
+ log.Println("对公转账 流水号查询回款记录失败 return_money_record", returnDataMap["bank_flow"], "bank_flow =? and state=1 and return_type=3")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ returnListData = returnListData_.List()
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //回款方式,通过支付单号查找相同的
|
|
|
+ returnListData_, err := tx.Query("select * from return_money_record where return_code =? and state=1 and return_type!=3", returnDataMap["return_code"])
|
|
|
+ if err != nil {
|
|
|
+ log.Println("回款单号查询回款记录失败 return_money_record", returnDataMap["return_code"], " return_code =? and state=1 and return_type!=3")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ returnListData = returnListData_.List()
|
|
|
+ }
|
|
|
+ for _, value := range returnListData {
|
|
|
+ //查看该订单是否还存在别的回款信息
|
|
|
+ returnData, err := tx.Query("select * from return_money_record where order_code =? and state=1", value["order_code"])
|
|
|
+ if returnData == nil || len(returnData) == 0 || err != nil {
|
|
|
+ log.Println("根据订单号查询回款记录失败:", err, value["order_code"], " order_code =? and state=1")
|
|
|
+ return errors.New("没查到")
|
|
|
+ }
|
|
|
+ updateData := map[string]interface{}{
|
|
|
+ "state": 0,
|
|
|
+ }
|
|
|
+ _, err = tx.Update("return_money_record", updateData, map[string]interface{}{"id": value["id"]})
|
|
|
+ if err != nil {
|
|
|
+ log.Println("更新回款记录失败:", err, updateData, map[string]interface{}{"id": value["id"]})
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ orderData := map[string]interface{}{}
|
|
|
+ if returnData.Len() > 1 {
|
|
|
+ //说明还有别的订单,订单回款需要改为部分回款
|
|
|
+ orderData["return_status"] = 2
|
|
|
+ } else if returnData.Len() == 1 {
|
|
|
+ //回款改为未回款
|
|
|
+ orderData["return_status"] = 0
|
|
|
+ }
|
|
|
+ _, err = tx.Update("dataexport_order", orderData, map[string]interface{}{"order_code": value["order_code"]})
|
|
|
+ if err != nil {
|
|
|
+ log.Println("更新dataexport_order失败:", orderData, map[string]interface{}{"order_code": value["order_code"]}, err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|