k.zhang 1 anno fa
parent
commit
0fe4ef8ab2

+ 2 - 0
apis/shanghu/base.go

@@ -27,5 +27,7 @@ func InitShangHuRouter(engine *gin.RouterGroup) {
 		v1.POST("/client/unified/order", UnifiedOrder)                      //生成订单
 		v1.POST("/client/pay/callback", PayCallBack)                        //支付回调
 		v1.POST("/client/merchant/card/list", MerchantClientCardList)       //c端商户卡列表
+		v1.POST("/merchant/card.canvas/get", GetMerchantCanvasCard)         //画布获取详情
+		v1.POST("/upload/picture", UploadPicture)                           //上传图片
 	}
 }

+ 207 - 0
apis/shanghu/merchant.card.go

@@ -131,6 +131,213 @@ func GetMerchantCard(c *gin.Context) {
 
 }
 
+func GetMerchantCanvasCard(c *gin.Context) {
+	var inData models.GetMerchantCardRequest
+	var outData models.GetMerchantCanvasCardReply
+	var sqlData shanghu.MerchantCard
+	var project []int64
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	sqlData.ID = inData.MerchantId
+
+	merchantCard, err := sqlData.GetMerchantCard()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	//  result: {
+	//      // 背景
+	//      static: {
+	//        background: "../../images/背景.jpg"
+	//      },
+	//      // 轮播图
+	//      swiperList: [{
+	//        id: 0,
+	//        url: 'http://lingyang.shisanmiao.com/shanghuka/%E8%B5%84%E6%BA%90%2045.png'
+	//      }, {
+	//        id: 1,
+	//        url: 'http://lingyang.shisanmiao.com/shanghuka/%E8%B5%84%E6%BA%90%2045.png',
+	//      }],
+	//      // 按钮数据
+	//      rightButton: [{
+	//        imageUrl: '../../images/资源 24.png',
+	//      },
+	//      {
+	//        imageUrl: '../../images/资源 25.png',
+	//      },
+	//      {
+	//        imageUrl: '../../images/资源 26.png',
+	//      },
+	//      {
+	//        imageUrl: '../../images/资源 28.png',
+	//      },
+	//      ],
+	//      // 套餐内容
+	//      dataList: [{
+	//        name: '小份黄焖鸡小份黄焖鸡小份',
+	//        count: 3,
+	//        price: '60'
+	//      },
+	//      {
+	//        name: '中份黄焖鸡小份黄焖鸡',
+	//        count: 3,
+	//        price: '70'
+	//      },
+	//      {
+	//        name: '大份黄焖鸡小份黄焖鸡',
+	//        count: 3,
+	//        price: '80'
+	//      },
+	//
+	//
+	//      ],
+	//
+	//      // 购买须知
+	//      buyKnowList: [{
+	//        icon: '../../images/资源 46.png',
+	//        title: '有效期',
+	//        text: '至2021-0520 23:00'
+	//      },
+	//      {
+	//        icon: '../../images/资源 47.png',
+	//        title: '使用时间',
+	//        text: '周一至周日10:00-23:00可用'
+	//      },
+	//      {
+	//        icon: '../../images/资源 48.png',
+	//        title: '限购数量',
+	//        text: '每位用户限购1张'
+	//      },
+	//      {
+	//        icon: '../../images/资源 49.png',
+	//        title: '使用规则',
+	//        text: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
+	//      },
+	//      ],
+	//
+	//      // 使用门店
+	//      shopData: {
+	//        imageUrl: '',
+	//        name: '黄焖鸡米饭(高新万达店)',
+	//        starRating: 4,
+	//        address: '地点:济南市高新区工业南路340号1层',
+	//        distance: '1.9km'
+	//      },
+	//
+	//      // 二维码变量
+	//      base64Img: "",
+	//
+	//      // 图片弹框
+	//      fenxiangShow: false,
+	//    }
+
+	outData.Static.Background = "/images/背景.jpg"
+	var swiperList []models.SwiperList
+	var rightButton models.RightButton
+	err = json.Unmarshal([]byte(merchantCard.Picture), &swiperList)
+	if err != nil {
+		fmt.Println("picture")
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	outData.SwiperList = swiperList
+
+	rightButton.ImageURL = "/images/资源 24.png"
+	outData.RightButton = append(outData.RightButton, rightButton)
+	rightButton.ImageURL = "/images/资源 25.png"
+	outData.RightButton = append(outData.RightButton, rightButton)
+	rightButton.ImageURL = "/images/资源 26.png"
+	outData.RightButton = append(outData.RightButton, rightButton)
+	rightButton.ImageURL = "/images/资源 28.png"
+	outData.RightButton = append(outData.RightButton, rightButton)
+
+	var dataList models.DataList
+
+	err = json.Unmarshal([]byte(merchantCard.CardProjectData), &project)
+	if err != nil {
+		fmt.Println("picture2")
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	//merchantCard.CardProjectData = ""
+	for i := 0; i < len(project); i++ {
+		var cardProject shanghu.MerchantCardProject
+		cardProject.ID = project[i]
+		projectInfo, err := cardProject.GetMerchantCardProject()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		dataList.Name = projectInfo.ProjectName
+		dataList.Count = 1
+		dataList.Price = projectInfo.ProjectUnitPrice
+		outData.DataList = append(outData.DataList, dataList)
+	}
+	var buy models.BuyKnowList
+
+	buy.Icon = "/images/资源 46.png"
+	buy.Title = "有效期"
+	buy.Text = "至" + tools.TimeToStr(merchantCard.MerchantCardTime)
+	outData.BuyKnowList = append(outData.BuyKnowList, buy)
+	buy.Icon = "/images/资源 48.png"
+	buy.Title = "限购数量"
+	buy.Text = "每位用户限购1张"
+	outData.BuyKnowList = append(outData.BuyKnowList, buy)
+	buy.Icon = "/images/资源 49.png"
+	buy.Title = "使用规则"
+	buy.Text = merchantCard.UseRule
+	outData.BuyKnowList = append(outData.BuyKnowList, buy)
+
+	var shopData models.ShopData
+	var merchant shanghu.Merchant
+	merchant.OpenId = merchantCard.MerchantOpenID
+	merchantInfo, err := merchant.GetMerchant()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	shopData.Name = merchantInfo.MerchantName
+	shopData.Address = merchantInfo.Address
+	outData.ShopData = shopData
+
+	outData.FenxiangShow = false
+
+	outData.MerchantAmount, err = decimal.NewFromString(merchantCard.CardPrice)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	outData.MerchantOriginalAmount = outData.MerchantAmount.Add(decimal.NewFromInt(50))
+
+	//err = json.Unmarshal([]byte(merchantCard.CardProjectData), &project)
+	//if err != nil {
+	//	app.Error(c, 500, err, err.Error())
+	//	return
+	//}
+	//
+	//merchantCard.CardProjectData = ""
+	//for i := 0; i < len(project); i++ {
+	//	var cardProject shanghu.MerchantCardProject
+	//	cardProject.ID = project[i]
+	//	projectInfo, err := cardProject.GetMerchantCardProject()
+	//	if err != nil {
+	//		app.Error(c, 500, err, err.Error())
+	//		return
+	//	}
+	//	merchantCard.CardProjectData = merchantCard.CardProjectData + projectInfo.ProjectName + "x1 "
+	//}
+
+	app.OK(c, outData, app.Success)
+
+}
+
 func MerchantCardList(c *gin.Context) {
 	var inData models.MerchantCardListRequest
 	var sqlData shanghu.MerchantCard

+ 45 - 1
apis/shanghu/models/card.go

@@ -1,6 +1,9 @@
 package models
 
-import "time"
+import (
+	"github.com/shopspring/decimal"
+	"time"
+)
 
 type CreateMerchantCardRequest struct {
 	MerchantCardName  string  `json:"merchant_card_name"`  // 商户卡名称
@@ -36,6 +39,47 @@ type GetMerchantCardReply struct {
 	RebateRate        int64  `json:"rebate_rate"`
 }
 
+// 画布详情
+type GetMerchantCanvasCardReply struct {
+	Static                 Static          `json:"static"`
+	SwiperList             []SwiperList    `json:"swiperList"`
+	RightButton            []RightButton   `json:"rightButton"`
+	DataList               []DataList      `json:"dataList"`
+	BuyKnowList            []BuyKnowList   `json:"buyKnowList"`
+	ShopData               ShopData        `json:"shopData"`
+	Base64Img              string          `json:"base64Img"`
+	FenxiangShow           bool            `json:"fenxiangShow"`
+	MerchantAmount         decimal.Decimal `json:"merchantAmount"`
+	MerchantOriginalAmount decimal.Decimal `json:"merchantOriginalAmount"`
+}
+type Static struct {
+	Background string `json:"background"`
+}
+type SwiperList struct {
+	ID  int    `json:"id"`
+	URL string `json:"url"`
+}
+type RightButton struct {
+	ImageURL string `json:"imageUrl"`
+}
+type DataList struct {
+	Name  string `json:"name"`
+	Count int    `json:"count"`
+	Price string `json:"price"`
+}
+type BuyKnowList struct {
+	Icon  string `json:"icon"`
+	Title string `json:"title"`
+	Text  string `json:"text"`
+}
+type ShopData struct {
+	ImageURL   string `json:"imageUrl"`
+	Name       string `json:"name"`
+	StarRating int    `json:"starRating"`
+	Address    string `json:"address"`
+	Distance   string `json:"distance"`
+}
+
 type MerchantCardListRequest struct {
 	OpenId    string `json:"open_id"`
 	PageSize  int    `json:"page_size"`

+ 10 - 0
apis/shanghu/models/upload.go

@@ -0,0 +1,10 @@
+package models
+
+type UploadPictureRequest struct {
+	UserCode string   `json:"user_code"`
+	Picture  []string `json:"picture"`
+}
+
+type UploadPictureReply struct {
+	Picture []string `json:"picture"`
+}

+ 78 - 0
apis/shanghu/upload.go

@@ -0,0 +1,78 @@
+package shanghu
+
+import (
+	"context"
+	"duoduo/apis/shanghu/models"
+	"duoduo/conf"
+	"duoduo/tools/app"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"github.com/qiniu/go-sdk/v7/auth/qbox"
+	"github.com/qiniu/go-sdk/v7/storage"
+	"time"
+)
+
+func UploadPicture(c *gin.Context) {
+
+	var inData models.UploadPictureRequest
+	var outData models.UploadPictureReply
+
+	confIni, err := conf.ConnIni()
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	err = c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	for i := 0; i < len(inData.Picture); i++ {
+		//fmt.Println(inData.Pictures[i])
+		//上传图片
+		timeUnixNano := time.Now().UnixNano() //单位纳秒
+		fileName := fmt.Sprintf("%d.jpg", timeUnixNano)
+
+		//七牛云上传图片
+		bucket := "imgxx"
+		key := "shanghuka/" + inData.UserCode + "/" + time.Now().Format(time.DateOnly) + "/" + fileName
+
+		putPolicy := storage.PutPolicy{
+			Scope: bucket,
+		}
+
+		mac := qbox.NewMac(confIni.MustValue("ly-qn", "access_key"), confIni.MustValue("ly-qn", "secret_key"))
+		upToken := putPolicy.UploadToken(mac)
+
+		cfg := storage.Config{}
+		// 空间对应的机房
+		cfg.Zone = &storage.ZoneHuanan
+		// 是否使用https域名
+		cfg.UseHTTPS = false
+		// 上传是否使用CDN上传加速
+		cfg.UseCdnDomains = false
+
+		// 构建表单上传的对象
+		baseUploader := storage.NewBase64Uploader(&cfg)
+		ret := storage.PutRet{}
+		//data, err := base64.RawStdEncoding.DecodeString(inData.Pictures[i])
+		//if err != nil {
+		//	app.Error(c, 500, err, "上传图片失败")
+		//	return
+		//}
+
+		err = baseUploader.Put(context.Background(), &ret, upToken, key, []byte(inData.Picture[i]), nil)
+		if err != nil {
+			app.Error(c, 500, err, "上传图片失败")
+			return
+		}
+
+		outData.Picture = append(outData.Picture, "http://lingyang.shisanmiao.com/"+key)
+
+	}
+
+	app.OK(c, outData, app.Success)
+
+}

+ 11 - 9
models/shanghu/client.account.go

@@ -1,19 +1,21 @@
 package shanghu
 
 import (
+	"github.com/shopspring/decimal"
 	"time"
 )
 
 type MerchantClientAccount struct {
-	ID           int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"` // 主键
-	ClientOpenID int       `gorm:"column:client_open_id;type:int(11)" json:"client_open_id"`
-	ReviewAmount string    `gorm:"column:review_amount;type:decimal(10,2)" json:"review_amount"` // 审核金额
-	Amount       string    `gorm:"column:amount;type:decimal(10,2)" json:"amount"`               // 可提现金额
-	CreateBy     int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`            // 创建者
-	UpdateBy     int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`            // 更新者
-	CreatedAt    time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"`         // 创建时间
-	UpdatedAt    time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`         // 最后更新时间
-	DeletedAt    time.Time `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`         // 删除时间
+	ID           int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
+	ClientOpenID string          `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`  // openid
+	ReviewAmount decimal.Decimal `gorm:"column:review_amount;type:decimal(10,2)" json:"review_amount"`   // 审核金额
+	Amount       decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"`                 // 可提现金额
+	Version      int             `gorm:"column:version;type:int(11)" json:"version"`                     // 创建者
+	CreateBy     int64           `gorm:"column:create_by;type:bigint(20)" json:"create_by"`              // 创建者
+	UpdateBy     int64           `gorm:"column:update_by;type:bigint(20)" json:"update_by"`              // 更新者
+	CreatedAt    time.Time       `gorm:"column:created_at;type:datetime(3)" json:"created_at"`           // 创建时间
+	UpdatedAt    time.Time       `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`           // 最后更新时间
+	DeletedAt    time.Time       `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`           // 删除时间
 }
 
 func (m *MerchantClientAccount) TableName() string {

+ 13 - 12
models/shanghu/client.account.log.go

@@ -1,22 +1,23 @@
 package shanghu
 
 import (
+	"github.com/shopspring/decimal"
 	"time"
 )
 
 type MerchantClientAccountLog struct {
-	ID                int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`           // 主键
-	ReviewAmountAfter string    `gorm:"column:review_amount_after;type:decimal(10,2)" json:"review_amount_after"` // 审核资金交易后
-	ReviewAmountPre   string    `gorm:"column:review_amount_pre;type:decimal(10,2)" json:"review_amount_pre"`     // 审核资金交易前
-	AmountPre         string    `gorm:"column:amount_pre;type:decimal(10,2)" json:"amount_pre"`                   // 交易前
-	AmountAfter       string    `gorm:"column:amount_after;type:decimal(10,2)" json:"amount_after"`               // 交易后
-	TransType         int       `gorm:"column:trans_type;type:int(11)" json:"trans_type"`                         // 交易类型  1-买卡入账 2-提现
-	MerchantCardID    int64     `gorm:"column:merchant_card_id;type:bigint(20)" json:"merchant_card_id"`          // 商户卡id
-	CreateBy          int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                        // 创建者
-	UpdateBy          int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                        // 更新者
-	CreatedAt         time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"`                     // 创建时间
-	UpdatedAt         time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`                     // 最后更新时间
-	DeletedAt         time.Time `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`                     // 删除时间
+	ID                int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`           // 主键
+	ReviewAmountAfter decimal.Decimal `gorm:"column:review_amount_after;type:decimal(10,2)" json:"review_amount_after"` // 审核资金交易后
+	ReviewAmountPre   decimal.Decimal `gorm:"column:review_amount_pre;type:decimal(10,2)" json:"review_amount_pre"`     // 审核资金交易前
+	AmountPre         decimal.Decimal `gorm:"column:amount_pre;type:decimal(10,2)" json:"amount_pre"`                   // 交易前
+	AmountAfter       decimal.Decimal `gorm:"column:amount_after;type:decimal(10,2)" json:"amount_after"`               // 交易后
+	TransType         int             `gorm:"column:trans_type;type:int(11)" json:"trans_type"`                         // 交易类型  1-买卡入账 2-提现
+	ClientOpenID      string          `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`            // 商户openid
+	CreateBy          int64           `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                        // 创建者
+	UpdateBy          int64           `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                        // 更新者
+	CreatedAt         time.Time       `gorm:"column:created_at;type:datetime(3)" json:"created_at"`                     // 创建时间
+	UpdatedAt         time.Time       `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`                     // 最后更新时间
+	DeletedAt         time.Time       `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`                     // 删除时间
 }
 
 func (m *MerchantClientAccountLog) TableName() string {

+ 23 - 0
models/shanghu/merchant.account.go

@@ -0,0 +1,23 @@
+package shanghu
+
+import (
+	"github.com/shopspring/decimal"
+	"time"
+)
+
+type MerchantAccount struct {
+	ID             int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`    // 主键
+	MerchantOpenID string          `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"` //openid
+	ReviewAmount   decimal.Decimal `gorm:"column:review_amount;type:decimal(10,2)" json:"review_amount"`      // 审核金额
+	Amount         decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"`                    // 可提现金额
+	Version        int             `gorm:"column:version;type:int(11)" json:"version"`                        // 创建者
+	CreateBy       int64           `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                 // 创建者
+	UpdateBy       int64           `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                 // 更新者
+	CreatedAt      time.Time       `gorm:"column:created_at;type:datetime(3)" json:"created_at"`              // 创建时间
+	UpdatedAt      time.Time       `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`              // 最后更新时间
+	DeletedAt      time.Time       `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`              // 删除时间
+}
+
+func (m *MerchantAccount) TableName() string {
+	return "merchant_account"
+}

+ 25 - 0
models/shanghu/merchant.account.log.go

@@ -0,0 +1,25 @@
+package shanghu
+
+import (
+	"github.com/shopspring/decimal"
+	"time"
+)
+
+type MerchantAccountLog struct {
+	ID                int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`           // 主键
+	ReviewAmountAfter decimal.Decimal `gorm:"column:review_amount_after;type:decimal(10,2)" json:"review_amount_after"` // 审核资金 交易后
+	ReviewAmountPre   decimal.Decimal `gorm:"column:review_amount_pre;type:decimal(10,2)" json:"review_amount_pre"`     // 审核资金 交易前
+	AmountPre         decimal.Decimal `gorm:"column:amount_pre;type:decimal(10,2)" json:"amount_pre"`                   // 交易前
+	AmountAfter       decimal.Decimal `gorm:"column:amount_after;type:decimal(10,2)" json:"amount_after"`               // 交易后
+	TransType         int             `gorm:"column:trans_type;type:int(11)" json:"trans_type"`                         // 交易类型  1-买卡入账 2-资金审核入账 99-提现
+	MerchantOpenID    string          `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"`        // 商户openid
+	CreateBy          int64           `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                        // 创建者
+	UpdateBy          int64           `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                        // 更新者
+	CreatedAt         time.Time       `gorm:"column:created_at;type:datetime(3)" json:"created_at"`                     // 创建时间
+	UpdatedAt         time.Time       `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`                     // 最后更新时间
+	DeletedAt         time.Time       `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`                     // 删除时间
+}
+
+func (m *MerchantAccountLog) TableName() string {
+	return "merchant_account_log"
+}

+ 122 - 0
models/shanghu/pay.go

@@ -18,6 +18,7 @@ type ClientPayTrans struct {
 	PayTime        time.Time       `gorm:"column:pay_time;type:datetime(3);default:null" json:"pay_time"`     // 支付时间
 	InvitationCode string          `gorm:"column:invitation_code;type:varchar(25)" json:"invitation_code"`    // 邀请码
 	AccountStatus  int             `gorm:"column:account_status;type:int(11)" json:"account_status"`          // 分账状态 99-分账成功 2-分账失败 1-未分账
+	AccountErrLog  string          `gorm:"column:account_err_log;type:varchar(255)" json:"account_err_log"`   // 分账err日志
 	CreateBy       int64           `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                 // 创建者
 	UpdateBy       int64           `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                 // 更新者
 	CreatedAt      time.Time       `gorm:"column:created_at;type:datetime(3)" json:"created_at"`              // 创建时间
@@ -89,3 +90,124 @@ func (m *ClientPayTrans) GetTransByAccount() (ClientPayTrans, error) {
 
 	return doc, nil
 }
+
+func (m *ClientPayTrans) GetPayTransByStatus() (ClientPayTrans, error) {
+	var doc ClientPayTrans
+
+	table := orm.ShMysql.Table(m.TableName())
+	table = table.Where("status = ? and  account_status = ?", m.Status, m.AccountStatus)
+
+	if err := table.Select("*").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+
+func (m *ClientPayTrans) UpdateById(data map[string]interface{}) error {
+	err := orm.ShMysql.Table(m.TableName()).Where("id = ?", m.ID).Updates(data).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (m *ClientPayTrans) SettleAdd(merchantAmount, clientAmount decimal.Decimal, merchantOpenId, clientOpenId string) error {
+	// 使用事务 添加
+	var err error
+	var clientAccount MerchantClientAccount
+	var merchantAccount MerchantAccount
+	tx := orm.ShMysql.Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+		} else {
+			tx.Commit()
+		}
+	}()
+
+	//查看是否有账号,没有创建
+	err = tx.Table("merchant_account").Select("*").Where("merchant_open_id = ?", merchantOpenId).Find(&merchantAccount).Error
+	if err != nil && err.Error() != "record not found" {
+		return err
+	}
+
+	if merchantAccount.ID == 0 {
+		merchantAccount.MerchantOpenID = merchantOpenId
+		merchantAccount.Version = 1
+		merchantAccount.UpdatedAt = time.Now()
+		merchantAccount.CreatedAt = time.Now()
+		err = tx.Table("merchant_account").Create(&merchantAccount).Error
+		if err != nil {
+			return err
+		}
+	}
+
+	err = tx.Table("merchant_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
+	if err != nil && err.Error() != "record not found" {
+		return err
+	}
+
+	if clientAccount.ID == 0 {
+		clientAccount.ClientOpenID = clientOpenId
+		clientAccount.Version = 1
+		clientAccount.UpdatedAt = time.Now()
+		clientAccount.CreatedAt = time.Now()
+		err = tx.Table("merchant_client_account").Create(&clientAccount).Error
+		if err != nil {
+			return err
+		}
+	}
+
+	//做金额加减操作并且入日志库
+	merchantAmountAdd := merchantAccount.Amount.Add(merchantAmount)
+	err = tx.Table("merchant_account").Model(&merchantAccount).Where("merchant_open_id = ? and version = ?", merchantOpenId, merchantAccount.Version).Updates(
+		map[string]interface{}{
+			"amount":     merchantAmountAdd,
+			"version":    merchantAccount.Version + 1,
+			"updated_at": time.Now()}).Error
+	if err != nil {
+		return err
+	}
+
+	var merchantAccountLog MerchantAccountLog
+
+	merchantAccountLog.MerchantOpenID = merchantOpenId
+	merchantAccountLog.UpdatedAt = time.Now()
+	merchantAccountLog.AmountPre = merchantAccount.Amount
+	merchantAccountLog.AmountAfter = merchantAmountAdd
+	merchantAccountLog.ReviewAmountAfter = merchantAccount.ReviewAmount
+	merchantAccountLog.ReviewAmountPre = merchantAccount.ReviewAmount
+
+	err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
+	if err != nil {
+		return err
+	}
+
+	clientAmountAdd := clientAccount.Amount.Add(clientAmount)
+	err = tx.Table("merchant_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
+		map[string]interface{}{
+			"amount":     clientAmountAdd,
+			"version":    clientAccount.Version + 1,
+			"updated_at": time.Now()}).Error
+	if err != nil {
+		return err
+	}
+
+	var clientAccountLog MerchantClientAccountLog
+
+	clientAccountLog.ClientOpenID = clientOpenId
+	clientAccountLog.UpdatedAt = time.Now()
+	clientAccountLog.AmountPre = clientAccount.Amount
+	clientAccountLog.AmountAfter = clientAmountAdd
+	clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
+	clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
+
+	err = tx.Table("merchant_client_account").Create(&clientAccountLog).Error
+	if err != nil {
+		return err
+	}
+
+	return nil
+
+}

+ 1 - 0
models/shanghu/upload.go

@@ -0,0 +1 @@
+package shanghu

+ 47 - 1
report/shang.hu.ka.go

@@ -1,5 +1,11 @@
 package report
 
+import (
+	"duoduo/models/shanghu"
+	"github.com/shopspring/decimal"
+	"time"
+)
+
 // C端分账
 // 分账逻辑:
 // 1.获取待分账、分账失败的数据
@@ -8,5 +14,45 @@ package report
 
 func ClientAccount() {
 	//
-	//var account m
+	for true {
+		//查询订单信息
+		var account shanghu.ClientPayTrans
+		account.AccountStatus = 1 //未分账
+		account.Status = 2        //支付成功
+		clientTrans, err := account.GetPayTransByStatus()
+		if err != nil {
+			break
+		}
+		data := make(map[string]interface{})
+		data["account_status"] = 3 //分账中
+		data["updated_at"] = time.Now()
+		err = clientTrans.UpdateById(data)
+		if err != nil {
+			break
+		}
+
+		//查询卡信息
+		var cardInfo shanghu.MerchantCard
+		cardInfo.ID = clientTrans.MerchantCardID
+		merchantCard, err := cardInfo.GetMerchantCard()
+		if err != nil {
+			dataErr := make(map[string]interface{})
+			dataErr["account_status"] = 2            //分账失败
+			dataErr["account_err_log"] = err.Error() //分账失败日志
+			dataErr["updated_at"] = time.Now()
+			err = clientTrans.UpdateById(dataErr)
+			if err != nil {
+				break
+			}
+			continue
+		}
+		//分账
+		//按比例商户一部分,分销一部分
+		//计算金额
+		clientAmount := account.Amount.Mul(decimal.NewFromInt(merchantCard.RebateRate)).Div(decimal.NewFromInt(100)).Round(2)
+		//merchantAmount := account.Amount.Sub(clientAmount).Round(2)
+		account.Amount.Sub(clientAmount).Round(2)
+
+	}
+
 }