CouponController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\validate\coupon\CouponValidate;
  4. use app\admin\validate\device\DeviceValidate;
  5. use app\controller\Curd;
  6. use app\model\Coupon;
  7. use app\model\CouponDetail;
  8. use app\model\CouponGoods;
  9. use app\model\Device;
  10. use app\model\Member;
  11. use app\model\SysDept;
  12. use app\model\SysSerial;
  13. use support\Db;
  14. use support\exception\BusinessException;
  15. use support\Request;
  16. use support\Response;
  17. use Tinywan\Jwt\JwtToken;
  18. class CouponController extends Curd
  19. {
  20. public function __construct()
  21. {
  22. $this->model = new Coupon();
  23. $this->validate = true;
  24. $this->validateClass = new CouponValidate();
  25. }
  26. /**
  27. * @Desc 列表
  28. * @Author Gorden
  29. * @Date 2024/3/28 15:01
  30. *
  31. * @param Request $request
  32. * @return Response
  33. * @throws \support\exception\BusinessException
  34. */
  35. public function select(Request $request): Response
  36. {
  37. $joinCouponCategoryId = $request->get('join_coupon_category_id', []);
  38. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  39. if (!empty($joinCouponCategoryId)) {
  40. $where['join_coupon_category_id'] = ['in', implode(',', $joinCouponCategoryId)];
  41. }
  42. $order = $request->get('order', 'desc');
  43. $field = $field ?? 'coupon_addtimes';
  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. $model = $this->model->with([
  50. 'category' => function ($query) {
  51. $query->select('category_id', 'category_name');
  52. },
  53. 'user',
  54. 'detail'
  55. ]);
  56. foreach ($where as $column => $value) {
  57. if (is_array($value)) {
  58. if ($value[0] === 'like' || $value[0] === 'not like') {
  59. $model = $model->where($column, $value[0], "%$value[1]%");
  60. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  61. $model = $model->where($column, $value[0], $value[1]);
  62. } elseif ($value[0] == 'in' && !empty($value[1])) {
  63. $valArr = $value[1];
  64. if (is_string($value[1])) {
  65. $valArr = explode(",", trim($value[1]));
  66. }
  67. $model = $model->whereIn($column, $valArr);
  68. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  69. $valArr = $value[1];
  70. if (is_string($value[1])) {
  71. $valArr = explode(",", trim($value[1]));
  72. }
  73. $model = $model->whereNotIn($column, $valArr);
  74. } elseif ($value[0] == 'null') {
  75. $model = $model->whereNull($column);
  76. } elseif ($value[0] == 'not null') {
  77. $model = $model->whereNotNull($column);
  78. } elseif ($value[0] !== '' || $value[1] !== '') {
  79. $model = $model->whereBetween($column, $value);
  80. }
  81. } else {
  82. $model = $model->where($column, $value);
  83. }
  84. }
  85. if ($field) {
  86. $model = $model->orderBy($field, $order);
  87. }
  88. return $model;
  89. }
  90. protected function afterQuery($items)
  91. {
  92. foreach ($items as &$item) {
  93. if (!empty($item['coupon_json'])) {
  94. $couponJson = json_decode($item['coupon_json'], true);
  95. if (isset($couponJson['member'])) {
  96. if (isset($couponJson['member']['list']) && !empty($couponJson['member']['list'])) {
  97. $item['coupon_use_member'] = 'member';
  98. $item['memberList'] = $couponJson['member']['list'];
  99. } else if (isset($couponJson['member']['where']) && !empty($couponJson['member']['where'])) {
  100. if (trim($couponJson['member']['where']) == '1') {
  101. $item['coupon_use_member'] = 'all';
  102. } else {
  103. $item['coupon_use_member'] = 'condition';
  104. $where = str_replace('`', '', urldecode($couponJson['member']['where']));
  105. $whereArr = explode('=', $where);
  106. // dump($whereArr);
  107. if (isset($whereArr[0]) && trim($whereArr[0]) == 'member_is_vip') {
  108. $item['condition'] = 'is_vip';
  109. } else if (isset($whereArr[0]) && trim($whereArr[0]) == 'member_classify') {
  110. if (isset($whereArr[1]) && str_replace("'", '', trim($whereArr[1])) == 'MEMBER') {
  111. $item['condition'] = 'is_member';
  112. } else if (isset($whereArr[1]) && str_replace("'", '', trim($whereArr[1])) == 'EMPLOY') {
  113. $item['condition'] = 'is_employ';
  114. }
  115. }
  116. }
  117. }
  118. }
  119. if (isset($couponJson['premises'])) {
  120. $item['premises'] = $couponJson['premises'];
  121. } else {
  122. $item['premises_range'] = 'all';
  123. }
  124. }
  125. if (!empty($item['coupon_validdate_begin']) && !empty($item['coupon_validdate_end'])) {
  126. $item['coupon_use_time'] = 'date';
  127. } elseif ($item['coupon_validdate_day']) {
  128. $item['coupon_use_time'] = 'day';
  129. }
  130. $item['member_count'] = 0;
  131. $item['pending_count'] = 0;
  132. $item['used_count'] = 0;
  133. if (!empty($item['detail'])) {
  134. foreach ($item['detail'] as $detail) {
  135. if (!empty($detail['join_coupon_detail_member_id'])) {
  136. $item['member_count'] += 1;
  137. }
  138. if ($detail['coupon_detail_status'] == 'PENDING') {
  139. $item['pending_count'] += 1;
  140. } elseif ($detail['coupon_detail_status'] == 'USED') {
  141. $item['used_count'] += 1;
  142. }
  143. }
  144. }
  145. }
  146. return $items;
  147. }
  148. public function selectList()
  149. {
  150. $coupons = Coupon::where('coupon_status', 'ACTIVED')
  151. ->select('coupon_id', 'coupon_name')
  152. ->get()
  153. ->toArray();
  154. return json_success('', $coupons);
  155. }
  156. // public function afterQuery($items)
  157. // {
  158. // foreach ($items as &$item) {
  159. // $item->coupon_begindate = date('Y-m-d H:i:s', $item->coupon_begindate);
  160. // $item->coupon_enddate = date('Y-m-d H:i:s', $item->coupon_enddate);
  161. // }
  162. //
  163. // return $items;
  164. // }
  165. /**
  166. * @Desc 添加优惠券
  167. * @Author Gorden
  168. * @Date 2024/8/14 15:16
  169. *
  170. * @param Request $request
  171. * @return Response
  172. */
  173. public function insert(Request $request): Response
  174. {
  175. if ($this->validate && !$this->validateClass->scene('add')->check($request->post())) {
  176. return json_fail($this->validateClass->getError());
  177. }
  178. $goods = $request->post('goodsContentList', []);
  179. if (empty($goods)) {
  180. return json_fail("请选择关联的产品");
  181. }
  182. Db::beginTransaction();
  183. try {
  184. $data = $this->insertInput($request);
  185. $data['coupon_creator_user_id'] = JwtToken::getCurrentId();
  186. $this->doInsert($data);
  187. // 关联优惠券和商品、规格
  188. $this->insertCouponGoods($goods, $data['coupon_id']);
  189. if ($data['coupon_number'] > 0) {
  190. $this->generateCouponDetail($data['coupon_id'], $data['coupon_status'], $data['coupon_number']);
  191. }
  192. Db::commit();
  193. return json_success('success');
  194. } catch (BusinessException $customException) {
  195. Db::rollBack();
  196. dump($customException->getMessage());
  197. return json_fail($customException->getMessage());
  198. } catch (\Exception $e) {
  199. Db::rollBack();
  200. dump($e->getMessage());
  201. return json_fail('数据写入失败');
  202. }
  203. }
  204. /**
  205. * @Desc 关联优惠券、产品、规格
  206. * @Author Gorden
  207. * @Date 2024/8/14 14:32
  208. *
  209. * @param $goods
  210. * @param $couponId
  211. * @return void
  212. * @throws BusinessException
  213. */
  214. public function insertCouponGoods($goods, $couponId)
  215. {
  216. try {
  217. foreach ($goods as $good) {
  218. CouponGoods::insert([
  219. 'join_goods_coupon_id' => $couponId,
  220. 'join_coupon_goods_id' => $good['goods_id'],
  221. 'join_coupon_goods_sku_id' => $good['sku_id'],
  222. 'coupon_goods_json' => '[]',
  223. 'coupon_goods_extend_json' => '[]',
  224. 'coupon_goods_addtimes' => time()
  225. ]);
  226. }
  227. } catch (\Exception $e) {
  228. throw new BusinessException("关联产品失败");
  229. }
  230. }
  231. protected function insertInput(Request $request): array
  232. {
  233. $params = $request->post();
  234. $data = $this->inputFilter($params);
  235. $data['coupon_id'] = "CU" . str_pad(SysSerial::getSerial(), 6, '0') . random_string(6, 'up');
  236. if (!empty($params['coupon_use_time']) && $params['coupon_use_time'] == 'date') {
  237. $data['coupon_validdate_day'] = 0;
  238. $data['coupon_validdate_begin'] = date("Y-m-d H:i:s", strtotime($data['coupon_validdate_begin']));
  239. $data['coupon_validdate_end'] = date("Y-m-d H:i:s", strtotime($data['coupon_validdate_end']));
  240. } elseif (!empty($params['coupon_use_time']) && $params['coupon_use_time'] == 'day') {
  241. $data['coupon_validdate_begin'] = '';
  242. $data['coupon_validdate_end'] = '';
  243. }
  244. $couponJson = [];
  245. if ($params['coupon_use_member'] == 'member' && isset($params['member_list'])) {
  246. $couponJson['member']['list'] = $params['member_list'];
  247. $couponJson['member']['range'] = '';
  248. $couponJson['member']['where'] = '';
  249. } else if ($params['coupon_use_member'] == 'condition') {
  250. $couponJson['member']['list'] = [];
  251. if (isset($params['condition']) && $params['condition'] == 'is_vip') {
  252. $couponJson['member']['range'] = 'VIP会员';
  253. $couponJson['member']['where'] = urlencode(" `member_is_vip` = 'Y'");
  254. } elseif (isset($params['condition']) && $params['condition'] == 'is_employ') {
  255. $couponJson['member']['range'] = '公司员工';
  256. $couponJson['member']['where'] = urlencode(" `member_classify` = 'EMPLOY'");
  257. } elseif (isset($params['condition']) && $params['condition'] == 'is_member') {
  258. $couponJson['member']['range'] = '会员';
  259. $couponJson['member']['where'] = urlencode(" `member_classify` = 'MEMBER'");
  260. }
  261. } else if ($params['coupon_use_member'] == 'all') {
  262. $couponJson['member']['list'] = [];
  263. $couponJson['member']['range'] = '全部员工';
  264. $couponJson['member']['where'] = '';
  265. }
  266. if (!empty($params['premises_range']) && $params['premises_range'] == 'all') {
  267. $couponJson['premises_range'] = '所有门店可用';
  268. } elseif (!empty($params['premises_range']) && $params['premises_range'] == 'part') {
  269. $couponJson['premises'] = $params['premises'];
  270. $couponJson['premises_range'] = '部分门店可用';
  271. }
  272. $data['coupon_json'] = json_encode($couponJson, JSON_UNESCAPED_UNICODE);
  273. return $data;
  274. }
  275. /**
  276. * @Desc 修改优惠券
  277. * @Author Gorden
  278. * @Date 2024/8/14 15:48
  279. *
  280. * @param Request $request
  281. * @return Response
  282. */
  283. public function update(Request $request): Response
  284. {
  285. if ($this->validate && !$this->validateClass->scene('update')->check($request->post())) {
  286. return json_fail($this->validateClass->getError());
  287. }
  288. $params = $request->post();
  289. Db::beginTransaction();
  290. try {
  291. [$id, $data] = $this->updateInput($request);
  292. $originCouponNumber = $data['origin_coupon_number'];
  293. $originCouponValiddateDay = $data['origin_coupon_validdate_day'];
  294. $originCouponValiddateEnd = $data['origin_coupon_validdate_end'];
  295. unset($data['origin_coupon_number'], $data['origin_coupon_validdate_day'], $data['origin_coupon_validdate_end']);
  296. $this->updateCouponGoods($request->post('goodsContentList'), $id);
  297. if ($originCouponNumber == 0 && $data['coupon_number'] != 0) {
  298. // 查已领取多少了
  299. $detailCount = CouponDetail::where('join_detail_coupon_id', $id)->count();
  300. if ($detailCount < $data['coupon_number']) {
  301. $this->generateCouponDetail($id, $data['coupon_status'], $data['coupon_number'] - $detailCount);
  302. }
  303. } else if ($originCouponNumber < $data['coupon_number']) {
  304. $this->generateCouponDetail($id, $data['coupon_status'], $data['coupon_number'] - $originCouponNumber);
  305. } else if ($originCouponNumber > $data['coupon_number']) {
  306. CouponDetail::where('join_detail_coupon_id', $id)
  307. ->where('join_coupon_detail_member_id', '')
  308. ->limit($originCouponNumber - $data['coupon_number'])
  309. ->delete();
  310. }
  311. // 修改时间了
  312. if ($params['coupon_use_time'] == 'day' && $originCouponValiddateDay != $data['coupon_validdate_day']) {
  313. $this->updateDetailTimeForDay($id, $data['coupon_validdate_day']);
  314. } elseif ($params['coupon_use_time'] == 'date' && $originCouponValiddateEnd != $data['coupon_validdate_end']) {
  315. $this->updateDetailTimeForDate($id, $data['coupon_validdate_end']);
  316. }
  317. // 激活状态,初始改为待领取
  318. if ($data['coupon_status'] == 'ACTIVED') {
  319. CouponDetail::where('join_detail_coupon_id', $id)->where('coupon_detail_status', 'INIT')->update(['coupon_detail_status' => 'PENDING']);
  320. } elseif ($data['coupon_status'] == 'DISABLED') { // 禁用状态,未领取未使用的都是禁用
  321. CouponDetail::where('join_detail_coupon_id', $id)->where('coupon_detail_status', '<>', 'USED')->update(['coupon_detail_status' => 'DISABLED']);
  322. }
  323. $this->doUpdate($id, $data);
  324. Db::commit();
  325. return json_success('success');
  326. } catch (BusinessException $e) {
  327. Db::rollBack();
  328. return json_fail($e->getMessage());
  329. } catch (\Exception $e) {
  330. Db::rollBack();
  331. dump($e->getMessage());
  332. return json_fail('数据更新失败');
  333. }
  334. }
  335. public function updateDetailTimeForDay($couponId, $day)
  336. {
  337. $details = CouponDetail::where('join_detail_coupon_id', $couponId)
  338. ->where('coupon_detail_status', '<>', 'USED')
  339. ->where('coupon_detail_deadline_datetime', '<>', '')
  340. ->get()
  341. ->toArray();
  342. try {
  343. foreach ($details as $detail) {
  344. $couponDetailGainDatetime = strtotime($detail['coupon_detail_gain_datetime']);
  345. CouponDetail::where('coupon_detail_id', $detail['coupon_detail_id'])
  346. ->update([
  347. 'coupon_detail_deadline_datetime' => date('Y-m-d H:i:s', $couponDetailGainDatetime + ($day * 24 * 3600))
  348. ]);
  349. }
  350. } catch (\Exception $e) {
  351. dump($e->getMessage());
  352. throw new BusinessException("优惠券更新失败");
  353. }
  354. }
  355. public function updateDetailTimeForDate($couponId, $endDate)
  356. {
  357. $details = CouponDetail::where('join_detail_coupon_id', $couponId)
  358. ->where('coupon_detail_status', '<>', 'USED')
  359. ->where('coupon_detail_deadline_datetime', '<>', '')
  360. ->get()
  361. ->toArray();
  362. try {
  363. foreach ($details as $detail) {
  364. CouponDetail::where('coupon_detail_id', $detail['coupon_detail_id'])
  365. ->update([
  366. 'coupon_detail_deadline_datetime' => $endDate
  367. ]);
  368. }
  369. } catch (\Exception $e) {
  370. dump($e->getMessage());
  371. throw new BusinessException("优惠券更新失败");
  372. }
  373. }
  374. public function updateCouponGoods($goods, $couponId)
  375. {
  376. try {
  377. $couponGoodsIds = CouponGoods::where('join_goods_coupon_id', $couponId)->pluck('join_coupon_goods_id')->toArray();
  378. $goodsIds = array_column($goods, 'goods_id');
  379. // 新的差集
  380. $goodsDiff = array_diff($goodsIds, $couponGoodsIds);
  381. // 旧的差集
  382. $couponDiff = array_diff($couponGoodsIds, $goodsIds);
  383. foreach ($goods as $good) {
  384. // 更新两个中都有的
  385. if (in_array($good['goods_id'], $couponGoodsIds)) {
  386. CouponGoods::where('join_goods_coupon_id', $couponId)->where('join_coupon_goods_id', $good['goods_id'])->update(['join_coupon_goods_sku_id' => $good['sku_id']]);
  387. } elseif (in_array($good['goods_id'], $goodsDiff)) { // 新加的
  388. CouponGoods::insert([
  389. 'join_goods_coupon_id' => $couponId,
  390. 'join_coupon_goods_id' => $good['goods_id'],
  391. 'join_coupon_goods_sku_id' => $good['sku_id'],
  392. 'coupon_goods_json' => '[]',
  393. 'coupon_goods_extend_json' => '[]',
  394. 'coupon_goods_addtimes' => time()
  395. ]);
  396. }
  397. }
  398. if (!empty($couponDiff)) { // 去掉的
  399. foreach ($couponDiff as $item) {
  400. CouponGoods::where('join_goods_coupon_id', $couponId)->where('join_coupon_goods_id', $item)->delete();
  401. }
  402. }
  403. } catch (\Exception $e) {
  404. throw new BusinessException('关联产品更新失败');
  405. }
  406. }
  407. protected function updateInput(Request $request): array
  408. {
  409. $params = $request->post();
  410. $primary_key = $this->model->getKeyName();
  411. $id = $request->post($primary_key);
  412. $data = $this->inputFilter($params);
  413. if (!empty($params['coupon_use_time']) && $params['coupon_use_time'] == 'date') {
  414. $data['coupon_validdate_day'] = 0;
  415. $data['coupon_validdate_begin'] = date("Y-m-d H:i:s", strtotime($data['coupon_validdate_begin']));
  416. $data['coupon_validdate_end'] = date("Y-m-d H:i:s", strtotime($data['coupon_validdate_end']));
  417. } elseif (!empty($params['coupon_use_time']) && $params['coupon_use_time'] == 'day') {
  418. $data['coupon_validdate_begin'] = '';
  419. $data['coupon_validdate_end'] = '';
  420. }
  421. $model = $this->model->find($id);
  422. // 原数量
  423. $data['origin_coupon_number'] = $model->coupon_number;
  424. // 原优惠天数
  425. $data['origin_coupon_validdate_day'] = $model->coupon_validdate_day;
  426. // 原结束时间
  427. $data['origin_coupon_validdate_end'] = $model->coupon_validdate_end;
  428. if (!$model) {
  429. throw new BusinessException('记录不存在', 2);
  430. }
  431. $couponJson = [];
  432. if (!empty($model->coupon_json)) {
  433. $couponJson = json_decode($model->coupon_json, true);
  434. }
  435. if ($params['coupon_use_member'] == 'member' && isset($params['member_list'])) {
  436. $couponJson['member']['list'] = $params['member_list'];
  437. // if (isset($couponJson['member']['range'])) {
  438. $couponJson['member']['range'] = '';
  439. // }
  440. // if (isset($couponJson['member']['where'])) {
  441. $couponJson['member']['where'] = '';
  442. // }
  443. } else if ($params['coupon_use_member'] == 'condition') {
  444. if (isset($params['condition']) && $params['condition'] == 'is_vip') {
  445. $couponJson['member']['range'] = 'VIP会员';
  446. $couponJson['member']['where'] = urlencode(" `member_is_vip` = 'Y'");
  447. } elseif (isset($params['condition']) && $params['condition'] == 'is_employ') {
  448. $couponJson['member']['range'] = '公司员工';
  449. $couponJson['member']['where'] = urlencode(" `member_classify` = 'EMPLOY'");
  450. } elseif (isset($params['condition']) && $params['condition'] == 'is_member') {
  451. $couponJson['member']['range'] = '会员';
  452. $couponJson['member']['where'] = urlencode(" `member_classify` = 'MEMBER'");
  453. }
  454. // if (isset($couponJson['member']['list'])) {
  455. $couponJson['member']['list'] = [];
  456. // }
  457. } else if ($params['coupon_use_member'] == 'all') {
  458. $couponJson['member']['list'] = [];
  459. $couponJson['member']['range'] = '全部员工';
  460. $couponJson['member']['where'] = '';
  461. if (isset($couponJson['member']['list'])) {
  462. $couponJson['member']['list'] = [];
  463. }
  464. }
  465. if (!empty($params['premises_range']) && $params['premises_range'] == 'all') {
  466. $couponJson['premises_range'] = '所有门店可用';
  467. unset($couponJson['premises']);
  468. } elseif (!empty($params['premises_range']) && $params['premises_range'] == 'part') {
  469. $couponJson['premises'] = $params['premises'];
  470. $couponJson['premises_range'] = '部分门店可用';
  471. }
  472. if (empty($data['coupon_validdate_day'])) {
  473. $data['coupon_validdate_day'] = 0;
  474. }
  475. $data['coupon_json'] = json_encode($couponJson, JSON_UNESCAPED_UNICODE);
  476. unset($data[$primary_key]);
  477. return [$id, $data];
  478. }
  479. /**
  480. * 优惠券详情
  481. */
  482. public function info(Request $request): Response
  483. {
  484. $couponId = $request->get('coupon_id', '');
  485. if (!$couponId) {
  486. return json_fail('参数异常');
  487. }
  488. $coupon = Coupon::where('coupon_id', $couponId)->first();
  489. if (!$coupon) {
  490. return json_fail('数据异常');
  491. }
  492. // 拼选择的商品
  493. $couponGoods = CouponGoods::with([
  494. 'goods' => function ($query) {
  495. $query->select('goods_id', 'goods_id as id', 'goods_name as name', 'join_goods_category_id as pid', 'goods_classify', 'goods_sales_price', 'goods_cover');
  496. },
  497. 'skus',
  498. 'sku'
  499. ])->where('join_goods_coupon_id', $couponId)
  500. ->get()
  501. ->toArray();
  502. $goodsIds = [];
  503. $goodsContent = [];
  504. foreach ($couponGoods as $couponGood) {
  505. $goodsIds[] = $couponGood['join_coupon_goods_id'];
  506. $content = [
  507. 'goods_id' => $couponGood['join_coupon_goods_id'],
  508. 'sku_id' => $couponGood['join_coupon_goods_sku_id'],
  509. ];
  510. if (isset($couponGood['goods'])) {
  511. $content['goods_name'] = $couponGood['goods']['name'];
  512. $content['goods_classify'] = $couponGood['goods']['goods_classify'];
  513. $content['goods_cover'] = getenv('STORAGE_DOMAIN') . $couponGood['goods']['goods_cover'];
  514. }
  515. if (isset($couponGood['sku']) && isset($couponGood['sku']['goods_sku_specs_json'])) {
  516. // $skuSpecsJson = json_decode($couponGood['sku']['goods_sku_specs_json'], true);
  517. // $skuName = '';
  518. // foreach ($skuSpecsJson as $item) {
  519. // if (is_array($item)) {
  520. // $item = implode('', $item);
  521. // }
  522. // $skuName .= $item . ',';
  523. // }
  524. // $content['sku_name'] = rtrim($skuName, ',');
  525. $content['goods_sales_price'] = $couponGood['sku']['goods_sku_sales_price'];
  526. }
  527. if (isset($couponGood['skus'])) {
  528. foreach ($couponGood['skus'] as $key => $skus) {
  529. if (!empty($skus['goods_sku_specs_json'])) {
  530. $skuSpecsJson = json_decode($skus['goods_sku_specs_json'], true);
  531. $skuName = '';
  532. foreach ($skuSpecsJson as $item) {
  533. if (is_array($item)) {
  534. $item = implode('', $item);
  535. }
  536. $skuName .= $item . ',';
  537. }
  538. $couponGood['skus'][$key]['sku_name'] = rtrim($skuName, ',');
  539. }
  540. }
  541. $content['skus'] = $couponGood['skus'];
  542. }
  543. $goodsContent[] = $content;
  544. }
  545. // 选中的会员
  546. $memberLists = [];
  547. // 可用门店
  548. $premises = [];
  549. if (!empty($coupon->coupon_json)) {
  550. $couponJson = json_decode($coupon->coupon_json, true);
  551. if (isset($couponJson['member']) && isset($couponJson['member']['list']) && !empty($couponJson['member']['list'])) {
  552. $res = Db::table('member')
  553. ->leftjoin('member_cert', 'member.member_id', '=', 'member_cert.join_cert_member_id')
  554. ->leftjoin('member_info', 'member.member_id', '=', 'member_info.join_info_member_id')
  555. ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name', 'member_info.member_info_nickname')
  556. ->where('member_status', 'ACTIVED')
  557. ->whereIn('member_id', $couponJson['member']['list'])
  558. ->get();
  559. $memberLists = [];
  560. foreach ($res as $item) {
  561. $name = '';
  562. if (!empty($item->member_cert_name)) {
  563. $name = $item->member_cert_name;
  564. } else if (!empty($item->member_info_nickname)) {
  565. $name = $item->member_info_nickname;
  566. }
  567. $value = !empty($name) ? $name . '-' : '';
  568. if ($item->member_mobile != '0000') {
  569. $value .= $item->member_mobile;
  570. }
  571. if (empty($value)) {
  572. $value = $item->member_mobile;
  573. }
  574. $value = rtrim($value, '-');
  575. $memberLists[] = [
  576. 'key' => $item->member_id,
  577. 'value' => $value,
  578. ];
  579. }
  580. }
  581. if (isset($couponJson['premises']) && !empty($couponJson['premises'])) {
  582. $premises = SysDept::whereIn('dept_id', $couponJson['premises'])
  583. ->select('dept_id', 'dept_name')
  584. ->get()
  585. ->toArray();
  586. }
  587. }
  588. $data = [
  589. 'goods_ids' => $goodsIds,
  590. 'goods_content' => $goodsContent,
  591. 'member_lists' => $memberLists,
  592. 'premises_list' => $premises
  593. ];
  594. return json_success('', $data);
  595. }
  596. /**
  597. * 删除
  598. * @param Request $request
  599. * @return Response
  600. * @throws BusinessException
  601. */
  602. public function delete(Request $request): Response
  603. {
  604. $ids = $this->deleteInput($request);
  605. Db::beginTransaction();
  606. try {
  607. // 删除优惠券关联的产品
  608. foreach ($ids as $id) {
  609. $couponDetail = CouponDetail::with([
  610. 'coupon' => function ($query) {
  611. $query->select('coupon_id', 'coupon_name');
  612. }
  613. ])->where('join_detail_coupon_id', $id)
  614. ->where('join_coupon_detail_member_id', '<>', '')
  615. ->first();
  616. if (!empty($couponDetail)) {
  617. Db::rollBack();
  618. return json_fail($couponDetail->coupon && $couponDetail->coupon->coupon_name ? $couponDetail->coupon->coupon_name . "已被领取,无法删除,请禁用" : "删除的优惠券中有已被领取的,无法删除,请禁用");
  619. }
  620. CouponGoods::where('join_goods_coupon_id', $id)->delete();
  621. // CouponDetail::where('join_detail_coupon_id', $id)
  622. // ->whereIn('coupon_detail_status',['INIT','PENDING','ACTIVED'])
  623. // ->delete();
  624. // CouponDetail::where('join_detail_coupon_id', $id)
  625. // ->where('coupon_detail_status','WAITING')
  626. // ->update(['coupon_detail_status'=>'DISABLED']);
  627. }
  628. $this->doDelete($ids);
  629. Db::commit();
  630. return json_success('success');
  631. } catch (\Exception $e) {
  632. dump($e->getMessage());
  633. Db::rollBack();
  634. return json_fail('删除失败');
  635. }
  636. }
  637. private function generateCouponDetail($couponId, $couponStatus, $nbr)
  638. {
  639. for ($i = 0; $i < intval($nbr); $i++) {
  640. CouponDetail::insert([
  641. 'coupon_detail_id' => 'CUDT' . str_pad(SysSerial::getSerial(), 6, '0') . random_string(6, 'up'),
  642. 'join_detail_coupon_id' => $couponId,
  643. 'coupon_detail_status' => $couponStatus == 'ACTIVED' ? 'PENDING' : 'INIT',
  644. 'coupon_detail_addtimes' => time()
  645. ]);
  646. }
  647. }
  648. }