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