| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 | package mysqlimport (	orm "duoduo/database"	"errors"	"github.com/shopspring/decimal")type Order struct {	CreateTime            string          `gorm:"column:create_time" json:"createTime"`	GoodsName             string          `gorm:"column:goods_name" json:"goodsName"`	GoodsThumbnailURL     string          `gorm:"column:goods_thumbnail_url" json:"goodsThumbnailUrl"`	JSONData              string          `gorm:"column:json_data" json:"jsonData"`	OrderGroupSuccessTime string          `gorm:"column:order_group_success_time" json:"orderGroupSuccessTime"`	OrderID               string          `gorm:"column:order_id" json:"orderId"`	OrderStatus           int             `gorm:"column:order_status" json:"orderStatus"`	OrderStatusDesc       string          `gorm:"column:order_status_desc" json:"orderStatusDesc"`	PID                   string          `gorm:"column:p_id" json:"pId"`	PromotionAmount       int64           `gorm:"column:promotion_amount" json:"promotionAmount"`	UpdateTime            string          `gorm:"column:update_time" json:"updateTime"`	OrderCreateTime       string          `gorm:"column:order_create_time" json:"orderCreateTime"`	OrderSn               string          `gorm:"column:order_sn" json:"orderSn"`	Amount                decimal.Decimal `gorm:"column:amount" json:"amount"`	Amount1               decimal.Decimal `gorm:"column:amount_1" json:"amount1"`	Amount2               decimal.Decimal `gorm:"column:amount_2" json:"amount2"`	OrderAmount           decimal.Decimal `gorm:"column:order_amount" json:"orderAmount"`	OpenId                string          `gorm:"column:open_id" json:"openId"`	OpenId1               string          `gorm:"column:open_id_1" json:"openId1"`	OpenId2               string          `gorm:"column:open_id_2" json:"openId2"`}type OrderAmount struct {	TotalAmount        decimal.Decimal `gorm:"column:total_amount" json:"totalAmount"`	TotalAmountOne     decimal.Decimal `gorm:"column:total_amount_one" json:"totalAmountOne"`	TotalAmountTwo     decimal.Decimal `gorm:"column:total_amount_two" json:"totalAmountTwo"`	TodayAmount        decimal.Decimal `gorm:"column:today_amount" json:"todayAmount"`	TodayAmountOne     decimal.Decimal `gorm:"column:today_amount_one" json:"todayAmountOne"`	TodayAmountTwo     decimal.Decimal `gorm:"column:today_amount_two" json:"todayAmountTwo"`	YesterdayAmount    decimal.Decimal `gorm:"column:yesterday_amount" json:"yesterdayAmount"`	YesterdayAmountOne decimal.Decimal `gorm:"column:yesterday_amount_one" json:"yesterdayAmountOne"`	YesterdayAmountTwo decimal.Decimal `gorm:"column:yesterday_amount_two" json:"yesterdayAmountTwo"`	LatelyAmount       decimal.Decimal `gorm:"column:lately_amount" json:"latelyAmount"`	LatelyAmountOne    decimal.Decimal `gorm:"column:lately_amount_one" json:"latelyAmountOne"`	LatelyAmountTwo    decimal.Decimal `gorm:"column:lately_amount_two" json:"latelyAmountTwo"`	AvailableAmount    decimal.Decimal `gorm:"column:available_amount" json:"availableAmount"`	AvailableAmountOne decimal.Decimal `gorm:"column:available_amount_one" json:"availableAmountOne"`	AvailableAmountTwo decimal.Decimal `gorm:"column:available_amount_two" json:"availableAmountTwo"`}// TableName sets the insert table name for this struct typefunc (o *Order) TableName() string {	return "order"}func (o *Order) Create() (Order, error) {	var doc Order	result := orm.Eloquent.Table(o.TableName()).Create(&o)	if result.Error != nil {		err := result.Error		return doc, err	}	doc = *o	return doc, nil}func (o *Order) GetNum() int {	var count int	tableCount := orm.Eloquent.Table(o.TableName()).Where("order_id = ? ", o.OrderID)	tableCount.Count(&count)	return count}func (o *Order) GetAmount() (Wallet, error) {	var wallet Wallet	//历史总收益: 0,1,2,3,5 求和	//今日预估收益:0,1,2,3 求和	//昨日预估收益:0,1,2,3 求和	//近30日预估收益:0,1,2,3,5 求和	//可体现金额:5求和-已体现金额	//已体现金额:提现记录求和	//历史总收益	err := orm.Eloquent.Select("SUM(promotion_amount) as total_amount").Where("order_status in (0,1,2,3,5) and p_id = ? ", o.PID).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//今日收益	err = orm.Eloquent.Select("SUM(promotion_amount) as today_amount").Where("order_status in (0,1,2,3) and p_id = ? and DATE(order_group_success_time) = curdate() ", o.PID).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//昨日收益	err = orm.Eloquent.Select("SUM(promotion_amount) as yesterday_amount").Where("order_status in (0,1,2,3) and p_id = ? and DATE(order_group_success_time) = date_sub(curdate(),interval 1 day) ", o.PID).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//近30日收益	err = orm.Eloquent.Select("SUM(promotion_amount) as lately_amount").Where("order_status in (0,1,2,3) and p_id = ? and DATE(order_group_success_time) >= date_sub(curdate(),interval 30 day) ", o.PID).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//上个月结算收益	err = orm.Eloquent.Select("SUM(promotion_amount) as available_amount").Where("order_status = 5 and p_id = ? and DATE(order_group_success_time) >= date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now()) day),interval 0 month) ", o.PID).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	return wallet, nil}func (o *Order) GetAmountOpenId() (OrderAmount, error) {	var wallet OrderAmount	//历史总收益: 0,1,2,3,5 求和	//今日预估收益:0,1,2,3 求和	//昨日预估收益:0,1,2,3 求和	//近30日预估收益:0,1,2,3,5 求和	//可体现金额:5求和-已体现金额	//已体现金额:提现记录求和	//历史总收益	err := orm.Eloquent.Select("SUM(amount) as total_amount").Where("order_status in (0,1,2,3,5) and open_id = ? ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_1) as total_amount_one").Where("order_status in (0,1,2,3,5) and open_id_1 = ? ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_2) as total_amount_two").Where("order_status in (0,1,2,3,5) and open_id_2 = ? ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//今日收益	err = orm.Eloquent.Select("SUM(amount) as today_amount").Where("order_status in (0,1,2,3) and open_id = ? and DATE(order_group_success_time) = curdate() ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_1) as today_amount_one").Where("order_status in (0,1,2,3) and open_id_1 = ? and DATE(order_group_success_time) = curdate() ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_2) as today_amount_two").Where("order_status in (0,1,2,3) and open_id_2 = ? and DATE(order_group_success_time) = curdate() ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//昨日收益	err = orm.Eloquent.Select("SUM(amount) as yesterday_amount").Where("order_status in (0,1,2,3) and open_id = ? and DATE(order_group_success_time) = date_sub(curdate(),interval 1 day) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_1) as yesterday_amount_one").Where("order_status in (0,1,2,3) and open_id_1 = ? and DATE(order_group_success_time) = date_sub(curdate(),interval 1 day) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_2) as yesterday_amount_two").Where("order_status in (0,1,2,3) and open_id_2 = ? and DATE(order_group_success_time) = date_sub(curdate(),interval 1 day) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//近30日收益	err = orm.Eloquent.Select("SUM(amount) as lately_amount").Where("order_status in (0,1,2,3) and open_id = ? and DATE(order_group_success_time) >= date_sub(curdate(),interval 30 day) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_1) as lately_amount_one").Where("order_status in (0,1,2,3) and open_id_1 = ? and DATE(order_group_success_time) >= date_sub(curdate(),interval 30 day) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_2) as lately_amount_two").Where("order_status in (0,1,2,3) and open_id_2 = ? and DATE(order_group_success_time) >= date_sub(curdate(),interval 30 day) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	//上个月结算收益	err = orm.Eloquent.Select("SUM(amount) as available_amount").Where("order_status = 5 and open_id = ? and DATE(order_group_success_time) >= date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now()) day),interval 0 month) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_1) as available_amount_one").Where("order_status = 5 and open_id_1 = ? and DATE(order_group_success_time) >= date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now()) day),interval 0 month) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	err = orm.Eloquent.Select("SUM(amount_2) as available_amount_two").Where("order_status = 5 and open_id_2 = ? and DATE(order_group_success_time) >= date_sub(date_sub(date_format(now(),'%y-%m-%d'),interval extract(day from now()) day),interval 0 month) ", o.OpenId).Table(o.TableName()).First(&wallet).Error	if err != nil {		return wallet, err	}	return wallet, nil}// 更新func (o *Order) Update(orderId string) (update Order, err error) {	if err = orm.Eloquent.Table(o.TableName()).Where("order_id = ?", orderId).First(&update).Error; err != nil {		return	}	//参数1:是要修改的数据	//参数2:是修改的数据	if err = orm.Eloquent.Table(o.TableName()).Where("order_id = ?", orderId).Model(&update).Updates(&o).Error; err != nil {		return	}	return}//list 接口使用func (o *Order) GetOrderList(pageSize int, pageIndex int) ([]Order, int, error) {	var doc []Order	table := orm.Eloquent.Table(o.TableName())	table = table.Where("p_id = ?  ", o.PID)	var count int	if err := table.Select("order_id,promotion_amount,p_id,order_status,order_status_desc,goods_name,goods_thumbnail_url,order_group_success_time,amount,amount_1,amount_2,order_sn,DATE_FORMAT(order_create_time,'%Y-%m-%d %H:%i:%s') as order_create_time,order_amount").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {		return nil, 0, err	}	table.Count(&count)	return doc, count, nil}//list 接口使用func (o *Order) GetOrderOpenIdList(pageSize int, pageIndex int, status int) ([]Order, int, error) {	var doc []Order	table := orm.Eloquent.Table(o.TableName())	if status == 0 {		table = table.Where("open_id = ?  ", o.OpenId)	} else if status == 1 {		table = table.Where("open_id_1 = ?  ", o.OpenId)	} else if status == 2 {		table = table.Where("open_id_2 = ?  ", o.OpenId)	} else {		return nil, 0, errors.New("status err")	}	//table = table.Where("open_id = ?  ", o.OpenId)	var count int	if err := table.Select("order_id,promotion_amount,p_id,order_status,order_status_desc,goods_name," +		"goods_thumbnail_url,order_group_success_time,amount,amount_1,amount_2,order_sn," +		"DATE_FORMAT(order_create_time,'%Y-%m-%d %H:%i:%s') as order_create_time,order_amount").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {		return nil, 0, err	}	table.Count(&count)	return doc, count, nil}
 |