CouponPacketSequenceController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\service\coupon\CouponService;
  4. use app\admin\service\member\MemberService;
  5. use app\controller\Curd;
  6. use app\model\Coupon;
  7. use app\model\CouponDetail;
  8. use app\model\CouponGoods;
  9. use app\model\CouponPacket;
  10. use app\model\CouponPacketSequence;
  11. use app\model\MemberRole;
  12. use app\model\SysCategory;
  13. use app\model\SysDept;
  14. use support\Request;
  15. use support\Response;
  16. class CouponPacketSequenceController extends Curd
  17. {
  18. public function __construct()
  19. {
  20. $this->model = new CouponPacketSequence();
  21. }
  22. public function select(Request $request): Response
  23. {
  24. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  25. // $where['join_coupon_detail_member_id'] = ['<>', ''];
  26. $order = $request->get('order', 'desc');
  27. $field = $field ?? 'coupon_packet_sequence_addtimes';
  28. $couponPacketName = $request->get('coupon_packet_name', '');
  29. if (!empty($couponPacketName)) {
  30. $packetIds = CouponPacket::where('coupon_packet_name', 'like', '%' . $couponPacketName . '%')->pluck('coupon_packet_id')->toArray();
  31. $where['join_sequence_coupon_packet_id'] = ['in', $packetIds];
  32. }
  33. $addTimes = $request->get('coupon_packet_sequence_addtimes', []);
  34. if (!empty($addTimes)) {
  35. $addTimes[0] = isset($addTimes[0]) ? strtotime($addTimes[0]) : time();
  36. $addTimes[1] = isset($addTimes[1]) ? strtotime($addTimes[1]) : time();
  37. $where['coupon_packet_sequence_addtimes'] = $addTimes;
  38. }
  39. $usedTime = $request->get('coupon_packet_sequence_used_datetime', []);
  40. if (!empty($usedTime)) {
  41. $usedTime[0] = isset($usedTime[0]) ? strtotime($usedTime[0]) : time();
  42. $usedTime[1] = isset($usedTime[1]) ? strtotime($usedTime[1]) : time();
  43. $where['coupon_packet_sequence_used_datetime'] = $usedTime;
  44. }
  45. $query = $this->doSelect($where, $field, $order);
  46. return $this->doFormat($query, $format, $limit);
  47. }
  48. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  49. {
  50. if (!empty($where['coupon_packet_sequence_used_datetime'])) {
  51. $usedTime = $where['coupon_packet_sequence_used_datetime'];
  52. unset($where['coupon_packet_sequence_used_datetime']);
  53. }
  54. $model = $this->model->with([
  55. 'packet' => function ($query) {
  56. $query->select('coupon_packet_id', 'coupon_packet_name');
  57. },
  58. 'member' => function ($query) {
  59. $query->select('member_id', 'member_mobile');
  60. },
  61. 'cert' => function ($query) {
  62. $query->select('join_cert_member_id', 'member_cert_name');
  63. },
  64. 'info' => function ($query) {
  65. $query->select('join_info_member_id', 'member_info_nickname');
  66. }
  67. ]);
  68. foreach ($where as $column => $value) {
  69. if (is_array($value)) {
  70. if ($value[0] === 'like' || $value[0] === 'not like') {
  71. $model = $model->where($column, $value[0], "%$value[1]%");
  72. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  73. $model = $model->where($column, $value[0], $value[1]);
  74. } elseif ($value[0] == 'in' && !empty($value[1])) {
  75. $valArr = $value[1];
  76. if (is_string($value[1])) {
  77. $valArr = explode(",", trim($value[1]));
  78. }
  79. $model = $model->whereIn($column, $valArr);
  80. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  81. $valArr = $value[1];
  82. if (is_string($value[1])) {
  83. $valArr = explode(",", trim($value[1]));
  84. }
  85. $model = $model->whereNotIn($column, $valArr);
  86. } elseif ($value[0] == 'null') {
  87. $model = $model->whereNull($column);
  88. } elseif ($value[0] == 'not null') {
  89. $model = $model->whereNotNull($column);
  90. } elseif ($value[0] !== '' || $value[1] !== '') {
  91. $model = $model->whereBetween($column, $value);
  92. }
  93. } else {
  94. $model = $model->where($column, $value);
  95. }
  96. }
  97. if (!empty($usedTime)) {
  98. $model = $model->whereRaw("UNIX_TIMESTAMP(coupon_packet_sequence_used_datetime) >= ? AND UNIX_TIMESTAMP(coupon_packet_sequence_used_datetime) <= ?", [$usedTime[0], $usedTime[1]]);
  99. }
  100. // $model = $model->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,MAX(coupon_detail_addtimes) as coupon_detail_addtimes,COUNT(*) as total');
  101. // $model = $model->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  102. if ($field) {
  103. $model = $model->orderBy($field, $order);
  104. }
  105. return $model;
  106. }
  107. public function afterQuery($items)
  108. {
  109. foreach ($items as &$item) {
  110. $mobile = $certName = $nickname = '';
  111. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  112. $mobile = $item['member']['member_mobile'];
  113. }
  114. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  115. $certName = $item['cert']['member_cert_name'];
  116. }
  117. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  118. $nickname = $item['info']['member_info_nickname'];
  119. }
  120. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  121. }
  122. return $items;
  123. }
  124. /**
  125. * @Desc 修改状态
  126. * @Author Gorden
  127. * @Date 2024/8/22 8:44
  128. *
  129. * @param Request $request
  130. * @return Response
  131. */
  132. public function updateStatus(Request $request)
  133. {
  134. $detailId = $request->post('coupon_detail_id', '');
  135. $status = $request->post('status', '');
  136. $detail = CouponDetail::where('coupon_detail_id', $detailId)->first();
  137. if (!$detail) {
  138. return json_fail('数据不存在');
  139. }
  140. //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED
  141. if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) {
  142. return json_fail("状态异常");
  143. }
  144. try {
  145. CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]);
  146. return json_success('success');
  147. } catch (\Exception $e) {
  148. dump($e->getMessage());
  149. return json_fail('数据修改失败');
  150. }
  151. }
  152. /**
  153. * @Desc 详情
  154. * @Author Gorden
  155. * @Date 2024/8/22 11:46
  156. *
  157. * @param Request $request
  158. * @return Response
  159. */
  160. public function info(Request $request): Response
  161. {
  162. $packetSequenceId = $request->get('coupon_packet_sequence_id', '');
  163. if (empty($packetSequenceId)) {
  164. return json_fail('参数异常');
  165. }
  166. $sequence = CouponPacketSequence::with([
  167. 'packet' => function ($query) {
  168. $query->select('coupon_packet_id', 'coupon_packet_name', 'join_coupon_packet_category_id', 'join_coupon_packet_dept_id', 'coupon_packet_status', 'coupon_packet_issue_num', 'coupon_packet_issue_datetime');
  169. },
  170. 'member' => function ($query) {
  171. $query->select('member_id', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
  172. },
  173. 'info' => function ($query) {
  174. $query->select('join_info_member_id', 'member_info_headimg');
  175. }
  176. ])->where('coupon_packet_sequence_id', $packetSequenceId)
  177. ->first();
  178. if (!empty($sequence->packet)) {
  179. if ($sequence->packet->join_coupon_packet_dept_id == 0) {
  180. $sequence->packet->dept_name = '所有门店';
  181. } else {
  182. $sequence->packet->dept_nam = '';
  183. $dept = SysDept::where('dept_id', $sequence->packet->join_coupon_packet_dept_id)->select('dept_id', 'dept_name')->first();
  184. if (!empty($dept)) {
  185. $sequence->packet->dept_name = $dept->dept_name;
  186. }
  187. }
  188. if (!empty($sequence->packet->join_coupon_packet_category_id)) {
  189. $category = SysCategory::where('category_id', $sequence->packet->join_coupon_packet_category_id)
  190. ->first();
  191. if (!empty($category->category_name)) {
  192. $sequence->packet->category_name = $category->category_name;
  193. }
  194. }
  195. }
  196. if (!empty($sequence->member) && !empty($sequence->member->join_member_role_id)) {
  197. $sequence->member->role_name = MemberRole::where('member_role_id', $sequence->member->join_member_role_id)->value('member_role_name');
  198. }
  199. if (!empty($sequence->info) && !empty($sequence->info->member_info_headimg)){
  200. $sequence->info->member_info_headimg = MemberService::getAvatarUrl($sequence->info->member_info_headimg);
  201. }
  202. $coupons = [];
  203. if (!empty($sequence->coupon_packet_sequence_json)) {
  204. $sequenceJson = json_decode($sequence->coupon_packet_sequence_json, true);
  205. if (isset($sequenceJson['coupons'])) {
  206. $couponIds = array_keys($sequenceJson['coupons']);
  207. $couponModel = Coupon::whereIn('coupon_id', $couponIds)
  208. ->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_value', 'coupon_minimum_limit', 'coupon_category')
  209. ->get()
  210. ->toArray();
  211. foreach ($couponModel as $coupon) {
  212. foreach ($sequenceJson['coupons'] as $key => $jsonCoupon) {
  213. if ($coupon['coupon_id'] == $key) {
  214. $coupons[] = [
  215. 'id' => $key,
  216. 'name' => $coupon['coupon_name'],
  217. 'nbr' => $jsonCoupon['num'],
  218. 'classify' => $coupon['coupon_classify'],
  219. 'info' => CouponService::couponClassifyInfo($coupon['coupon_classify'], $coupon['coupon_category'], $coupon['coupon_value'], $coupon['coupon_minimum_limit'])
  220. ];
  221. }
  222. }
  223. }
  224. }
  225. }
  226. $sequence->coupons = $coupons;
  227. return json_success('', $sequence);
  228. }
  229. public function myCouponGrouping(Request $request)
  230. {
  231. $memberId = $request->get('member_id', '');
  232. $goods = $request->get('goods', []);
  233. $settlementNow = $request->get('settlement_now', 'N');
  234. if ($memberId == '') {
  235. return json_fail("参数异常");
  236. }
  237. $goodsIds = array_column($goods, 'goods_id');
  238. $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)->get()->toArray();
  239. $couponIds = [];
  240. foreach ($couponGoods as $couponGood) {
  241. foreach ($goods as $good) {
  242. if ($good['sku_id'] = $couponGood['join_coupon_goods_sku_id']) {
  243. $couponIds[] = $couponGood['join_goods_coupon_id'];
  244. }
  245. }
  246. }
  247. $details = CouponDetail::with([
  248. 'coupon' => function ($query) {
  249. $query->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_minimum_limit');
  250. }
  251. ])->whereIn('join_detail_coupon_id', $couponIds)
  252. ->where('join_coupon_detail_member_id', $memberId);
  253. if ($settlementNow == 'Y') {
  254. $details = $details->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
  255. } else {
  256. $details = $details->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  257. }
  258. $details = $details->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,COUNT(*) as count')
  259. ->groupBy('join_detail_coupon_id', 'join_coupon_detail_member_id')
  260. ->get()
  261. ->toArray();
  262. $data = [];
  263. foreach ($details as $detail) {
  264. if (empty($detail['coupon'])) {
  265. continue;
  266. }
  267. $classify = CouponService::couponClassifyInfo($detail['coupon']['coupon_classify'], $detail['coupon']['coupon_category'], $detail['coupon']['coupon_value'], $detail['coupon']['coupon_minimum_limit']);
  268. $data[] = [
  269. 'coupon_id' => $detail['coupon']['coupon_id'],
  270. 'coupon_name' => $detail['coupon']['coupon_name'],
  271. 'classify' => $classify,
  272. 'count' => $detail['count'],
  273. 'coupon_classify' => $detail['coupon']['coupon_classify'],
  274. 'coupon_value' => $detail['coupon']['coupon_value'] ?? '',
  275. 'coupon_minimum_limit' => $detail['coupon']['coupon_minimum_limit'] ?? '',
  276. ];
  277. }
  278. return json_success('', $data);
  279. }
  280. }