OrderService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. *
  4. * @file:OrderService.php
  5. * @Date: 2023/5/23
  6. * @Time: 13:40
  7. */
  8. namespace Modules\Channel\Services\Order;
  9. use Illuminate\Support\Facades\DB;
  10. use Modules\Channel\Models\Order;
  11. use Modules\Channel\Models\UserHasMiniprograms;
  12. use Modules\Manage\Models\Miniprogram;
  13. use Modules\User\Models\User;
  14. class OrderService
  15. {
  16. /**
  17. * 订单列表查询
  18. * name: getOrderList
  19. * @param array $param
  20. * date 2023/05/23 13:45
  21. */
  22. public static function getOrderList(array $param, $isAll = false)
  23. {
  24. $list = self::getOrderQuerySql($param)->orderBy("orders.id",'desc');
  25. if ($isAll) {
  26. $list = $list->get();
  27. } else {
  28. $list = $list->paginate(getProp($param, 'limit', 15));
  29. }
  30. if (!$list->isEmpty()) {
  31. $status = array_column(self::getOrderPayType(), null, 'value');
  32. $types = array_column(self::getOrderType(), null, 'value');
  33. foreach ($list as $val) {
  34. $val->username = $val->user_id > 0 ? User::where('id', $val->user_id)->value('username') : "";
  35. $val->company_name = "-";
  36. $val->play_name = '-';
  37. $company = Miniprogram::where('id', $val->miniprogram_id)->first();
  38. if ($company) {
  39. $val->company_name = $val->puser_id > 0 ? $company->company() : "-";
  40. $val->play_name = $company->play_name;
  41. $val->xcx_name = $company->name;
  42. }
  43. $val->promotion_title = $val->promotion_id > 0 ? DB::table('promotions')->where('id', $val->promotion_id)->value('name') : "";
  44. $val->total_count = Order::where('uid', $val->uid)->where('id', "<=", $val->id)->count();
  45. if ($val->user_id > 0) {
  46. $val->caompany_count = Order::where('uid', $val->uid)->where('puser_id', '>', 0)->where('puser_id', $val->puser_id)->where('id', "<=", $val->id)->count();
  47. $val->promotion_count = Order::where('uid', $val->uid)->where('puser_id', '>', 0)->where('puser_id', $val->puser_id)->where('user_id', ">", 0)->where('user_id', $val->user_id)->where('id', "<=", $val->id)->count();
  48. } else {
  49. $val->caompany_count = Order::where('uid', $val->uid)->where('puser_id', '>', 0)->count();
  50. $val->promotion_count = Order::where('uid', $val->uid)->where('user_id', '>', 0)->count();
  51. }
  52. $val->tip_text = $types[$val->order_type]['name'] ?? "-";
  53. $val->status_txt = $status[$val->status]['name'] ?? "-";
  54. $val->pay_name = '微信支付';
  55. }
  56. }
  57. return $list;
  58. }
  59. public static function getOrderType()
  60. {
  61. return [
  62. ["value" => "COIN", "name" => "普通充值"],
  63. ["value" => 'MONTH', "name" => '包月'],
  64. ["value" => 'QUARTER', "name" => '包季'],
  65. ["value" => 'YEAR', "name" => '包年'],
  66. ["value" => "FIRST_COIN", "name" => "首充"]
  67. ];
  68. }
  69. public static function getOrderPayType()
  70. {
  71. return [
  72. ["value" => "PAID", "name" => "已支付"],
  73. ["value" => 'REFUND', "name" => '已退款'],
  74. ["value" => 'UNPAID', "name" => '未支付'],
  75. ];
  76. }
  77. private static function getOrderQuerySql(array $param)
  78. {
  79. $sql = DB::table('orders');
  80. // 优化师查询
  81. if (!empty(getProp($param, 'user_id'))) {
  82. $sql->where('user_id', $param['user_id']);
  83. }
  84. // 公司查询
  85. if (!empty(getProp($param, 'puser_id'))) {
  86. $sql->where('puser_id', $param['puser_id']);
  87. }
  88. // 小程序
  89. if (!empty(getProp($param, 'miniprogram_id'))) {
  90. $sql->where('miniprogram_id', $param['miniprogram_id']);
  91. }
  92. // 推广id
  93. if (!empty(getProp($param, 'promotion_id'))) {
  94. $sql->where('promotion_id', $param['promotion_id']);
  95. }
  96. // 订单状态查询
  97. if (!empty(getProp($param, 'status'))) {
  98. $sql->where('status', $param['status']);
  99. }
  100. // 充值项类型
  101. if (!empty(getProp($param, 'order_type'))) {
  102. $sql->where('order_type', $param['order_type']);
  103. }
  104. // 用户
  105. if (!empty(getProp($param, 'uid'))) {
  106. $sql->where('uid', $param['uid']);
  107. }
  108. // 订单号查询
  109. if (!empty(getProp($param, 'trade_no'))) {
  110. $sql->where('trade_no', "like", "%" . $param['trade_no'] . "%");
  111. // $sql->where('trade_no', $param['trade_no']);
  112. }
  113. if (!empty(getProp($param, 'trade_no'))) {
  114. // $sql->where('trade_no', "like", "%" . $param['trade_no'] . "%");
  115. $sql->where('trade_no', $param['trade_no']);
  116. }
  117. // 支付时间
  118. if (!empty(getProp($param, 'pay_end_at_start'))) {
  119. $sql->where('pay_end_at', ">=",$param['pay_end_at_start']);
  120. }
  121. if (!empty(getProp($param, 'pay_end_at_end'))) {
  122. $end =$param['pay_end_at_end'];
  123. if ($end == getProp($param,'pay_end_at_start','')){
  124. $end = date("Y-m-d",strtotime($end)) . " 23:59:59";
  125. }
  126. $sql->where('pay_end_at', "<=", $end);
  127. }
  128. // 创建时间
  129. if (!empty(getProp($param, 'created_at_start'))) {
  130. $sql->where('created_at', '>=',$param['created_at_start']);
  131. }
  132. if (!empty(getProp($param, 'created_at_end'))) {
  133. $end =$param['created_at_end'];
  134. if ($end == getProp($param,'created_at_start','')){
  135. $end = date("Y-m-d",strtotime($end)) . " 23:59:59";
  136. }
  137. $sql->where('created_at', "<=", $end);
  138. }
  139. return $sql;
  140. }
  141. /**
  142. * 用户订单小程选择列表
  143. * name: userUseProgramsList
  144. * @param mixed $uid
  145. * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
  146. * date 2023/05/23 17:51
  147. */
  148. public static function userUseProgramsList(mixed $uid, $name = "", $limit = 20)
  149. {
  150. $operateUser = User::find($uid);
  151. $operateUserRoles = $operateUser->roles->pluck('identify');
  152. if ($operateUserRoles->contains('company') || $operateUserRoles->contains('optimizer')) {
  153. $list = UserHasMiniprograms::query()->join('miniprogram', 'miniprogram.id', '=', 'user_has_miniprograms.miniprogram_id')
  154. ->where('uid', $uid)
  155. ->groupBy("user_has_miniprograms.miniprogram_id")
  156. ->select('user_has_miniprograms.miniprogram_id', 'miniprogram.name', 'miniprogram.play_name');
  157. if ($name) {
  158. $list->where('name', "like", "%" . $name . "%");
  159. }
  160. return $list->orderBy('miniprogram.id', 'desc')->limit($limit)->get();
  161. } else {
  162. if ($name) {
  163. return DB::table('miniprogram')->where('name', "like", "%" . $name . "%")->select("id as miniprogram_id", "name", "play_name")->limit($limit)->get();
  164. }
  165. return DB::table('miniprogram')->select("id as miniprogram_id", "name", "play_name")->orderBy('id', 'desc')->limit($limit)->get();
  166. }
  167. }
  168. /**
  169. * 推广列表选择项
  170. * name: promotionsOptions
  171. * @param mixed $uid
  172. * @param mixed $title
  173. * @return \Illuminate\Support\Collection
  174. * date 2023/05/23 18:13
  175. */
  176. public static function promotionsOptions(mixed $uid, mixed $title,mixed $miniprogram_id,mixed $limit = 10)
  177. {
  178. $operateUser = User::find($uid);
  179. $operateUserRoles = $operateUser->roles->pluck('identify');
  180. $list = DB::table('promotions')->select('id as promotion_id', 'name');
  181. if ($operateUserRoles->contains('optimizer')) {
  182. if ($miniprogram_id){
  183. $list->where('miniprogram_id',$miniprogram_id);
  184. }
  185. if ($title) {
  186. $list->where('name', 'like', "%" . $title . "%");
  187. }
  188. return $list->where('uid', $uid)->orderBy('id', 'desc')->limit($limit)->get();
  189. } else if ($operateUserRoles->contains('company')) {
  190. $list = DB::table('promotions')->join('users', 'users.id', "promotions.uid")
  191. ->where('users.pid', $uid)->orWhere("promotions.uid", $uid)->select('promotions.id as promotion_id', 'promotions.name');
  192. if ($title) {
  193. $list->where('promotions.name', 'like', "%" . $title . "%");
  194. }
  195. if ($miniprogram_id){
  196. $list->where('promotions.miniprogram_id',$miniprogram_id);
  197. }
  198. return $list->orderBy('promotions.id','desc')->limit($limit)->get();
  199. } else {
  200. if ($miniprogram_id){
  201. $list->where('miniprogram_id',$miniprogram_id);
  202. }
  203. if ($title) {
  204. $list->where('name', 'like', "%" . $title . "%");
  205. }
  206. return $list->orderBy('id', 'desc')->limit($limit)->get();
  207. }
  208. }
  209. /***
  210. * 订单列表优化师选择项
  211. * name: promotionsUsers
  212. * @param mixed $pid
  213. * @param mixed $name
  214. * @param mixed $limit
  215. * date 2023/05/26 09:22
  216. */
  217. public static function promotionsUsers(mixed $pid, mixed $name,mixed $miniprogram_id, mixed $limit = 10)
  218. {
  219. if ($pid > 0) {
  220. $list = DB::table('users')->where('pid', $pid)->select('users.id as user_id', 'users.username as name');
  221. if ($name) {
  222. $list->where('username', 'like', "%" . $name . "%");
  223. }
  224. if ($miniprogram_id > 0){
  225. $list->join('user_has_miniprograms','user_has_miniprograms.uid','users.id')
  226. ->where('user_has_miniprograms.miniprogram_id',$miniprogram_id);
  227. }
  228. return $list->orderBy('users.id', 'desc')->limit($limit)->get();
  229. }
  230. $list = DB::table('users')->join('user_has_roles','users.id',"user_has_roles.user_id")->join('roles','roles.id','user_has_roles.role_id')
  231. ->select('users.id as user_id', 'users.username as name')->where('roles.identify','optimizer');
  232. if ($miniprogram_id > 0){
  233. $list->join('user_has_miniprograms','user_has_miniprograms.uid','users.id')
  234. ->where('user_has_miniprograms.miniprogram_id',$miniprogram_id);
  235. }
  236. if ($name){
  237. $list->where('users.username', 'like', "%" . $name . "%");
  238. }
  239. return $list->orderBy('users.id', 'desc')->limit($limit)->get();
  240. }
  241. }