list.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package order
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/frame/g"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. "jyOrderManager/internal/jyutil"
  9. "jyOrderManager/internal/model"
  10. "log"
  11. "strings"
  12. )
  13. func List(ctx context.Context, param model.OrderListParams) (map[string]interface{}, error) {
  14. var (
  15. orderSql []string
  16. adminId = jyutil.GetUserMsgFromCtx(ctx).EntUserId
  17. )
  18. if param.Page <= 0 {
  19. param.Page = 1
  20. }
  21. if param.Size < 0 || param.Size > 100 {
  22. param.Size = 50
  23. }
  24. if param.IsHelpList {
  25. // 我的订单、高翔、沈炳义查看所有得帮助用户下单的订单
  26. orderSql = append(orderSql, " a.del_status = 0 ")
  27. countRes, _ := g.DB().GetCount(ctx, fmt.Sprintf(`SELECT * from cadmin.request_auth WHERE user_id = %d and request_call = 'Baiy.Cadmin.Order.helpUserOrders'`, adminId))
  28. if countRes > 0 {
  29. orderSql = append(orderSql, " a.order_channel = 'xdqd04' ")
  30. } else {
  31. orderSql = append(orderSql, fmt.Sprintf(" f.ent_userId= '%d' ", adminId))
  32. }
  33. } else {
  34. orderSql = append(orderSql, fmt.Sprintf(`(a.salesperson_id ='%d' or f.ent_userId ='%d')`, adminId, adminId))
  35. }
  36. if param.SearchContent != "" {
  37. orderSql = append(orderSql, fmt.Sprintf(` (a.order_code = '%s' or a.company_name LIKE '%s' or a.user_phone LIKE '%s')`,
  38. param.SearchContent, `%`+param.SearchContent+`%`, `%`+param.SearchContent+`%`))
  39. }
  40. if len(param.CourseStatus) > 0 {
  41. var statusArr []string
  42. for _, status := range param.CourseStatus {
  43. switch status {
  44. case 4: //已退回
  45. statusArr = append(statusArr, "-2,-3,-4")
  46. case 5: //审核通过
  47. statusArr = append(statusArr, "4")
  48. default:
  49. statusArr = append(statusArr, gconv.String(status))
  50. }
  51. }
  52. orderSql = append(orderSql, fmt.Sprintf("a.audit_status in (%s)", strings.Join(statusArr, ",")))
  53. }
  54. if len(param.OrderStatus) > 0 {
  55. orderSql = append(orderSql, fmt.Sprintf("a.order_status in (%s)", strings.Join(param.OrderStatus, ",")))
  56. }
  57. if len(param.ReturnStatus) > 0 {
  58. orderSql = append(orderSql, fmt.Sprintf("a.return_status in (%s)", strings.Join(param.ReturnStatus, ",")))
  59. }
  60. if len(param.RefundStatus) > 0 {
  61. orderSql = append(orderSql, fmt.Sprintf("a.refund_status in (%s)", strings.Join(param.RefundStatus, ",")))
  62. }
  63. switch len(param.InvoiceType) { //存在单选或2个选项是进行判断 全选或不选不进行where
  64. case 1: //单选发票方式
  65. for _, s := range param.InvoiceType {
  66. switch s {
  67. case 1: //未开票
  68. orderSql = append(orderSql, `b.order_code is null`)
  69. case 2: //部分开票
  70. orderSql = append(orderSql, `(b.order_code is not null and IFNULL(b.invoiced_amount, 0) < a.pay_money - a.commission)`)
  71. case 3:
  72. orderSql = append(orderSql, `(b.order_code is not null and IFNULL(b.invoiced_amount, 0) = a.pay_money - a.commission)`)
  73. }
  74. }
  75. case 2: //多选发票方式
  76. var isOne bool
  77. for _, s := range param.InvoiceType {
  78. if s == 1 {
  79. isOne = true
  80. }
  81. }
  82. if isOne { //存在未开发票选项
  83. for _, s := range param.InvoiceType {
  84. switch s {
  85. case 2: //部分开票
  86. orderSql = append(orderSql, `IFNULL(b.invoiced_amount, 0) < a.pay_money - a.commission - IFNULL(g.orderMoney, 0)`)
  87. case 3:
  88. orderSql = append(orderSql, `(b.order_code is null or IFNULL(b.invoiced_amount, 0) = a.pay_money - a.commission - IFNULL(g.orderMoney, 0))`)
  89. }
  90. }
  91. } else { //不存在未开发票选项
  92. orderSql = append(orderSql, `(b.order_code is not null and IFNULL(b.invoiced_amount, 0) <= a.pay_money - a.commission - IFNULL(g.orderMoney, 0))`)
  93. }
  94. }
  95. if len(param.InvoiceType) == 1 {
  96. var statusArr []string
  97. for _, s := range param.ContractType {
  98. switch s {
  99. case 1: //未归档
  100. statusArr = append(statusArr, `e.contract_archive_status is null or e.contract_archive_status = 0`)
  101. case 2: //已归档
  102. statusArr = append(statusArr, `e.contract_archive_status = 1`)
  103. }
  104. }
  105. orderSql = append(orderSql, fmt.Sprintf("(%s)", strings.Join(statusArr, " or ")))
  106. }
  107. if len(param.ProductType) > 0 {
  108. var statusArr []string
  109. 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 ")))
  110. }
  111. if param.OrderTimeStart != "" {
  112. orderSql = append(orderSql, fmt.Sprintf("a.create_time>='%s'", param.OrderTimeStart))
  113. }
  114. if param.OrderTimeEnd != "" {
  115. orderSql = append(orderSql, fmt.Sprintf("a.create_time<='%s'", param.OrderTimeEnd))
  116. }
  117. sqlWhere := `SELECT
  118. %s
  119. FROM
  120. dataexport_order a
  121. LEFT JOIN (
  122. SELECT
  123. order_code,
  124. SUM(invoice_money) AS invoiced_amount
  125. FROM
  126. invoice
  127. WHERE
  128. invoice_status = 1 and invoice_changed = 0
  129. GROUP BY
  130. order_code
  131. ) b ON a.order_code = b.order_code
  132. LEFT JOIN (
  133. SELECT
  134. order_code,
  135. SUM(return_money) AS return_money
  136. FROM
  137. return_money_record
  138. WHERE
  139. state = 1
  140. GROUP BY
  141. order_code
  142. ) c ON a.order_code = c.order_code
  143. LEFT JOIN (
  144. SELECT
  145. order_code,
  146. SUM(refund_money) AS refund_money
  147. FROM
  148. refund_record
  149. GROUP BY
  150. order_code
  151. ) d ON a.order_code = d.order_code
  152. LEFT JOIN (
  153. SELECT ordercode, ent_userId FROM order_sale_record WHERE ent_userId = '%d' and state in (1,2) GROUP BY ordercode,ent_userId
  154. ) f ON a.order_code = f.ordercode
  155. LEFT JOIN contract e ON a.order_code = e.order_code
  156. LEFT JOIN (
  157. SELECT
  158. orderCode,
  159. SUM(orderMoney) AS orderMoney
  160. FROM
  161. moneyCorrection
  162. GROUP BY
  163. orderCode
  164. ) g ON a.order_code = g.orderCode
  165. WHERE
  166. %s`
  167. 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 "))
  168. sqlCount := fmt.Sprintf(sqlWhere, " * ", adminId, strings.Join(orderSql, " and "))
  169. log.Println("list count sql :", sqlCount)
  170. log.Println("list sql :", sql)
  171. count, err := g.DB().GetCount(ctx, sqlCount)
  172. if err != nil {
  173. log.Println("count err:=", err.Error())
  174. return nil, err
  175. }
  176. 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))
  177. if err != nil {
  178. log.Println("list err:=", err.Error())
  179. return nil, err
  180. }
  181. for _, m := range list.List() {
  182. jyOrderDetail, _ := g.DB().GetOne(ctx, fmt.Sprintf(`SELECT * FROM jy_order_detail WHERE id = %d`, gconv.Int64(m["order_detail_id"])))
  183. if jyOrderDetail.IsEmpty() {
  184. continue
  185. }
  186. m["service_type"] = jyOrderDetail.Map()["service_type"]
  187. m["filter"] = jyOrderDetail.Map()["filter"]
  188. //未回款金额
  189. m["no_return_money"] = gconv.Int(m["pay_money"]) - gconv.Int(m["return_money"]) - gconv.Int(m["commission"])
  190. //未退款金额
  191. m["no_refund_money"] = gconv.Int(m["return_money"]) - gconv.Int(m["refund_money"])
  192. //开票
  193. if m["invoiced_amount"] != nil && m["invoiced_amount"] != 0 {
  194. noInvoicedAmount := gconv.Int(m["pay_money"]) - gconv.Int(m["invoiced_amount"]) - gconv.Int(m["commission"])
  195. m["no_invoiced_amount"] = noInvoicedAmount //未开票金额
  196. m["invoiced_status"] = common.If(noInvoicedAmount == 0, 1, 2)
  197. } else {
  198. m["invoiced_status"] = 0 //未开票
  199. }
  200. if m["productType"] == "大会员" {
  201. filter := gconv.Map(jyOrderDetail.Map()["filter"])
  202. isSINGLE := common.IntAll(filter["areaCount"]) == 1
  203. if filter["comboId"] == 6 {
  204. if isSINGLE {
  205. m["productType"] = "大会员商机版2.0(单省版)"
  206. } else {
  207. m["productType"] = "大会员商机版2.0"
  208. }
  209. } else if filter["comboId"] == 7 {
  210. m["productType"] = "大会员专家版2.0"
  211. }
  212. }
  213. }
  214. return map[string]interface{}{
  215. "count": count,
  216. "list": list.List(),
  217. }, nil
  218. }