CouponPacketSequenceController.php 11 KB

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