From 530fed8ec225453572d57b15c200ab062c335457 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 01 十一月 2023 19:20:21 +0800 Subject: [PATCH] 公海member_id使用0 --- model/serviceCollectionPlan.go | 86 +++++++++++++++++++++++++++++++++--------- 1 files changed, 67 insertions(+), 19 deletions(-) diff --git a/model/serviceCollectionPlan.go b/model/serviceCollectionPlan.go index be9da1b..0e81b21 100644 --- a/model/serviceCollectionPlan.go +++ b/model/serviceCollectionPlan.go @@ -5,27 +5,33 @@ "aps_crm/pkg/mysqlx" "errors" "fmt" + "github.com/shopspring/decimal" "gorm.io/gorm" ) type ( - // ServiceCollectionPlan 鏈嶅姟鍚堝悓鏀舵璁″垝 + // ServiceCollectionPlan 鏀舵璁″垝 ServiceCollectionPlan struct { - Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` - CollectionType int `gorm:"collection_type" json:"collectionType"` // 绫诲瀷锛�1 璁″垝鏀舵鏃ユ湡 2 椤圭洰鐘舵�侊級 - ServiceContractId int `gorm:"service_contract_id" json:"serviceContractId"` // 鏈嶅姟鍚堝悓id - PrincipalId int `gorm:"principal_id" json:"principalId"` // 鏀舵璐熻矗浜篒D - Term int `gorm:"term" json:"term"` // 鏈熸 - Percent float64 `gorm:"percent" json:"percent"` // 姣斾緥 - Amount float64 `gorm:"amount" json:"amount"` // 閲戦 - MoneyType string `gorm:"money_type" json:"moneyType"` // 甯佺 - CollectionDate string `gorm:"collection_date" json:"collectionDate"` // 璁″垝鏀舵鏃ユ湡 - Remark string `gorm:"remark" json:"remark"` // 澶囨敞 - Status int `gorm:"status" json:"status"` // 鐘舵�侊紙1鏈敹2宸叉敹锛� - FileId int `gorm:"file_id" json:"fileId"` // 闄勪欢id + Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` + CollectionType int `gorm:"column:collection_type;type:tinyint;not null;default 0;comment:绫诲瀷锛�1 璁″垝鏀舵鏃ユ湡 2 椤圭洰鐘舵�侊級" json:"collectionType"` // 绫诲瀷锛�1 璁″垝鏀舵鏃ユ湡 2 椤圭洰鐘舵�侊級 + SourceType constvar.CollectionSourceType `gorm:"column:source_type;type:tinyint;not null;default 0;comment:婧愬崟绫诲瀷锛�1閿�鍞槑缁�2鏈嶅姟鍚堝悓3閿�鍞彂绁級" json:"sourceType"` // 婧愬崟绫诲瀷锛�1閿�鍞槑缁�2鏈嶅姟鍚堝悓3閿�鍞彂绁級 + SourceId int `gorm:"column:source_id;type:int;not null;default 0;comment:婧愬崟id" json:"sourceId"` // 婧愬崟id + PrincipalId int `gorm:"column:principal_id;type:int;not null;default 0;comment:鏀舵璐熻矗浜篒D" json:"principalId"` // 鏀舵璐熻矗浜篒D + Principal User `gorm:"foreignKey:PrincipalId" json:"principal"` // 鏀舵璐熻矗浜篒D + Term int `gorm:"column:term;type:tinyint;not null;default 0;comment:鏈熸" json:"term"` // 鏈熸 + Percent decimal.Decimal `gorm:"column:percent;type:decimal(5,2);not null;default 0.00;comment:鏀舵姣斾緥" gorm:"" json:"percent"` // 姣斾緥 + Amount decimal.Decimal `gorm:"column:amount;type:decimal(12,2);not null;default '0.00';comment:閲戦" gorm:"" json:"amount"` // 閲戦 + MoneyType string `gorm:"column:money_type;type:varchar(255);not null;default '';comment:甯佺" json:"moneyType"` // 甯佺 + CollectionDate string `gorm:"column:collection_date;type:varchar(255);not null;default '';comment:璁″垝鏀舵鏃ユ湡" json:"collectionDate"` // 璁″垝鏀舵鏃ユ湡 + Remark string `gorm:"column:remark;type:varchar(512);not null;default '';comment:澶囨敞" json:"remark"` // 澶囨敞 + Status constvar.CollectionStatus `gorm:"column:status;type:tinyint;not null;default '';comment:鐘舵�侊紙1鏈敹2閮ㄥ垎宸叉敹3宸叉敹锛�" json:"status"` // 鐘舵�侊紙1鏈敹2閮ㄥ垎宸叉敹3宸叉敹锛� + AmountReceivable decimal.Decimal `gorm:"column:amount_receivable;type:decimal(12,2);comment:搴旀敹閲戦" json:"amountReceivable"` // 搴旀敹閲戦 + AmountReceived decimal.Decimal `gorm:"column:amount_received;type:decimal(12,2);comment:宸叉敹閲戦" json:"amountReceived"` // 宸叉敹閲戦 + AmountTotal decimal.Decimal `gorm:"column:amount_total;type:decimal(12,2);comment:鎬婚" json:"amountTotal"` // 鎬婚 + FileId int `gorm:"column:file_id;type:int;comment:闄勪欢id" json:"fileId"` // 闄勪欢id } - // ServiceCollectionPlanSearch 鏈嶅姟鍚堝悓鏀舵璁″垝鎼滅储鏉′欢 + // ServiceCollectionPlanSearch 鏀舵璁″垝鎼滅储鏉′欢 ServiceCollectionPlanSearch struct { ServiceCollectionPlan Orm *gorm.DB @@ -38,7 +44,7 @@ ) func (ServiceCollectionPlan) TableName() string { - return "service_collection_plan" + return "collection_plan" } func NewServiceCollectionPlanSearch() *ServiceCollectionPlanSearch { @@ -53,10 +59,15 @@ db = db.Where("id = ?", slf.Id) } - if slf.ServiceContractId != 0 { - db = db.Where("service_contract_id = ?", slf.ServiceContractId) + if slf.SourceId != 0 { + db = db.Where("source_id = ?", slf.SourceId) } + if slf.SourceType != 0 { + db = db.Where("source_type = ?", slf.SourceType) + } + + db = db.Preload("Principal") return db } @@ -97,8 +108,13 @@ return slf } -func (slf *ServiceCollectionPlanSearch) SetServiceContractId(id int) *ServiceCollectionPlanSearch { - slf.ServiceContractId = id +func (slf *ServiceCollectionPlanSearch) SetSourceId(id int) *ServiceCollectionPlanSearch { + slf.SourceId = id + return slf +} + +func (slf *ServiceCollectionPlanSearch) SetSourceType(sourceType constvar.CollectionSourceType) *ServiceCollectionPlanSearch { + slf.SourceType = sourceType return slf } @@ -142,6 +158,38 @@ return records, total, err } +func (slf *ServiceCollectionPlanSearch) UpdateByMap(upMap map[string]interface{}) error { + var ( + db = slf.build() + ) + + if err := db.Updates(upMap).Error; err != nil { + return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap) + } + + return nil +} + +func (slf *ServiceCollectionPlanSearch) UpdateStatusAndAmount(collection *ServiceCollectionPlan, receiveAmount decimal.Decimal) error { + receivedAmount := collection.AmountReceived.Add(receiveAmount) + var status constvar.CollectionStatus + if receiveAmount.GreaterThanOrEqual(collection.AmountTotal) { + status = constvar.CollectionStatusCollected + } else { + status = constvar.CollectionStatusSubCollected + } + receivableAmount := collection.AmountTotal.Add(receivedAmount) + err := slf.SetId(collection.Id).UpdateByMap(map[string]interface{}{ + "status": status, + "amount_received": receivedAmount, + "amount_receivable": receivableAmount}) + if err != nil { + return err + } + + return nil +} + // InitDefaultData 鍒濆鍖栨暟鎹� func (slf *ServiceCollectionPlanSearch) InitDefaultData() error { var ( -- Gitblit v1.8.0