Files
jx-callback/business/model/dao/dao_order.go
苏尹岚 a06620e34b aa
2021-01-12 10:36:45 +08:00

520 lines
18 KiB
Go

package dao
import (
"fmt"
"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)
fmt.Println(sql)
fmt.Println(sqlParams)
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 getFromSql(orderType, status int, alies string, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time, orderTypes []int) (sql string, sqlParams []interface{}) {
sql += `
(SELECT SUM(IFNULL(b.pay_price,0)) total_pay
FROM user a
JOIN ` + "`order` b " + `ON b.user_id = a.user_id AND b.type = ? AND b.status = ?
`
sqlParams = append(sqlParams, orderType, status)
if len(orderTypes) > 0 {
sql += ` AND b.order_type IN (` + GenQuestionMarks(len(orderTypes)) + `)`
sqlParams = append(sqlParams, orderTypes)
}
if fromTime != utils.ZeroTimeValue {
sql += ` AND b.created_at > ?`
sqlParams = append(sqlParams, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql += ` AND b.created_at < ?`
sqlParams = append(sqlParams, toTime)
}
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 a.status = ? AND a.deleted_at = ?
`
sqlParams = append(sqlParams, model.UserStatusNormal, utils.DefaultTimeValue)
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)
}
sql += `) ` + alies
return sql, sqlParams
}
func GetPayStatistics(db *DaoDB, userID string, pop int, cityCodes []int, mobile string, fromTime, toTime time.Time, orderTypes []int) (getPayStatisticsResult *GetPayStatisticsResult, err error) {
sqlParams := []interface{}{}
sql := `SELECT t1.total_pay, t2.total_pay submit_cash, t3.total_pay cashed, t4.account_balance, t4.account_balance + t2.total_pay can_cash
FROM `
rSQL1, rSQLParams1 := getFromSql(model.OrderTypePay, model.OrderStatusFinished, "t1", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes)
sql += rSQL1 + ","
sqlParams = append(sqlParams, rSQLParams1...)
rSQL2, rSQLParams2 := getFromSql(model.OrderTypeCash, model.OrderStatusWait4Pay, "t2", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes)
sql += rSQL2 + ","
sqlParams = append(sqlParams, rSQLParams2...)
rSQL3, rSQLParams3 := getFromSql(model.OrderTypeCash, model.OrderStatusFinished, "t3", userID, pop, cityCodes, mobile, fromTime, toTime, orderTypes)
sql += rSQL3 + ","
sqlParams = append(sqlParams, rSQLParams3...)
sql += `(SELECT SUM(IFNULL(b.account_balance,0)) account_balance
FROM user a
JOIN user_bill b ON a.user_id = b.user_id
`
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)
}
sql += `
) t4`
err = GetRow(db, &getPayStatisticsResult, sql, sqlParams)
sqlParams2 := []interface{}{}
getPayStatisticsResult1 := &GetPayStatisticsResult{}
sql2 := `
SELECT SUM(IFNULL(d.expend_price,0)) member_income
FROM user a`
if mobile != "" {
if pop == 1 {
sql2 += " JOIN user e ON e.moblie = ? AND a.pop_user = e.user_id"
sqlParams2 = append(sqlParams2, mobile)
}
}
sql2 += `
LEFT JOIN user_bill c ON c.user_id = a.user_id
LEFT JOIN bill_expend d ON d.bill_id = c.bill_id AND d.type = ?
`
sqlParams2 = append(sqlParams2, model.BillTypeMember)
if fromTime != utils.ZeroTimeValue {
sql2 += ` AND d.created_at > ?`
sqlParams2 = append(sqlParams2, fromTime)
}
if toTime != utils.ZeroTimeValue {
sql2 += ` AND d.created_at < ?`
sqlParams2 = append(sqlParams2, toTime)
}
sql2 += `
WHERE a.status = ? AND a.deleted_at = ?
`
sqlParams2 = append(sqlParams2, model.UserStatusNormal, utils.DefaultTimeValue)
if len(cityCodes) > 0 {
sql2 += ` AND a.city_code IN (` + GenQuestionMarks(len(cityCodes)) + `)`
sqlParams2 = append(sqlParams2, cityCodes)
}
if mobile != "" {
if pop == 0 {
sql2 += " AND a.mobile = ?"
sqlParams2 = append(sqlParams2, mobile)
}
}
if userID != "" {
if pop == 1 {
sql2 += " AND a.pop_user = ?"
sqlParams2 = append(sqlParams2, userID)
} else {
sql2 += " AND a.user_id = ?"
sqlParams2 = append(sqlParams2, userID)
}
}
err = GetRow(db, &getPayStatisticsResult1, sql2, sqlParams2)
getPayStatisticsResult.MemberIncome = getPayStatisticsResult1.MemberIncome
getPayStatisticsResult.CashIncome = getPayStatisticsResult.Cashed / 10
getPayStatisticsResult.TotalIncome = getPayStatisticsResult.MemberIncome + getPayStatisticsResult.CashIncome
return getPayStatisticsResult, err
}
type GetManageStatisticsResult struct {
Date time.Time `json:"date"` //日期
BrowseCount int `json:"browseCount"` //点击次数
AcceptCount int `json:"acceptCount"` //接受任务数
FinishCount int `json:"finishCount"` //完成任务数
CancelCount int `json:"cancelCount"` //取消任务数
TotalCash int `json:"totalCash"` //返现总额
InversionRate float64 `json:"inversionRate"` //转化率
}
func getFromSqlManage(status int, alies string, cityCodes []int, jobTime, endTime time.Time, jobIDs []int) (sql string, sqlParams []interface{}) {
sql += `
(SELECT COUNT(b.id) count
FROM job a
JOIN job_order b ON a.id = b.job_id
WHERE a.deleted_at = ?
`
sqlParams = append(sqlParams, utils.DefaultTimeValue)
if status != 0 {
sql += " AND b.status = ?"
sqlParams = append(sqlParams, status)
}
if jobTime != utils.ZeroTimeValue {
sql += " AND a.created_at > ?"
sqlParams = append(sqlParams, jobTime)
}
if endTime != utils.ZeroTimeValue {
sql += " AND a.created_at < ?"
sqlParams = append(sqlParams, endTime)
}
if len(cityCodes) > 0 {
sql += ` AND a.job_city_code IN (` + GenQuestionMarks(len(cityCodes)) + `)`
sqlParams = append(sqlParams, cityCodes)
}
if len(jobIDs) > 0 {
sql += ` AND a.id IN (` + GenQuestionMarks(len(jobIDs)) + `)`
sqlParams = append(sqlParams, jobIDs)
}
sql += ") " + alies
return sql, sqlParams
}
func getWhereSqlManage(cityCodes []int, jobTime, endTime time.Time, jobIDs []int) (sql string, sqlParams []interface{}) {
if jobTime != utils.ZeroTimeValue {
sql += " AND a.created_at > ?"
sqlParams = append(sqlParams, jobTime)
}
if endTime != utils.ZeroTimeValue {
sql += " AND a.created_at < ?"
sqlParams = append(sqlParams, endTime)
}
if len(cityCodes) > 0 {
sql += ` AND a.job_city_code IN (` + GenQuestionMarks(len(cityCodes)) + `)`
sqlParams = append(sqlParams, cityCodes)
}
if len(jobIDs) > 0 {
sql += ` AND a.id IN (` + GenQuestionMarks(len(jobIDs)) + `)`
sqlParams = append(sqlParams, jobIDs)
}
return sql, sqlParams
}
func GetManageStatistics(db *DaoDB, cityCodes []int, jobTime time.Time, jobIDs []int) (getManageStatisticsResult *GetManageStatisticsResult, err error) {
endTime := jobTime.AddDate(0, 0, 1)
sqlParams := []interface{}{}
sql := `
SELECT t1.count accept_count, t2.count finish_count, t3.count cancel_count, t4.browse_count, t6.total_cash
FROM
`
rSQL1, rSQLparams1 := getFromSqlManage(model.JobOrderStatusAccept, "t1", cityCodes, jobTime, endTime, jobIDs)
sql += rSQL1 + ","
sqlParams = append(sqlParams, rSQLparams1...)
rSQL2, rSQLparams2 := getFromSqlManage(model.JobOrderStatusFinish, "t2", cityCodes, jobTime, endTime, jobIDs)
sql += rSQL2 + ","
sqlParams = append(sqlParams, rSQLparams2...)
rSQL3, rSQLparams3 := getFromSqlManage(model.JobOrderStatusCancel, "t3", cityCodes, jobTime, endTime, jobIDs)
sql += rSQL3 + ","
sqlParams = append(sqlParams, rSQLparams3...)
sql += `
(SELECT SUM(a.browse_count) browse_count
FROM job a
JOIN job_order b ON a.id = b.job_id
WHERE a.deleted_at = ?
`
sqlParams = append(sqlParams, utils.DefaultTimeValue)
rSQL4, rSQLparams4 := getWhereSqlManage(cityCodes, jobTime, endTime, jobIDs)
sql += rSQL4
sqlParams = append(sqlParams, rSQLparams4...)
sql += `
) t4,
`
sql += `
(SELECT t5.count * a.avg_price total_cash FROM job a,
(SELECT COUNT(b.id) count, a.id
FROM job a
JOIN job_order b ON a.id = b.job_id
WHERE a.deleted_at = ? AND b.status = ?
`
sqlParams = append(sqlParams, utils.DefaultTimeValue, model.JobOrderStatusFinish)
rSQL5, rSQLparams5 := getWhereSqlManage(cityCodes, jobTime, endTime, jobIDs)
sql += rSQL5
sqlParams = append(sqlParams, rSQLparams5...)
sql += `
GROUP BY 2) t5
WHERE a.id = t5.id
)t6
`
GetRow(db, &getManageStatisticsResult, sql, sqlParams)
if getManageStatisticsResult == nil {
return &GetManageStatisticsResult{
Date: jobTime,
}, err
} else {
getManageStatisticsResult.Date = jobTime
if getManageStatisticsResult.BrowseCount == 0 {
getManageStatisticsResult.InversionRate = 0
} else {
getManageStatisticsResult.InversionRate = float64(getManageStatisticsResult.FinishCount) / float64(getManageStatisticsResult.BrowseCount)
}
return getManageStatisticsResult, err
}
}
type GetManageStatisticsJobResult struct {
BrowseCount int `json:"browseCount"` //点击次数
AcceptCount int `json:"acceptCount"` //接受任务数
FinishCount int `json:"finishCount"` //完成任务数
CancelCount int `json:"cancelCount"` //取消任务数
TotalCash int `json:"totalCash"` //返现总额
InversionRate float64 `json:"inversionRate"` //转化率
Title string `json:"title"` //任务标题
}
func GetManageStatisticsJob(db *DaoDB, cityCodes []int, fromTime, toTime time.Time, jobIDs []int, offset, pageSize int) (pageInfo *model.PagedInfo, err error) {
var (
sqlParams = []interface{}{}
getManageStatisticsJobResult []*GetManageStatisticsJobResult
)
sql := `
SELECT t1.count acceptCount, t2.count finish_count, t3.count cancel_count, a.browse_count, a.title, a.browse_count / t2.count inversion_rate, t4.total_cash
FROM job a
LEFT JOIN (SELECT job_id, COUNT(*) count FROM job_order WHERE status = ? GROUP BY 1) t1 ON t1.job_id = a.id
LEFT JOIN (SELECT job_id, COUNT(*) count FROM job_order WHERE status = ? GROUP BY 1) t2 ON t2.job_id = a.id
LEFT JOIN (SELECT job_id, COUNT(*) count FROM job_order WHERE status = ? GROUP BY 1) t3 ON t3.job_id = a.id
`
sql += `
LEFT JOIN (SELECT COUNT(b.id) * a.avg_price total_cash, a.id
FROM job a
JOIN job_order b ON a.id = b.job_id AND b.status = ?
GROUP BY 2) t4 ON t4.id = a.id
`
sqlParams = append(sqlParams, model.JobOrderStatusAccept, model.JobOrderStatusFinish, model.JobOrderStatusCancel, model.JobOrderStatusFinish)
sql += " WHERE a.deleted_at = ?"
sqlParams = append(sqlParams, utils.DefaultTimeValue)
rSQL, rSQLparams := getWhereSqlManage(cityCodes, fromTime, toTime, jobIDs)
sql += rSQL
sqlParams = append(sqlParams, rSQLparams...)
sql += " LIMIT ? OFFSET ?"
pageSize = jxutils.FormalizePageSize(pageSize)
sqlParams = append(sqlParams, pageSize, offset)
Begin(db)
defer Commit(db)
err = GetRows(db, &getManageStatisticsJobResult, sql, sqlParams)
if err == nil {
pageInfo = &model.PagedInfo{
Data: getManageStatisticsJobResult,
TotalCount: GetLastTotalRowCount(db),
}
}
return pageInfo, err
}