Files
jx-callback/business/model/dao/dao_order.go
苏尹岚 e2b67fd1cb aa
2021-01-04 09:18:27 +08:00

315 lines
10 KiB
Go

package dao
import (
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/model"
)
func GetDeliveryOrdersNoPage(db *DaoDB, userIDs []string, statuss []int, fromTime, toTime time.Time, isWeights []int) (dOrders []*model.DeliveryOrder, err error) {
sql := `
SELECT a.*
FROM delivery_order a
WHERE 1 = 1
`
sqlParams := []interface{}{}
if len(userIDs) > 0 {
sql += ` AND a.user_id IN (` + GenQuestionMarks(len(userIDs)) + `)`
sqlParams = append(sqlParams, userIDs)
}
if len(statuss) > 0 {
sql += ` AND a.status IN (` + GenQuestionMarks(len(statuss)) + `)`
sqlParams = append(sqlParams, statuss)
}
if len(isWeights) > 0 {
sql += ` AND a.is_weight IN (` + GenQuestionMarks(len(isWeights)) + `)`
sqlParams = append(sqlParams, isWeights)
}
if fromTime != utils.ZeroTimeValue {
sql += ` AND a.created_at >= ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND a.created_at <= ?`
sqlParams = append(sqlParams, toTime)
}
err = GetRows(db, &dOrders, sql, sqlParams)
return dOrders, err
}
type GetDeliveryOrdersResult struct {
model.DeliveryOrder
DeliverySendInfo *UserDeliveryAddressEx `json:"deliverySendInfo"`
DeliveryReceiveInfo *UserDeliveryAddressEx `json:"deliveryReceiveInfo"`
}
func GetDeliveryOrders(db *DaoDB, userIDs []string, statuss []int, fromTime, toTime time.Time, pageSize, offset int) (pagedInfo *model.PagedInfo, err error) {
var dOrders []*GetDeliveryOrdersResult
sql := `
SELECT SQL_CALC_FOUND_ROWS a.*
FROM delivery_order a
WHERE 1 = 1
`
sqlParams := []interface{}{}
if len(userIDs) > 0 {
sql += ` AND a.user_id IN (` + GenQuestionMarks(len(userIDs)) + `)`
sqlParams = append(sqlParams, userIDs)
}
if len(statuss) > 0 {
sql += ` AND a.status IN (` + GenQuestionMarks(len(statuss)) + `)`
sqlParams = append(sqlParams, statuss)
}
if fromTime != utils.ZeroTimeValue {
sql += ` AND a.created_at >= ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND a.created_at <= ?`
sqlParams = append(sqlParams, toTime)
}
sql += " ORDER BY a.created_at DESC"
sql += " LIMIT ? OFFSET ?"
pageSize = jxutils.FormalizePageSize(pageSize)
sqlParams = append(sqlParams, pageSize, offset)
Begin(db)
defer Commit(db)
if err = GetRows(db, &dOrders, sql, sqlParams...); err == nil {
pagedInfo = &model.PagedInfo{
TotalCount: GetLastTotalRowCount(db),
// Data: dOrders,
}
for _, v := range dOrders {
if addressList1, _, err := QueryUserDeliveryAddress(db, int64(v.DeliverySendID), nil, 0, 0, 0); err == nil && len(addressList1) > 0 {
v.DeliverySendInfo = addressList1[0]
} else {
sendInfo := &UserDeliveryAddressEx{}
sendInfo.ConsigneeName = v.SendName
sendInfo.ConsigneeMobile = v.SendMobile
sendInfo.Address = v.SendAddress
sendInfo.AutoAddress = v.SendAutoAddress
sendInfo.CityCode = v.SendCityCode
sendInfo.DistrictCode = v.SendDistrictCode
sendInfo.DetailAddress = v.SendDetailAddress
sendInfo.Lng = v.SendLng
sendInfo.Lat = v.SendLat
v.DeliverySendInfo = sendInfo
}
if addressList2, _, err := QueryUserDeliveryAddress(db, int64(v.DeliveryReceiveID), nil, 0, 0, 0); err == nil && len(addressList2) > 0 {
v.DeliveryReceiveInfo = addressList2[0]
} else {
receiveInfo := &UserDeliveryAddressEx{}
receiveInfo.ConsigneeName = v.ReceiveName
receiveInfo.ConsigneeMobile = v.ReceiveMobile
receiveInfo.Address = v.ReceiveAddress
receiveInfo.AutoAddress = v.ReceiveAutoAddress
receiveInfo.CityCode = v.ReceiveCityCode
receiveInfo.DistrictCode = v.ReceiveDistrictCode
receiveInfo.DetailAddress = v.ReceiveDetailAddress
receiveInfo.Lng = v.ReceiveLng
receiveInfo.Lat = v.ReceiveLat
v.DeliveryReceiveInfo = receiveInfo
}
}
pagedInfo.Data = dOrders
}
return pagedInfo, err
}
type GetOrdersResult struct {
model.Order
UserName string `json:"userName"`
Mobile string `json:"mobile"`
}
func GetOrders(db *DaoDB, orderID, userID string, orderType int, cityCodes []int, fromTime, toTime time.Time, keyword string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
var orders []*GetOrdersResult
sql := `
SELECT SQL_CALC_FOUND_ROWS a.*, b.name user_name, b.mobile
FROM ` + "`order`" + ` a
JOIN user b ON b.user_id = a.user_id AND b.deleted_at = ?
WHERE 1 = 1
`
sqlParams := []interface{}{
utils.DefaultTimeValue,
}
if orderID != "" {
sql += ` AND a.order_id = ?`
sqlParams = append(sqlParams, orderID)
}
if userID != "" {
sql += ` AND a.user_id = ?`
sqlParams = append(sqlParams, userID)
}
if orderType != 0 {
sql += ` AND a.type = ?`
sqlParams = append(sqlParams, orderType)
}
if len(cityCodes) > 0 {
sql += ` AND a.city_code IN ` + GenQuestionMarks(len(cityCodes)) + `)`
sqlParams = append(sqlParams, cityCodes)
}
if fromTime != utils.ZeroTimeValue {
sql += ` AND a.created_at >= ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND a.created_at <= ?`
sqlParams = append(sqlParams, toTime)
}
if keyword != "" {
sql += ` AND (b.mobile LIKE ? OR b.name LIKE ?)`
sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
}
sql += " LIMIT ? OFFSET ?"
pageSize = jxutils.FormalizePageSize(pageSize)
sqlParams = append(sqlParams, pageSize, offset)
Begin(db)
defer Commit(db)
if err = GetRows(db, &orders, sql, sqlParams); err == nil {
pagedInfo = &model.PagedInfo{
TotalCount: GetLastTotalRowCount(db),
Data: orders,
}
}
return pagedInfo, err
}
type GetPayStatisticsResult struct {
TotalPay int `json:"totalPay"` //支付总额
CanCash int `json:"canCash"` //可提现(未提现+申请提现)
AccountBalance int `json:"accountBalance"` //未提现(含保证金)
SubmitCash int `json:"submitCash"` //申请提现
Cashed int `json:"cashed"` //已经体现
CashIncome int `json:"cashIncome"` //提现收益
TotalIncome int `json:"totalIncome"` //收益总额
MemberIncome int `json:"memberIncome"` //会员收益
}
func GetPayStatistics(db *DaoDB, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time, orderTypes []int) (getPayStatisticsResult *GetPayStatisticsResult, err error) {
sqlParams := []interface{}{}
getWhereSql := func() (sql string) {
if len(orderTypes) > 0 {
sql += ` AND order_type IN ` + GenQuestionMarks(len(orderTypes)) + `)`
sqlParams = append(sqlParams, orderTypes)
}
if fromTime != utils.ZeroTimeValue {
sql += ` AND created_at > ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND created_at < ?`
sqlParams = append(sqlParams, toTime)
}
return sql
}
sql := `
SELECT t1.total_pay, t2.submit_cash + t3.account_balance can_cash, t3.account_balance, t4.cashed, t4.cashed / 10 cash_income
FROM user a
LEFT JOIN (SELECT SUM(pay_price) total_pay, user_id FROM order WHERE type = ? AND status = ? ` + getWhereSql() + ` GROUP BY 2) t1 ON t1.user_id = a.user_id
LEFT JOIN (SELECT SUM(pay_price) submit_cash, user_id FROM order WHERE type = ? AND status = ?` + getWhereSql() + ` GROUP BY 2) t2 ON t2.user_id = a.user_id
LEFT JOIN user_bill t3 ON t3.user_id = a.user_id
LEFT JOIN (SELECT SUM(pay_price) cashed, user_id FROM order WHERE type = ? AND status = ?` + getWhereSql() + ` GROUP BY 2) t4 ON t4.user_id = a.user_id
`
sqlParams = append(sqlParams, model.OrderTypePay, model.OrderStatusFinished)
sqlParams = append(sqlParams, model.OrderTypeCash, model.OrderStatusWait4Pay)
sqlParams = append(sqlParams, model.OrderTypeCash, model.OrderStatusFinished)
if mobile != "" {
if pop == 1 {
sql += " JOIN user e ON e.moblie = ? AND a.pop_user = e.user_id"
sqlParams = append(sqlParams, mobile)
}
}
sql += `
WHERE 1 = 1
`
if userID != "" {
if pop == 1 {
sql += " AND a.pop_user = ?"
sqlParams = append(sqlParams, userID)
} else {
sql += " AND a.user_id = ?"
sqlParams = append(sqlParams, userID)
}
}
if mobile != "" {
if pop == 0 {
sql += " AND a.mobile = ?"
sqlParams = append(sqlParams, mobile)
}
}
if len(cityCodes) > 0 {
sql += ` AND a.city_code IN ` + GenQuestionMarks(len(cityCodes)) + `)`
sqlParams = append(sqlParams, cityCodes)
}
err = GetRow(db, &getPayStatisticsResult, sql, sqlParams)
sqlParams2 := []interface{}{}
getWhereSql2 := func() (sql string) {
sql += ` AND order_type = ?`
sqlParams = append(sqlParams, model.OrderTpyeMember)
if fromTime != utils.ZeroTimeValue {
sql += ` AND created_at > ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND created_at < ?`
sqlParams = append(sqlParams, toTime)
}
return sql
}
sql2 := `
SELECT t2.member_income1 + t3.member_income2 member_income
FROM (SELECT DISTINCT a.user_id FROM user a`
if mobile != "" {
if pop == 1 {
sql2 += " JOIN user e ON e.moblie = ? AND a.pop_user = e.user_id"
sqlParams = append(sqlParams, mobile)
} else {
sql2 += " AND a.mobile = ?"
sqlParams = append(sqlParams, mobile)
}
}
if len(cityCodes) > 0 {
sql2 += ` AND a.city_code IN ` + GenQuestionMarks(len(cityCodes)) + `)`
sqlParams = append(sqlParams, cityCodes)
}
if userID != "" {
if pop == 1 {
sql2 += " AND a.pop_user = ?"
sqlParams = append(sqlParams, userID)
} else {
sql2 += " AND a.user_id = ?"
sqlParams = append(sqlParams, userID)
}
}
sql2 += `) t1
LEFT JOIN (SELECT SUM(pay_price) member_income1, user_id FROM order WHERE type = ? AND status = ? ` + getWhereSql2() + ` AND user_id = t1.user_id GROUP BY 2) t2
LEFT JOIN (SELECT SUM(b.expend_price) member_income2
FROM user_bill b
LEFT JOIN bill_expend c ON c.bill_id = b.bill_id AND c.type = ?
`
sqlParams = append(sqlParams, model.OrderTypePay, model.OrderStatusFinished, model.BillTypeMember)
if fromTime != utils.ZeroTimeValue {
sql2 += ` AND c.created_at > ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql2 += ` AND c.created_at < ?`
sqlParams = append(sqlParams, toTime)
}
sql2 += `
WHERE b.user_id = t1.user_id) t3
`
err = GetRow(db, &getPayStatisticsResult, sql2, sqlParams2)
return getPayStatisticsResult, err
}
func GetIncomeStatistics(db *DaoDB, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time) (getIncomeStatisticsResult *GetIncomeStatisticsResult, err error) {
return getIncomeStatisticsResult, err
}