CardMainController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\controller\Curd;
  4. use app\model\Card;
  5. use app\model\CardIssue;
  6. use app\model\CardMain;
  7. use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
  8. use Illuminate\Database\Query\Builder as QueryBuilder;
  9. use support\Db;
  10. use support\exception\BusinessException;
  11. use support\Log;
  12. use support\Model;
  13. use support\Request;
  14. use support\Response;
  15. use Tinywan\Jwt\JwtToken;
  16. class CardMainController extends Curd
  17. {
  18. public function __construct()
  19. {
  20. $this->model = new CardMain();
  21. }
  22. public function select(Request $request): Response
  23. {
  24. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  25. $order = $request->get('order', 'desc');
  26. $field = $field ?? 'card_main_addtimes';
  27. if (!empty($where['card_main_name'])) {
  28. $where['card_main_name'] = ['like', $where['card_main_name']];
  29. }
  30. if (!empty($where['card_main_addtimes'])) {
  31. $where['card_main_addtimes'][0] = strtotime($where['card_main_addtimes'][0]);
  32. $where['card_main_addtimes'][1] = strtotime($where['card_main_addtimes'][1]);
  33. }
  34. $query = $this->doSelect($where, $field, $order);
  35. return $this->doFormat($query, $format, $limit);
  36. }
  37. /**
  38. * 指定查询where条件,并没有真正的查询数据库操作
  39. * @param array $where
  40. * @param string|null $field
  41. * @param string $order
  42. * @return EloquentBuilder|QueryBuilder|Model
  43. */
  44. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  45. {
  46. $model = $this->model->with('category');
  47. foreach ($where as $column => $value) {
  48. if (is_array($value)) {
  49. if ($value[0] === 'like' || $value[0] === 'not like') {
  50. $model = $model->where($column, $value[0], "%$value[1]%");
  51. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  52. $model = $model->where($column, $value[0], $value[1]);
  53. } elseif ($value[0] == 'in' && !empty($value[1])) {
  54. $valArr = $value[1];
  55. if (is_string($value[1])) {
  56. $valArr = explode(",", trim($value[1]));
  57. }
  58. $model = $model->whereIn($column, $valArr);
  59. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  60. $valArr = $value[1];
  61. if (is_string($value[1])) {
  62. $valArr = explode(",", trim($value[1]));
  63. }
  64. $model = $model->whereNotIn($column, $valArr);
  65. } elseif ($value[0] == 'null') {
  66. $model = $model->whereNull($column);
  67. } elseif ($value[0] == 'not null') {
  68. $model = $model->whereNotNull($column);
  69. } elseif ($value[0] !== '' || $value[1] !== '') {
  70. $model = $model->whereBetween($column, $value);
  71. }
  72. } else {
  73. $model = $model->where($column, $value);
  74. }
  75. }
  76. if ($field) {
  77. $model = $model->orderBy($field, $order);
  78. }
  79. return $model;
  80. }
  81. /**
  82. * @Desc 查询后处理
  83. * @Author Gorden
  84. * @Date 2024/11/15 8:35
  85. *
  86. * @param $items
  87. * @return mixed
  88. */
  89. public function afterQuery($items)
  90. {
  91. foreach ($items as &$item) {
  92. if ($item['card_main_valid_mode'] == 'DAYS') {
  93. $item['valid_term'] = $item['card_main_valid_days'] ? $item['card_main_valid_days'] . '天' : 0;
  94. } elseif ($item['card_main_valid_mode'] == 'LONG') {
  95. $item['valid_term'] = '长期有效';
  96. } elseif ($item['card_main_valid_mode'] == 'DATE') {
  97. $item['valid_term'] = date('Y/m/d', strtotime($item['card_main_valid_begin'])) . '-' . date('Y/m/d', strtotime($item['card_main_valid_end']));
  98. }
  99. }
  100. return $items;
  101. }
  102. /**
  103. * @Desc 卡的下拉搜索
  104. * @Author Gorden
  105. * @Date 2024/11/18 9:51
  106. *
  107. * @return Response
  108. */
  109. public function selectList()
  110. {
  111. $cardMain = CardMain::where('card_main_status', 'ACTIVED')
  112. ->orderBy('card_main_addtimes', 'DESC')
  113. ->select('card_main_id', 'card_main_name')
  114. ->get()
  115. ->toArray();
  116. return json_success('success', $cardMain);
  117. }
  118. /**
  119. * @Desc 储值卡详情
  120. * @Author Gorden
  121. * @Date 2024/11/15 11:33
  122. *
  123. * @param Request $request
  124. * @return Response
  125. */
  126. public function info(Request $request): Response
  127. {
  128. $cardMainId = $request->get('card_main_id');
  129. if (!$cardMainId) {
  130. return json_fail('参数异常');
  131. }
  132. $main = CardMain::with('category')->where('card_main_id', $cardMainId)->first();
  133. if ($main->card_main_valid_mode == 'DAYS') {
  134. $main->valid_term = $main->card_main_valid_days ? $main->card_main_valid_days . '天' : 0;
  135. } elseif ($main->card_main_valid_mode == 'LONG') {
  136. $main->valid_term = '长期有效';
  137. } elseif ($main->card_main_valid_mode == 'DATE') {
  138. $main->valid_term = date('Y/m/d', strtotime($main->card_main_valid_begin)) . '-' . date('Y/m/d', strtotime($main->card_main_valid_end));
  139. }
  140. // 发行记录
  141. $cardIssue = CardIssue::where('join_issue_card_main_id', $cardMainId)->orderBy('card_issue_addtimes', 'DESC')->get()->toArray();
  142. // 发行统计
  143. $cardTotal = Card::where('join_card_main_id', $cardMainId)->count();
  144. $isIssueTotal = Card::where('join_card_main_id', $cardMainId)->where('is_issue', 'Y')->count();
  145. $cardInitTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'INIT')->count();
  146. $cardWaitingTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'WAITING')->count();
  147. $cardPendingTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'PENDING')->count();
  148. $cardUsedTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'USED')->count();
  149. $cardDoneTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'DONE')->count();
  150. $cardExpiredTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'EXPIRED')->count();
  151. $cardPausedTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'PAUSED')->count();
  152. $statistics = [
  153. [
  154. 'total' => $cardTotal,
  155. 'isIssueTotal' => $isIssueTotal,
  156. 'initTotal' => $cardInitTotal,
  157. 'waitingTotal' => $cardWaitingTotal,
  158. 'pendingTotal' => $cardPendingTotal,
  159. 'usedTotal' => $cardUsedTotal,
  160. 'doneTotal' => $cardDoneTotal,
  161. 'expiredTotal' => $cardExpiredTotal,
  162. 'pausedTotal' => $cardPausedTotal
  163. ]
  164. ];
  165. $data = [
  166. 'main' => $main,
  167. 'issue' => $cardIssue,
  168. 'statistics' => $statistics
  169. ];
  170. return json_success('', $data);
  171. }
  172. /**
  173. * @Desc 插入前置方法
  174. * @Author Gorden
  175. * @Date 2024/11/14 14:29
  176. *
  177. * @param Request $request
  178. * @return array
  179. * @throws BusinessException
  180. */
  181. public function insertInput(Request $request): array
  182. {
  183. $params = $request->post();
  184. $data = $this->inputFilter($request->post());
  185. if ($data['card_main_valid_mode'] == 'DATE') {
  186. $data['card_main_valid_begin'] = date('Y-m-d 00:00:00', strtotime($data['card_main_valid_begin']));
  187. $data['card_main_valid_end'] = date('Y-m-d 23:59:59', strtotime($data['card_main_valid_end']));
  188. $data['card_main_valid_days'] = 0;
  189. } elseif ($data['card_main_valid_mode'] == 'LONG' || $data['card_main_valid_mode'] == 'DAYS') {
  190. $data['card_main_valid_begin'] = '';
  191. $data['card_main_valid_end'] = '';
  192. }
  193. $cardMainJson = [
  194. 'desc' => $params['card_main_desc'] ?? [],
  195. 'is_recharge' => $params['is_recharge'] ?? 'N',
  196. 'issue' => $params['issue'] ?? 'BATCH',
  197. 'demand_config' => []
  198. ];
  199. if ($params['issue'] == 'DEMAND') {
  200. $cardMainJson['demand_config'] = [
  201. 'length' => $params['card_length'],
  202. 'begin' => $params['card_begin'],
  203. 'end' => $params['card_end'],
  204. 'prefix' => $params['card_prefix'],
  205. 'suffix' => $params['card_suffix'],
  206. 'rand' => $params['card_rand'],
  207. 'rand_varchar' => is_numeric($params['card_rand']) ? 'NUMBER' : 'STRING',
  208. ];
  209. }
  210. $data['card_main_json'] = json_encode($cardMainJson);
  211. return $data;
  212. }
  213. /**
  214. * 更新前置方法
  215. * @param Request $request
  216. * @return array
  217. * @throws BusinessException
  218. */
  219. protected function updateInput(Request $request): array
  220. {
  221. $params = $request->post();
  222. $primary_key = $this->model->getKeyName();
  223. $id = $request->post($primary_key);
  224. $data = $this->inputFilter($request->post());
  225. $model = $this->model->find($id);
  226. if (!$model) {
  227. throw new BusinessException('记录不存在', 2);
  228. }
  229. if ($data['card_main_valid_mode'] == 'DATE') {
  230. $data['card_main_valid_begin'] = date('Y-m-d 00:00:00', strtotime($data['card_main_valid_begin']));
  231. $data['card_main_valid_end'] = date('Y-m-d 23:59:59', strtotime($data['card_main_valid_end']));
  232. $data['card_main_valid_days'] = 0;
  233. } elseif ($data['card_main_valid_mode'] == 'LONG' || $data['card_main_valid_mode'] == 'DAYS') {
  234. $data['card_main_valid_begin'] = '';
  235. $data['card_main_valid_end'] = '';
  236. }
  237. $cardMainJson = [];
  238. if (!empty($model->card_main_json)) {
  239. $cardMainJson = json_decode($model->card_main_json, true);
  240. }
  241. $cardMainJson['desc'] = $params['card_main_desc'] ?? [];
  242. if (!empty($params['is_recharge'])) {
  243. $cardMainJson['is_recharge'] = $params['is_recharge'];
  244. }
  245. if (!empty($params['issue'])) {
  246. $cardMainJson['issue'] = $params['issue'];
  247. }
  248. if ($cardMainJson['issue'] == 'BATCH') {
  249. $cardMainJson['demand_config'] = [];
  250. } elseif ($cardMainJson['issue'] == 'DEMAND') {
  251. $cardMainJson['demand_config'] = [
  252. 'length' => $params['card_length'],
  253. 'begin' => $params['card_begin'],
  254. 'end' => $params['card_end'],
  255. 'prefix' => $params['card_prefix'],
  256. 'suffix' => $params['card_suffix'],
  257. 'rand' => $params['card_rand'],
  258. 'rand_varchar' => is_numeric($params['card_rand']) ? 'NUMBER' : 'STRING',
  259. ];
  260. }
  261. $data['card_main_json'] = json_encode($cardMainJson);
  262. // 卡状态设置为禁用,则未绑定的卡冻结,恢复未发行状态
  263. if ($data['card_main_status'] == 'DISABLED' && $model->card_main_status != 'DISABLED') {
  264. Card::where('join_card_main_id', $id)->where('join_card_member_id', '')->update(['card_status' => 'PAUSED', 'is_issue' => 'N']);
  265. // 记录日志
  266. _syslog("储值卡", "禁用");
  267. } elseif ($data['card_main_status'] == 'ACTIVED' && $model->card_main_status == 'DISABLED') {
  268. Card::where('join_card_main_id', $id)->where('join_card_member_id', '')
  269. ->where('card_status', 'PAUSED')
  270. ->update(['card_status' => 'INIT']);
  271. // 记录日志
  272. _syslog("储值卡", "启用");
  273. }
  274. unset($data[$primary_key]);
  275. return [$id, $data];
  276. }
  277. /**
  278. * @Desc 删除储值卡
  279. * @Author Gorden
  280. * @Date 2024/11/18 16:33
  281. *
  282. * @param Request $request
  283. * @return Response
  284. * @throws BusinessException
  285. */
  286. public function delete(Request $request): Response
  287. {
  288. $ids = $this->deleteInput($request);
  289. Db::beginTransaction();
  290. try {
  291. if (Card::whereIn('join_card_main_id', $ids)->where(function ($query) {
  292. $query->where('card_status', '<>', 'INIT')
  293. ->orWhere('join_card_dept_id', '<>', '')
  294. ->orWhere('join_card_member_id', '<>', '');
  295. })->exists()) {
  296. return json_fail("存在已发行、已分配的卡,无法删除!");
  297. }
  298. $this->doDelete($ids);
  299. // 同时删除批次记录
  300. CardIssue::whereIn('join_issue_card_main_id', $ids)->delete();
  301. // 同时删除卡记录
  302. Card::whereIn('join_card_main_id', $ids)->delete();
  303. Db::commit();
  304. _syslog("储值卡", '删除成功');
  305. return json_success('success');
  306. } catch (\Exception $e) {
  307. Db::rollBack();
  308. _syslog("储值次", '删除失败');
  309. Log::error("储值卡删除失败", ['msg' => $e->getMessage()]);
  310. return json_fail("删除失败");
  311. }
  312. }
  313. /**
  314. * @Desc 储值卡统计
  315. * @Author Gorden
  316. * @Date 2024/11/22 13:06
  317. *
  318. * @param Request $request
  319. * @return Response
  320. */
  321. public function statistics(Request $request)
  322. {
  323. $classify = $request->get('card_main_classify');
  324. $categoryId = $request->get('join_card_main_category_id');
  325. $name = $request->get('card_main_name');
  326. $addtimes = $request->get('card_main_addtimes');
  327. $amount = $request->get('card_main_amount');
  328. $cardMain = CardMain::when(!empty($classify), function ($query) use ($classify) {
  329. $query->where('card_main_classify', $classify);
  330. })->when(!empty($categoryId), function ($query) use ($categoryId) {
  331. $query->where('join_card_main_category_id', $categoryId);
  332. })->when(!empty($name), function ($query) use ($name) {
  333. $query->where('card_main_name', 'like', '%' . $name . '%');
  334. })->when(!empty($amount), function ($query) use ($amount) {
  335. $query->where('card_main_amount', $amount);
  336. })->when(!empty($addtimes), function ($query) use ($addtimes) {
  337. $addtimes[0] = strtotime($addtimes[0]);
  338. $addtimes[1] = strtotime($addtimes[1]);
  339. $query->whereBetween('card_main_addtimes', $addtimes);
  340. });
  341. $totalModel = clone $cardMain;
  342. $totalList = $totalModel->select('card_main_id', 'card_main_amount')->get()->toArray();
  343. $statistics = [
  344. 'total' => ['total' => 0, 'amount' => 0],
  345. 'is_issue' => ['total' => 0, 'amount' => 0],
  346. 'init' => ['total' => 0, 'amount' => 0],
  347. 'waiting' => ['total' => 0, 'amount' => 0],
  348. 'pending' => ['total' => 0, 'amount' => 0],
  349. 'used' => ['total' => 0, 'amount' => 0],
  350. 'done' => ['total' => 0, 'amount' => 0],
  351. 'expired' => ['total' => 0, 'amount' => 0],
  352. 'paused' => ['total' => 0, 'amount' => 0]
  353. ];
  354. foreach ($totalList as $total) {
  355. // 发行统计
  356. $isIssueTotal = Card::where('is_issue', 'Y')->where('join_card_main_id', $total['card_main_id'])->count();
  357. $statistics['is_issue']['total'] += $isIssueTotal;
  358. $statistics['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $total['card_main_amount'] + $statistics['is_issue']['amount']);
  359. // 待分配统计
  360. $initTotal = Card::where('card_status', 'INIT')->where('join_card_main_id', $total['card_main_id'])->count();
  361. $statistics['init']['total'] += $initTotal;
  362. $statistics['init']['amount'] = sprintf('%.2f', $initTotal * $total['card_main_amount'] + $statistics['init']['amount']);
  363. // 已分配,待售统计
  364. $waitingTotal = Card::where('card_status', 'WAITING')->where('join_card_main_id', $total['card_main_id'])->count();
  365. $statistics['waiting']['total'] += $waitingTotal;
  366. $statistics['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $total['card_main_amount'] + $statistics['waiting']['amount']);
  367. // 已售,待激活统计
  368. $pendingTotal = Card::where('card_status', 'PENDING')->where('join_card_main_id', $total['card_main_id'])->count();
  369. $statistics['pending']['total'] += $pendingTotal;
  370. $statistics['pending']['amount'] = sprintf('%.2f', $pendingTotal * $total['card_main_amount'] + $statistics['pending']['amount']);
  371. // 已激活统计
  372. $usedTotal = Card::where('card_status', 'USED')->where('join_card_main_id', $total['card_main_id'])->count();
  373. $statistics['used']['total'] += $usedTotal;
  374. $statistics['used']['amount'] = sprintf('%.2f', $usedTotal * $total['card_main_amount'] + $statistics['used']['amount']);
  375. // 使用完成统计
  376. $doneTotal = Card::where('card_status', 'DONE')->where('join_card_main_id', $total['card_main_id'])->count();
  377. $statistics['done']['total'] += $doneTotal;
  378. $statistics['done']['amount'] = sprintf('%.2f', $doneTotal * $total['card_main_amount'] + $statistics['done']['amount']);
  379. // 过期统计
  380. $doneTotal = Card::where('card_status', 'EXPIRED')->where('join_card_main_id', $total['card_main_id'])->count();
  381. $statistics['expired']['total'] += $doneTotal;
  382. $statistics['expired']['amount'] = sprintf('%.2f', $doneTotal * $total['card_main_amount'] + $statistics['expired']['amount']);
  383. // 冻结统计
  384. $pausedTotal = Card::where('card_status', 'PAUSED')->where('join_card_main_id', $total['card_main_id'])->count();
  385. $statistics['paused']['total'] += $pausedTotal;
  386. $statistics['paused']['amount'] = sprintf('%.2f', $pausedTotal * $total['card_main_amount'] + $statistics['paused']['total']);
  387. }
  388. $statistics['total']['total'] = $statistics['init']['total'] + $statistics['waiting']['total']
  389. + $statistics['pending']['total'] + $statistics['used']['total'] + $statistics['done']['total']
  390. + $statistics['expired']['total'] + $statistics['paused']['total'];
  391. $statistics['total']['amount'] = sprintf('%.2f', $statistics['init']['amount']
  392. + $statistics['waiting']['amount'] + $statistics['pending']['amount'] + $statistics['used']['amount']
  393. + $statistics['expired']['amount'] + $statistics['done']['amount'] + $statistics['paused']['amount']);
  394. return json_success('success', $statistics);
  395. }
  396. }