CouponController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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'] = null;
  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'] = null;
  242. $data['coupon_validdate_end'] = null;
  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. Db::beginTransaction();
  289. try {
  290. [$id, $data] = $this->updateInput($request);
  291. $originCouponNumber = $data['origin_coupon_number'];
  292. unset($data['origin_coupon_number']);
  293. $this->updateCouponGoods($request->post('goodsContentList'), $id);
  294. $this->doUpdate($id, $data);
  295. if ($originCouponNumber == 0 && $data['coupon_number'] != 0) {
  296. // 查已领取多少了
  297. $detailCount = CouponDetail::where('join_detail_coupon_id', $id)->count();
  298. if ($detailCount < $data['coupon_number']) {
  299. $this->generateCouponDetail($id, $data['coupon_status'], $data['coupon_number'] - $detailCount);
  300. }
  301. } else if ($originCouponNumber < $data['coupon_number']) {
  302. $this->generateCouponDetail($id, $data['coupon_status'], $data['coupon_number'] - $originCouponNumber);
  303. } else if ($originCouponNumber > $data['coupon_number']) {
  304. CouponDetail::where('join_detail_coupon_id', $id)
  305. ->where('join_coupon_detail_member_id', '')
  306. ->limit($originCouponNumber - $data['coupon_number'])
  307. ->delete();
  308. }
  309. // 激活状态,初始改为待领取
  310. if ($data['coupon_status'] == 'ACTIVED') {
  311. CouponDetail::where('join_detail_coupon_id', $id)->where('coupon_detail_status', 'INIT')->update(['coupon_detail_status' => 'PENDING']);
  312. } elseif ($data['coupon_status'] == 'DISABLED') { // 禁用状态,未领取未使用的都是禁用
  313. CouponDetail::where('join_detail_coupon_id', $id)->where('coupon_detail_status', '<>', 'USED')->update(['coupon_detail_status' => 'DISABLED']);
  314. }
  315. Db::commit();
  316. return json_success('success');
  317. } catch (BusinessException $e) {
  318. Db::rollBack();
  319. return json_fail($e->getMessage());
  320. } catch (\Exception $e) {
  321. Db::rollBack();
  322. dump($e->getMessage());
  323. return json_fail('数据更新失败');
  324. }
  325. }
  326. public function updateCouponGoods($goods, $couponId)
  327. {
  328. try {
  329. $couponGoodsIds = CouponGoods::where('join_goods_coupon_id', $couponId)->pluck('join_coupon_goods_id')->toArray();
  330. $goodsIds = array_column($goods, 'goods_id');
  331. // 新的差集
  332. $goodsDiff = array_diff($goodsIds, $couponGoodsIds);
  333. // 旧的差集
  334. $couponDiff = array_diff($couponGoodsIds, $goodsIds);
  335. foreach ($goods as $good) {
  336. // 更新两个中都有的
  337. if (in_array($good['goods_id'], $couponGoodsIds)) {
  338. CouponGoods::where('join_goods_coupon_id', $couponId)->where('join_coupon_goods_id', $good['goods_id'])->update(['join_coupon_goods_sku_id' => $good['sku_id']]);
  339. } elseif (in_array($good['goods_id'], $goodsDiff)) { // 新加的
  340. CouponGoods::insert([
  341. 'join_goods_coupon_id' => $couponId,
  342. 'join_coupon_goods_id' => $good['goods_id'],
  343. 'join_coupon_goods_sku_id' => $good['sku_id'],
  344. 'coupon_goods_json' => '[]',
  345. 'coupon_goods_extend_json' => '[]',
  346. 'coupon_goods_addtimes' => time()
  347. ]);
  348. }
  349. }
  350. if (!empty($couponDiff)) { // 去掉的
  351. foreach ($couponDiff as $item) {
  352. CouponGoods::where('join_goods_coupon_id', $couponId)->where('join_coupon_goods_id', $item)->delete();
  353. }
  354. }
  355. } catch (\Exception $e) {
  356. throw new BusinessException('关联产品更新失败');
  357. }
  358. }
  359. protected function updateInput(Request $request): array
  360. {
  361. $params = $request->post();
  362. $primary_key = $this->model->getKeyName();
  363. $id = $request->post($primary_key);
  364. $data = $this->inputFilter($params);
  365. if (!empty($params['coupon_use_time']) && $params['coupon_use_time'] == 'date') {
  366. $data['coupon_validdate_day'] = null;
  367. $data['coupon_validdate_begin'] = date("Y-m-d H:i:s", strtotime($data['coupon_validdate_begin']));
  368. $data['coupon_validdate_end'] = date("Y-m-d H:i:s", strtotime($data['coupon_validdate_end']));
  369. } elseif (!empty($params['coupon_use_time']) && $params['coupon_use_time'] == 'day') {
  370. $data['coupon_validdate_begin'] = null;
  371. $data['coupon_validdate_end'] = null;
  372. }
  373. $model = $this->model->find($id);
  374. // 原数量
  375. $data['origin_coupon_number'] = $model->coupon_number;
  376. if (!$model) {
  377. throw new BusinessException('记录不存在', 2);
  378. }
  379. $couponJson = [];
  380. if (!empty($model->coupon_json)) {
  381. $couponJson = json_decode($model->coupon_json, true);
  382. }
  383. if ($params['coupon_use_member'] == 'member' && isset($params['member_list'])) {
  384. $couponJson['member']['list'] = $params['member_list'];
  385. // if (isset($couponJson['member']['range'])) {
  386. $couponJson['member']['range'] = '';
  387. // }
  388. // if (isset($couponJson['member']['where'])) {
  389. $couponJson['member']['where'] = '';
  390. // }
  391. } else if ($params['coupon_use_member'] == 'condition') {
  392. if (isset($params['condition']) && $params['condition'] == 'is_vip') {
  393. $couponJson['member']['range'] = 'VIP会员';
  394. $couponJson['member']['where'] = urlencode(" `member_is_vip` = 'Y'");
  395. } elseif (isset($params['condition']) && $params['condition'] == 'is_employ') {
  396. $couponJson['member']['range'] = '公司员工';
  397. $couponJson['member']['where'] = urlencode(" `member_classify` = 'EMPLOY'");
  398. } elseif (isset($params['condition']) && $params['condition'] == 'is_member') {
  399. $couponJson['member']['range'] = '会员';
  400. $couponJson['member']['where'] = urlencode(" `member_classify` = 'MEMBER'");
  401. }
  402. // if (isset($couponJson['member']['list'])) {
  403. $couponJson['member']['list'] = [];
  404. // }
  405. } else if ($params['coupon_use_member'] == 'all') {
  406. $couponJson['member']['list'] = [];
  407. $couponJson['member']['range'] = '全部员工';
  408. $couponJson['member']['where'] = '';
  409. if (isset($couponJson['member']['list'])) {
  410. $couponJson['member']['list'] = [];
  411. }
  412. }
  413. if (!empty($params['premises_range']) && $params['premises_range'] == 'all') {
  414. $couponJson['premises_range'] = '所有门店可用';
  415. unset($couponJson['premises']);
  416. } elseif (!empty($params['premises_range']) && $params['premises_range'] == 'part') {
  417. $couponJson['premises'] = $params['premises'];
  418. $couponJson['premises_range'] = '部分门店可用';
  419. }
  420. $data['coupon_json'] = json_encode($couponJson, JSON_UNESCAPED_UNICODE);
  421. unset($data[$primary_key]);
  422. return [$id, $data];
  423. }
  424. /**
  425. * 优惠券详情
  426. */
  427. public function info(Request $request): Response
  428. {
  429. $couponId = $request->get('coupon_id', '');
  430. if (!$couponId) {
  431. return json_fail('参数异常');
  432. }
  433. $coupon = Coupon::where('coupon_id', $couponId)->first();
  434. if (!$coupon) {
  435. return json_fail('数据异常');
  436. }
  437. // 拼选择的商品
  438. $couponGoods = CouponGoods::with([
  439. 'goods' => function ($query) {
  440. $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');
  441. },
  442. 'skus',
  443. 'sku'
  444. ])->where('join_goods_coupon_id', $couponId)
  445. ->get()
  446. ->toArray();
  447. $goodsIds = [];
  448. $goodsContent = [];
  449. foreach ($couponGoods as $couponGood) {
  450. $goodsIds[] = $couponGood['join_coupon_goods_id'];
  451. $content = [
  452. 'goods_id' => $couponGood['join_coupon_goods_id'],
  453. 'sku_id' => $couponGood['join_coupon_goods_sku_id'],
  454. ];
  455. if (isset($couponGood['goods'])) {
  456. $content['goods_name'] = $couponGood['goods']['name'];
  457. $content['goods_classify'] = $couponGood['goods']['goods_classify'];
  458. $content['goods_cover'] = getenv('STORAGE_DOMAIN') . $couponGood['goods']['goods_cover'];
  459. }
  460. if (isset($couponGood['sku']) && isset($couponGood['sku']['goods_sku_specs_json'])) {
  461. // $skuSpecsJson = json_decode($couponGood['sku']['goods_sku_specs_json'], true);
  462. // $skuName = '';
  463. // foreach ($skuSpecsJson as $item) {
  464. // if (is_array($item)) {
  465. // $item = implode('', $item);
  466. // }
  467. // $skuName .= $item . ',';
  468. // }
  469. // $content['sku_name'] = rtrim($skuName, ',');
  470. $content['goods_sales_price'] = $couponGood['sku']['goods_sku_sales_price'];
  471. }
  472. if (isset($couponGood['skus'])) {
  473. foreach ($couponGood['skus'] as $key => $skus) {
  474. if (!empty($skus['goods_sku_specs_json'])) {
  475. $skuSpecsJson = json_decode($skus['goods_sku_specs_json'], true);
  476. $skuName = '';
  477. foreach ($skuSpecsJson as $item) {
  478. if (is_array($item)) {
  479. $item = implode('', $item);
  480. }
  481. $skuName .= $item . ',';
  482. }
  483. $couponGood['skus'][$key]['sku_name'] = rtrim($skuName, ',');
  484. }
  485. }
  486. $content['skus'] = $couponGood['skus'];
  487. }
  488. $goodsContent[] = $content;
  489. }
  490. // 选中的会员
  491. $memberLists = [];
  492. // 可用门店
  493. $premises = [];
  494. if (!empty($coupon->coupon_json)) {
  495. $couponJson = json_decode($coupon->coupon_json, true);
  496. if (isset($couponJson['member']) && isset($couponJson['member']['list']) && !empty($couponJson['member']['list'])) {
  497. $res = Db::table('member')
  498. ->leftjoin('member_cert', 'member.member_id', '=', 'member_cert.join_cert_member_id')
  499. ->leftjoin('member_info', 'member.member_id', '=', 'member_info.join_info_member_id')
  500. ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name', 'member_info.member_info_nickname')
  501. ->where('member_status', 'ACTIVED')
  502. ->whereIn('member_id', $couponJson['member']['list'])
  503. ->get();
  504. $memberLists = [];
  505. foreach ($res as $item) {
  506. $name = '';
  507. if (!empty($item->member_cert_name)) {
  508. $name = $item->member_cert_name;
  509. } else if (!empty($item->member_info_nickname)) {
  510. $name = $item->member_info_nickname;
  511. }
  512. $value = !empty($name) ? $name . '-' : '';
  513. if ($item->member_mobile != '0000') {
  514. $value .= $item->member_mobile;
  515. }
  516. if (empty($value)) {
  517. $value = $item->member_mobile;
  518. }
  519. $value = rtrim($value, '-');
  520. $memberLists[] = [
  521. 'key' => $item->member_id,
  522. 'value' => $value,
  523. ];
  524. }
  525. }
  526. if (isset($couponJson['premises']) && !empty($couponJson['premises'])) {
  527. $premises = SysDept::whereIn('dept_id', $couponJson['premises'])
  528. ->select('dept_id', 'dept_name')
  529. ->get()
  530. ->toArray();
  531. }
  532. }
  533. $data = [
  534. 'goods_ids' => $goodsIds,
  535. 'goods_content' => $goodsContent,
  536. 'member_lists' => $memberLists,
  537. 'premises_list' => $premises
  538. ];
  539. return json_success('', $data);
  540. }
  541. /**
  542. * 删除
  543. * @param Request $request
  544. * @return Response
  545. * @throws BusinessException
  546. */
  547. public function delete(Request $request): Response
  548. {
  549. $ids = $this->deleteInput($request);
  550. Db::beginTransaction();
  551. try {
  552. // 删除优惠券关联的产品
  553. foreach ($ids as $id) {
  554. $couponDetail = CouponDetail::with([
  555. 'coupon'=>function($query){
  556. $query->select('coupon_id','coupon_name');
  557. }
  558. ])->where('join_detail_coupon_id',$id)
  559. ->where('join_coupon_detail_member_id','<>','')
  560. ->first();
  561. if (!empty($couponDetail)){
  562. Db::rollBack();
  563. return json_fail($couponDetail->coupon && $couponDetail->coupon->coupon_name ? $couponDetail->coupon->coupon_name."已被领取,无法删除,请禁用" : "删除的优惠券中有已被领取的,无法删除,请禁用");
  564. }
  565. CouponGoods::where('join_goods_coupon_id', $id)->delete();
  566. // CouponDetail::where('join_detail_coupon_id', $id)
  567. // ->whereIn('coupon_detail_status',['INIT','PENDING','ACTIVED'])
  568. // ->delete();
  569. // CouponDetail::where('join_detail_coupon_id', $id)
  570. // ->where('coupon_detail_status','WAITING')
  571. // ->update(['coupon_detail_status'=>'DISABLED']);
  572. }
  573. $this->doDelete($ids);
  574. Db::commit();
  575. return json_success('success');
  576. } catch (\Exception $e) {
  577. dump($e->getMessage());
  578. Db::rollBack();
  579. return json_fail('删除失败');
  580. }
  581. }
  582. private function generateCouponDetail($couponId, $couponStatus, $nbr)
  583. {
  584. for ($i = 0; $i < intval($nbr); $i++) {
  585. CouponDetail::insert([
  586. 'coupon_detail_id' => 'CUDT' . str_pad(SysSerial::getSerial(), 6, '0') . random_string(6, 'up'),
  587. 'join_detail_coupon_id' => $couponId,
  588. 'coupon_detail_status' => $couponStatus == 'ACTIVED' ? 'PENDING' : 'INIT',
  589. 'coupon_detail_addtimes' => time()
  590. ]);
  591. }
  592. }
  593. }