CouponPacketSequenceController.php 12 KB

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