From 2def11ba83760b5be1361f10c3756cc0e9cfd165 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 14 八月 2023 09:32:13 +0800
Subject: [PATCH] merge

---
 model/salesRefund.go |   89 ++++++++++++++++++++++++++++++++++----------
 1 files changed, 69 insertions(+), 20 deletions(-)

diff --git a/model/salesRefund.go b/model/salesRefund.go
index cbdf41b..0d942a0 100644
--- a/model/salesRefund.go
+++ b/model/salesRefund.go
@@ -1,33 +1,45 @@
 package model
 
 import (
+	"aps_crm/constvar"
 	"aps_crm/pkg/mysqlx"
+	"fmt"
+	"github.com/shopspring/decimal"
 	"gorm.io/gorm"
+	"gorm.io/gorm/clause"
 )
 
 type (
 	SalesRefund struct {
-		Id           int         `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
-		ClientId     int         `json:"clientId" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
-		Number       string      `json:"number" gorm:"column:number;type:varchar(255);comment:閫�娆惧崟鍙�"`
-		MemberId     int         `json:"memberId" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
-		RefundDate   *CustomTime `json:"refundDate" gorm:"column:refund_date;type:datetime;comment:閫�娆炬棩鏈�"`
-		RefundMethod string      `json:"refundMethod" gorm:"column:refund_method;type:varchar(255);comment:閫�娆炬柟寮�"`
-		AccountId    int         `json:"accountId" gorm:"column:account_id;type:int;comment:璐︽埛"`
-		IsInvoice    int         `json:"isInvoice" gorm:"column:is_invoice;type:int;comment:鏄惁寮�绁�"`
-		Reason       string      `json:"reason" gorm:"column:reason;type:varchar(255);comment:閫�娆惧師鍥�"`
-		Products     []Product   `json:"products" gorm:"many2many:salesRefund_product;"`
-		gorm.Model   `json:"-"`
+		Id            int                       `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
+		ClientId      int                       `json:"clientId" gorm:"column:client_id;type:int;comment:瀹㈡埛id"`
+		Client        Client                    `json:"client" gorm:"foreignKey:ClientId"`
+		Number        string                    `json:"number" gorm:"column:number;type:varchar(255);comment:閫�娆惧崟鍙�"`
+		SourceType    constvar.RefundSourceType `gorm:"column:source_type;type:int;not null;default 0;comment:鏉ユ簮绫诲瀷锛�1閿�鍞槑缁嗗崟2鏈嶅姟鍚堝悓3閿�鍞彂绁�4鏀舵璁″垝5鍑哄簱鍗曪級" json:"sourceType"` // 鏉ユ簮绫诲瀷锛�1閿�鍞槑缁嗗崟2鏈嶅姟鍚堝悓3閿�鍞彂绁�4鏀舵璁″垝5鍑哄簱鍗曪級
+		SourceId      int                       `gorm:"column:source_id;type:int;not null;default 0;comment:婧愬崟id " json:"sourceId"`                               // 婧愬崟id
+		MemberId      int                       `json:"memberId" gorm:"column:member_id;type:int;comment:璐熻矗浜篿d"`
+		Member        User                      `json:"member" gorm:"foreignKey:MemberId"`
+		RefundDate    string                    `json:"refundDate" gorm:"column:refund_date;type:datetime;comment:閫�娆炬棩鏈�"`
+		PaymentTypeId int                       `gorm:"column:payment_type_id;type:int;not null;default 0;comment:鏀舵鏂瑰紡ID" json:"paymentTypeId"` // 鏀舵鏂瑰紡ID
+		PaymentType   PaymentType               `gorm:"foreignKey:PaymentTypeId" json:"paymentType"`
+		BankAccountId int                       `gorm:"column:bank_account_id;type:int;not null;default 0;comment:璐︽埛id" json:"bankAccountId"` // 璐︽埛id
+		BankAccount   BankAccount               `gorm:"foreignKey:BankAccountId" json:"bankAccount"`
+		IsInvoice     int                       `json:"isInvoice" gorm:"column:is_invoice;type:int;comment:鏄惁寮�绁�"`
+		Reason        string                    `json:"reason" gorm:"column:reason;type:varchar(255);comment:閫�娆惧師鍥�"`
+		Products      []*Product                `json:"products" gorm:"many2many:salesRefund_product;"`
+		AmountTotal   decimal.Decimal           `gorm:"column:amount_total;type:decimal(12,2);comment:浠风◣鍚堣" json:"amountTotal"` // 浠风◣鍚堣
+		CrmModel
 	}
 
 	SalesRefundSearch struct {
 		SalesRefund
-
-		Orm      *gorm.DB
-		Keyword  string
-		OrderBy  string
-		PageNum  int
-		PageSize int
+		Orm         *gorm.DB
+		KeywordType constvar.SalesRefundKeywordType
+		Keyword     string
+		OrderBy     string
+		PageNum     int
+		PageSize    int
+		Preload     bool
 	}
 )
 
@@ -43,11 +55,38 @@
 
 func (slf *SalesRefundSearch) build() *gorm.DB {
 	var db = slf.Orm.Model(&SalesRefund{})
-	if slf.Keyword != "" {
-		db = db.Where("name LIKE ?", "%"+slf.Keyword+"%")
-	}
 	if slf.Id != 0 {
 		db = db.Where("id = ?", slf.Id)
+	}
+
+	if slf.Preload {
+		db = db.Preload("Client").
+			Preload("PaymentType").
+			Preload("BankAccount").
+			Preload("Products")
+	}
+	if slf.KeywordType != "" {
+		switch slf.KeywordType {
+		case constvar.SalesRefundKeywordRefundNumber:
+			db = db.Where("number like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordClientName:
+			db = db.Joins("Client", clause.LeftJoin).Where("Client.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordRefundDate:
+			db = db.Where("refund_date like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordAccount:
+			db = db.Joins("BankAccount").Where("BankAccount name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordIsInvoice:
+			db = db.Where("is_invoice like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordPaymentType:
+			db = db.Joins("PaymentType").Where("PaymentType.name like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordPrincipal:
+			db = db.Joins("left join user on user.id = sales_refund.member_id").Where("user.username like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordUpdateAt:
+			db = db.Where("updated_at like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+		case constvar.SalesRefundKeywordPriceTotal:
+			db = db.Where("amount_total like ?", fmt.Sprintf("%%%s%%", slf.Keyword))
+
+		}
 	}
 
 	return db
@@ -99,6 +138,16 @@
 	return slf
 }
 
+func (slf *SalesRefundSearch) SetPreload(preload bool) *SalesRefundSearch {
+	slf.Preload = preload
+	return slf
+}
+
+func (slf *SalesRefundSearch) SetKeywordType(keywordType constvar.SalesRefundKeywordType) *SalesRefundSearch {
+	slf.KeywordType = keywordType
+	return slf
+}
+
 func (slf *SalesRefundSearch) SetKeyword(keyword string) *SalesRefundSearch {
 	slf.Keyword = keyword
 	return slf

--
Gitblit v1.8.0