123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package order
- import (
- "app.yhyue.com/moapp/jybase/common"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "jyOrderManager/internal/jyutil"
- "jyOrderManager/internal/model"
- "log"
- "strings"
- )
- func List(ctx context.Context, param model.OrderListParams) (map[string]interface{}, error) {
- var (
- orderSql []string
- adminId = jyutil.GetUserMsgFromCtx(ctx).EntUserId
- )
- if param.Page <= 0 {
- param.Page = 1
- }
- if param.Size < 0 || param.Size > 100 {
- param.Size = 50
- }
- if param.IsHelpList {
- // 我的订单、高翔、沈炳义查看所有得帮助用户下单的订单
- orderSql = append(orderSql, " a.del_status = 0 ")
- countRes, _ := g.DB().GetCount(ctx, fmt.Sprintf(`SELECT * from cadmin.request_auth WHERE user_id = %d and request_call = 'Baiy.Cadmin.Order.helpUserOrders'`, adminId))
- if countRes > 0 {
- orderSql = append(orderSql, " a.order_channel = 'xdqd04' ")
- } else {
- orderSql = append(orderSql, fmt.Sprintf(" f.ent_userId= '%d' ", adminId))
- }
- } else {
- orderSql = append(orderSql, fmt.Sprintf(`(a.salesperson_id ='%d' or f.ent_userId ='%d')`, adminId, adminId))
- }
- if param.SearchContent != "" {
- orderSql = append(orderSql, fmt.Sprintf(` (a.order_code = '%s' or a.company_name LIKE '%s' or a.user_phone LIKE '%s')`,
- param.SearchContent, `%`+param.SearchContent+`%`, `%`+param.SearchContent+`%`))
- }
- if len(param.CourseStatus) > 0 {
- var statusArr []string
- for _, status := range param.CourseStatus {
- switch status {
- case 4: //已退回
- statusArr = append(statusArr, "-2,-3,-4")
- case 5: //审核通过
- statusArr = append(statusArr, "4")
- default:
- statusArr = append(statusArr, gconv.String(status))
- }
- }
- orderSql = append(orderSql, fmt.Sprintf("a.audit_status in (%s)", strings.Join(statusArr, ",")))
- }
- if len(param.OrderStatus) > 0 {
- orderSql = append(orderSql, fmt.Sprintf("a.order_status in (%s)", strings.Join(param.OrderStatus, ",")))
- }
- if len(param.ReturnStatus) > 0 {
- orderSql = append(orderSql, fmt.Sprintf("a.return_status in (%s)", strings.Join(param.ReturnStatus, ",")))
- }
- if len(param.RefundStatus) > 0 {
- orderSql = append(orderSql, fmt.Sprintf("a.refund_status in (%s)", strings.Join(param.RefundStatus, ",")))
- }
- switch len(param.InvoiceType) { //存在单选或2个选项是进行判断 全选或不选不进行where
- case 1: //单选发票方式
- for _, s := range param.InvoiceType {
- switch s {
- case 1: //未开票
- orderSql = append(orderSql, `b.order_code is null`)
- case 2: //部分开票
- orderSql = append(orderSql, `(b.order_code is not null and IFNULL(b.invoiced_amount, 0) < a.pay_money - a.commission)`)
- case 3:
- orderSql = append(orderSql, `(b.order_code is not null and IFNULL(b.invoiced_amount, 0) = a.pay_money - a.commission)`)
- }
- }
- case 2: //多选发票方式
- var isOne bool
- for _, s := range param.InvoiceType {
- if s == 1 {
- isOne = true
- }
- }
- if isOne { //存在未开发票选项
- for _, s := range param.InvoiceType {
- switch s {
- case 2: //部分开票
- orderSql = append(orderSql, `IFNULL(b.invoiced_amount, 0) < a.pay_money - a.commission - IFNULL(g.orderMoney, 0)`)
- case 3:
- orderSql = append(orderSql, `(b.order_code is null or IFNULL(b.invoiced_amount, 0) = a.pay_money - a.commission - IFNULL(g.orderMoney, 0))`)
- }
- }
- } else { //不存在未开发票选项
- orderSql = append(orderSql, `(b.order_code is not null and IFNULL(b.invoiced_amount, 0) <= a.pay_money - a.commission - IFNULL(g.orderMoney, 0))`)
- }
- }
- if len(param.InvoiceType) == 1 {
- var statusArr []string
- for _, s := range param.ContractType {
- switch s {
- case 1: //未归档
- statusArr = append(statusArr, `e.contract_archive_status is null or e.contract_archive_status = 0`)
- case 2: //已归档
- statusArr = append(statusArr, `e.contract_archive_status = 1`)
- }
- }
- orderSql = append(orderSql, fmt.Sprintf("(%s)", strings.Join(statusArr, " or ")))
- }
- if len(param.ProductType) > 0 {
- var statusArr []string
- orderSql = append(orderSql, fmt.Sprintf(" a.order_code in (SELECT DISTINCT(order_code) FROM jy_order_detail WHERE product_type in ('%s') )", strings.Join(statusArr, " or ")))
- }
- if param.OrderTimeStart != "" {
- orderSql = append(orderSql, fmt.Sprintf("a.create_time>='%s'", param.OrderTimeStart))
- }
- if param.OrderTimeEnd != "" {
- orderSql = append(orderSql, fmt.Sprintf("a.create_time<='%s'", param.OrderTimeEnd))
- }
- sqlWhere := `SELECT
- %s
- FROM
- dataexport_order a
- LEFT JOIN (
- SELECT
- order_code,
- SUM(invoice_money) AS invoiced_amount
- FROM
- invoice
- WHERE
- invoice_status = 1 and invoice_changed = 0
- GROUP BY
- order_code
- ) b ON a.order_code = b.order_code
- LEFT JOIN (
- SELECT
- order_code,
- SUM(return_money) AS return_money
- FROM
- return_money_record
- WHERE
- state = 1
- GROUP BY
- order_code
- ) c ON a.order_code = c.order_code
- LEFT JOIN (
- SELECT
- order_code,
- SUM(refund_money) AS refund_money
- FROM
- refund_record
- GROUP BY
- order_code
- ) d ON a.order_code = d.order_code
- LEFT JOIN (
- SELECT ordercode, ent_userId FROM order_sale_record WHERE ent_userId = '%d' and state in (1,2) GROUP BY ordercode,ent_userId
- ) f ON a.order_code = f.ordercode
- LEFT JOIN contract e ON a.order_code = e.order_code
- LEFT JOIN (
- SELECT
- orderCode,
- SUM(orderMoney) AS orderMoney
- FROM
- moneyCorrection
- GROUP BY
- orderCode
- ) g ON a.order_code = g.orderCode
- WHERE
- %s`
- sql := fmt.Sprintf(sqlWhere, " a.id,\n a.order_code,\n a.create_time,\n a.company_name,\n a.user_phone,\n a.user_nickname,\n a.buy_subject,\n a.audit_status,\n a.order_status,\n a.pay_money - IFNULL(g.orderMoney, 0) as pay_money,\n a.commission,\n a.return_status,\n IFNULL(c.return_money, 0) as return_money,\n IFNULL(b.invoiced_amount, 0) as invoiced_amount,\n a.refund_status,\n IFNULL(d.refund_money, 0) as refund_money,\n f.ent_userId,\n e.contract_status,\n e.contract_time,\n e.contract_archive_status,\n e.contract_archive_time,\n (SELECT id FROM jy_order_detail where order_code = a.order_code order by final_price desc, id desc LIMIT 1) as order_detail_id ", adminId, strings.Join(orderSql, " and "))
- sqlCount := fmt.Sprintf(sqlWhere, " * ", adminId, strings.Join(orderSql, " and "))
- log.Println("list count sql :", sqlCount)
- log.Println("list sql :", sql)
- count, err := g.DB().GetCount(ctx, sqlCount)
- if err != nil {
- log.Println("count err:=", err.Error())
- return nil, err
- }
- list, err := g.DB().Query(ctx, fmt.Sprintf(`%s order by a.create_time desc LIMIT %d,%d`, sql, (param.Page-1)*param.Size, param.Size))
- if err != nil {
- log.Println("list err:=", err.Error())
- return nil, err
- }
- for _, m := range list.List() {
- jyOrderDetail, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE id = %d`, gconv.Int64(m["order_detail_id"])))
- if jyOrderDetail.IsEmpty() {
- continue
- }
- m["service_type"] = jyOrderDetail.Map()["service_type"]
- m["filter"] = jyOrderDetail.Map()["filter"]
- //未回款金额
- m["no_return_money"] = gconv.Int(m["pay_money"]) - gconv.Int(m["return_money"]) - gconv.Int(m["commission"])
- //未退款金额
- m["no_refund_money"] = gconv.Int(m["return_money"]) - gconv.Int(m["refund_money"])
- //开票
- if m["invoiced_amount"] != nil && m["invoiced_amount"] != 0 {
- noInvoicedAmount := gconv.Int(m["pay_money"]) - gconv.Int(m["invoiced_amount"]) - gconv.Int(m["commission"])
- m["no_invoiced_amount"] = noInvoicedAmount //未开票金额
- m["invoiced_status"] = common.If(noInvoicedAmount == 0, 1, 2)
- } else {
- m["invoiced_status"] = 0 //未开票
- }
- if m["productType"] == "大会员" {
- filter := gconv.Map(jyOrderDetail.Map()["filter"])
- isSINGLE := common.IntAll(filter["areaCount"]) == 1
- if filter["comboId"] == 6 {
- if isSINGLE {
- m["productType"] = "大会员商机版2.0(单省版)"
- } else {
- m["productType"] = "大会员商机版2.0"
- }
- } else if filter["comboId"] == 7 {
- m["productType"] = "大会员专家版2.0"
- }
- }
- }
- return map[string]interface{}{
- "count": count,
- "list": list.List(),
- }, nil
- }
|