CardMainController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. // 发行记录
  133. $cardIssue = CardIssue::where('join_issue_card_main_id', $cardMainId)->orderBy('card_issue_addtimes', 'DESC')->get()->toArray();
  134. // 发行统计
  135. $cardTotal = Card::where('join_card_main_id', $cardMainId)->count();
  136. $isIssueTotal = Card::where('join_card_main_id', $cardMainId)->where('is_issue', 'Y')->count();
  137. $cardInitTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'INIT')->count();
  138. $cardWaitingTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'WAITING')->count();
  139. $cardPendingTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'PENDING')->count();
  140. $cardUsedTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'USED')->count();
  141. $cardDoneTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'DONE')->count();
  142. $cardExpiredTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'EXPIRED')->count();
  143. $cardPausedTotal = Card::where('join_card_main_id', $cardMainId)->where('card_status', 'PAUSED')->count();
  144. $statistics = [
  145. [
  146. 'total' => $cardTotal,
  147. 'isIssueTotal' => $isIssueTotal,
  148. 'initTotal' => $cardInitTotal,
  149. 'waitingTotal' => $cardWaitingTotal,
  150. 'pendingTotal' => $cardPendingTotal,
  151. 'usedTotal' => $cardUsedTotal,
  152. 'doneTotal' => $cardDoneTotal,
  153. 'expiredTotal' => $cardExpiredTotal,
  154. 'pausedTotal' => $cardPausedTotal
  155. ]
  156. ];
  157. $data = [
  158. 'issue' => $cardIssue,
  159. 'statistics' => $statistics
  160. ];
  161. return json_success('', $data);
  162. }
  163. /**
  164. * @Desc 插入前置方法
  165. * @Author Gorden
  166. * @Date 2024/11/14 14:29
  167. *
  168. * @param Request $request
  169. * @return array
  170. * @throws BusinessException
  171. */
  172. public function insertInput(Request $request): array
  173. {
  174. $params = $request->post();
  175. $data = $this->inputFilter($request->post());
  176. if ($data['card_main_valid_mode'] == 'DATE') {
  177. $data['card_main_valid_begin'] = date('Y-m-d 00:00:00', strtotime($data['card_main_valid_begin']));
  178. $data['card_main_valid_end'] = date('Y-m-d 23:59:59', strtotime($data['card_main_valid_end']));
  179. $data['card_main_valid_days'] = 0;
  180. } elseif ($data['card_main_valid_mode'] == 'LONG' || $data['card_main_valid_mode'] == 'DAYS') {
  181. $data['card_main_valid_begin'] = '';
  182. $data['card_main_valid_end'] = '';
  183. }
  184. $cardMainJson = [
  185. 'desc' => $params['card_main_desc'] ?? [],
  186. 'is_recharge' => $params['is_recharge'] ?? 'N',
  187. 'issue' => $params['issue'] ?? 'BATCH',
  188. 'demand_config' => []
  189. ];
  190. if ($params['issue'] == 'DEMAND') {
  191. $cardMainJson['demand_config'] = [
  192. 'length' => $params['card_length'],
  193. 'begin' => $params['card_begin'],
  194. 'end' => $params['card_end'],
  195. 'prefix' => $params['card_prefix'],
  196. 'suffix' => $params['card_suffix'],
  197. 'rand' => $params['card_rand'],
  198. 'rand_varchar' => is_numeric($params['card_rand']) ? 'NUMBER' : 'STRING',
  199. ];
  200. }
  201. $data['card_main_json'] = json_encode($cardMainJson);
  202. return $data;
  203. }
  204. /**
  205. * 更新前置方法
  206. * @param Request $request
  207. * @return array
  208. * @throws BusinessException
  209. */
  210. protected function updateInput(Request $request): array
  211. {
  212. $params = $request->post();
  213. $primary_key = $this->model->getKeyName();
  214. $id = $request->post($primary_key);
  215. $data = $this->inputFilter($request->post());
  216. $model = $this->model->find($id);
  217. if (!$model) {
  218. throw new BusinessException('记录不存在', 2);
  219. }
  220. if ($data['card_main_valid_mode'] == 'DATE') {
  221. $data['card_main_valid_begin'] = date('Y-m-d 00:00:00', strtotime($data['card_main_valid_begin']));
  222. $data['card_main_valid_end'] = date('Y-m-d 23:59:59', strtotime($data['card_main_valid_end']));
  223. $data['card_main_valid_days'] = 0;
  224. } elseif ($data['card_main_valid_mode'] == 'LONG' || $data['card_main_valid_mode'] == 'DAYS') {
  225. $data['card_main_valid_begin'] = '';
  226. $data['card_main_valid_end'] = '';
  227. }
  228. $cardMainJson = [];
  229. if (!empty($model->card_main_json)) {
  230. $cardMainJson = json_decode($model->card_main_json, true);
  231. }
  232. $cardMainJson['desc'] = $params['card_main_desc'] ?? [];
  233. if (!empty($params['is_recharge'])) {
  234. $cardMainJson['is_recharge'] = $params['is_recharge'];
  235. }
  236. if (!empty($params['issue'])) {
  237. $cardMainJson['issue'] = $params['issue'];
  238. }
  239. if ($cardMainJson['issue'] == 'BATCH') {
  240. $cardMainJson['demand_config'] = [];
  241. } elseif ($cardMainJson['issue'] == 'DEMAND') {
  242. $cardMainJson['demand_config'] = [
  243. 'length' => $params['card_length'],
  244. 'begin' => $params['card_begin'],
  245. 'end' => $params['card_end'],
  246. 'prefix' => $params['card_prefix'],
  247. 'suffix' => $params['card_suffix'],
  248. 'rand' => $params['card_rand'],
  249. 'rand_varchar' => is_numeric($params['card_rand']) ? 'NUMBER' : 'STRING',
  250. ];
  251. }
  252. $data['card_main_json'] = json_encode($cardMainJson);
  253. // 卡状态设置为禁用,则未绑定的卡冻结,恢复未发行状态
  254. if ($data['card_main_status'] == 'DISABLED' && $model->card_main_status != 'DISABLED') {
  255. Card::where('join_card_main_id', $id)->where('join_card_member_id', '')->update(['card_status' => 'PAUSED', 'is_issue' => 'N']);
  256. // 记录日志
  257. _syslog("储值卡", "禁用");
  258. } elseif ($data['card_main_status'] == 'ACTIVED' && $model->card_main_status == 'DISABLED') {
  259. Card::where('join_card_main_id', $id)->where('join_card_member_id', '')
  260. ->where('card_status', 'PAUSED')
  261. ->update(['card_status' => 'INIT']);
  262. // 记录日志
  263. _syslog("储值卡", "启用");
  264. }
  265. unset($data[$primary_key]);
  266. return [$id, $data];
  267. }
  268. /**
  269. * @Desc 删除储值卡
  270. * @Author Gorden
  271. * @Date 2024/11/18 16:33
  272. *
  273. * @param Request $request
  274. * @return Response
  275. * @throws BusinessException
  276. */
  277. public function delete(Request $request): Response
  278. {
  279. $ids = $this->deleteInput($request);
  280. Db::beginTransaction();
  281. try {
  282. if (Card::whereIn('join_card_main_id', $ids)->where(function ($query) {
  283. $query->where('card_status', '<>', 'INIT')
  284. ->orWhere('join_card_dept_id', '<>', '')
  285. ->orWhere('join_card_member_id', '<>', '');
  286. })->exists()) {
  287. return json_fail("存在已发行、已分配的卡,无法删除!");
  288. }
  289. $this->doDelete($ids);
  290. // 同时删除批次记录
  291. CardIssue::whereIn('join_issue_card_main_id', $ids)->delete();
  292. // 同时删除卡记录
  293. Card::whereIn('join_card_main_id', $ids)->delete();
  294. Db::commit();
  295. _syslog("储值卡", '删除成功');
  296. return json_success('success');
  297. } catch (\Exception $e) {
  298. Db::rollBack();
  299. _syslog("储值次", '删除失败');
  300. Log::error("储值卡删除失败", ['msg' => $e->getMessage()]);
  301. return json_fail("删除失败");
  302. }
  303. }
  304. /**
  305. * @Desc 储值卡统计
  306. * @Author Gorden
  307. * @Date 2024/11/22 13:06
  308. *
  309. * @param Request $request
  310. * @return Response
  311. */
  312. public function statistics(Request $request)
  313. {
  314. $classify = $request->get('card_main_classify');
  315. $categoryId = $request->get('join_card_main_category_id');
  316. $name = $request->get('card_main_name');
  317. $addtimes = $request->get('card_main_addtimes');
  318. $cardMain = CardMain::when(!empty($classify), function ($query) use ($classify) {
  319. $query->where('card_main_classify', $classify);
  320. })->when(!empty($categoryId), function ($query) use ($categoryId) {
  321. $query->where('join_card_main_category_id', $categoryId);
  322. })->when(!empty($name), function ($query) use ($name) {
  323. $query->where('card_main_name', 'like', '%' . $name . '%');
  324. })->when(!empty($addtimes), function ($query) use ($addtimes) {
  325. $addtimes[0] = strtotime($addtimes[0]);
  326. $addtimes[1] = strtotime($addtimes[1]);
  327. $query->whereBetween('card_main_addtimes', $addtimes);
  328. });
  329. $statistics = [];
  330. $totalModel = clone $cardMain;
  331. $totalList = $totalModel->select('card_main_id', 'card_main_amount')->get()->toArray();
  332. $statistics = [
  333. 'total' => ['total' => 0, 'amount' => 0],
  334. 'is_issue' => ['total' => 0, 'amount' => 0],
  335. 'init' => ['total' => 0, 'amount' => 0],
  336. 'waiting' => ['total' => 0, 'amount' => 0],
  337. 'pending' => ['total' => 0, 'amount' => 0],
  338. 'used' => ['total' => 0, 'amount' => 0],
  339. 'done' => ['total' => 0, 'amount' => 0],
  340. 'paused' => ['total' => 0, 'amount' => 0]
  341. ];
  342. foreach ($totalList as $total) {
  343. // 发行统计
  344. $isIssueTotal = Card::where('is_issue', 'Y')->where('join_card_main_id', $total['card_main_id'])->count();
  345. $statistics['is_issue']['total'] += $isIssueTotal;
  346. $statistics['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $total['card_main_amount'] + $statistics['is_issue']['amount']);
  347. // 待分配统计
  348. $initTotal = Card::where('card_status', 'INIT')->where('join_card_main_id', $total['card_main_id'])->count();
  349. $statistics['init']['total'] += $initTotal;
  350. $statistics['init']['amount'] = sprintf('%.2f', $initTotal * $total['card_main_amount'] + $statistics['init']['amount']);
  351. // 已分配,待售统计
  352. $waitingTotal = Card::where('card_status', 'WAITING')->where('join_card_main_id', $total['card_main_id'])->count();
  353. $statistics['waiting']['total'] += $waitingTotal;
  354. $statistics['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $total['card_main_amount'] + $statistics['waiting']['amount']);
  355. // 已售,待激活统计
  356. $pendingTotal = Card::where('card_status', 'PENDING')->where('join_card_main_id', $total['card_main_id'])->count();
  357. $statistics['pending']['total'] += $pendingTotal;
  358. $statistics['pending']['amount'] = sprintf('%.2f', $pendingTotal * $total['card_main_amount'] + $statistics['pending']['amount']);
  359. // 已激活统计
  360. $usedTotal = Card::where('card_status', 'USED')->where('join_card_main_id', $total['card_main_id'])->count();
  361. $statistics['used']['total'] += $usedTotal;
  362. $statistics['used']['amount'] = sprintf('%.2f', $usedTotal * $total['card_main_amount'] + $statistics['used']['amount']);
  363. // 使用完成统计
  364. $doneTotal = Card::where('card_status', 'DONE')->where('join_card_main_id', $total['card_main_id'])->count();
  365. $statistics['done']['total'] += $doneTotal;
  366. $statistics['done']['amount'] = sprintf('%.2f', $doneTotal * $total['card_main_amount'] + $statistics['done']['amount']);
  367. // 冻结统计
  368. $pausedTotal = Card::where('card_status', 'PAUSED')->where('join_card_main_id', $total['card_main_id'])->count();
  369. $statistics['paused']['total'] += $pausedTotal;
  370. $statistics['paused']['amount'] = sprintf('%.2f', $pausedTotal * $total['card_main_amount'] + $statistics['paused']['total']);
  371. }
  372. $statistics['total']['total'] = $statistics['init']['total'] + $statistics['waiting']['total']
  373. + $statistics['pending']['total'] + $statistics['used']['total'] + $statistics['done']['total']
  374. + $statistics['paused']['total'];
  375. $statistics['total']['amount'] = sprintf('%.2f', $statistics['init']['amount']
  376. + $statistics['waiting']['amount'] + $statistics['pending']['amount'] + $statistics['used']['amount']
  377. + $statistics['done']['amount'] + $statistics['paused']['amount']);
  378. return json_success('success', $statistics);
  379. }
  380. }