CardBatchService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. * CardBatchService.php
  4. * User: ZhouBenXu
  5. * Date: 2024/6/27
  6. * Time: 上午9:46
  7. * Notes:
  8. */
  9. namespace app\admin\service\card;
  10. use support\Db;
  11. use support\exception\BusinessException;
  12. use support\Response;
  13. use app\model\CardBatch;
  14. use app\model\Card;
  15. use app\admin\service\card\CardService;
  16. use app\admin\service\sys_manage\CategoryService;
  17. class CardBatchService
  18. {
  19. /**
  20. * Notes: 添加卡批次
  21. * User: ZhouBenXu
  22. * DateTime: 2024/6/27 上午10:04
  23. * @param $params
  24. */
  25. public static function insert($params)
  26. {
  27. Db::beginTransaction();
  28. try {
  29. // 添加卡批次
  30. $model = new CardBatch();
  31. if (is_array($params['join_card_batch_category_id'])) {
  32. $params['join_card_batch_category_id'] = end($params['join_card_batch_category_id']);
  33. }
  34. $model->join_card_batch_user_id = $params['join_card_batch_user_id']; // 批次创建用户 user_id
  35. $model->join_card_batch_category_id = $params['join_card_batch_category_id']; // 批次卡分类ID
  36. $model->card_batch_status = $params['card_batch_status']; // 批次状态
  37. $model->card_batch_category = $params['card_batch_category'] ?? "NORMAL"; // 批次类型 NORMAL
  38. $model->card_batch_name = $params['card_batch_name']; // 卡批次名称
  39. $model->card_batch_prefix = $params['card_batch_prefix']; // 卡批次前缀
  40. $model->card_batch_suffix = $params['card_batch_suffix']; // 卡批次后缀
  41. $model->card_batch_serial_begin = $params['card_batch_serial_begin']; // 卡批次序号起始
  42. $model->card_batch_serial_end = $params['card_batch_serial_end']; // 卡批次序号结束
  43. $model->card_batch_amount = $params['card_batch_amount']; // 卡批次面额
  44. $model->card_batch_validtimes = $params['card_batch_validtimes']; // 卡批次有效期 0=永久 天
  45. $model->card_batch_json = $params['card_batch_json'] ?? "{}"; // 卡JSON {}
  46. $model->card_batch_remark = $params['card_batch_remark'] ?? ""; // 备注
  47. $model->card_batch_extend_json = $params['card_batch_extend_json'] ?? "{}"; // 扩展 {}
  48. if ($model->save()) {
  49. $join_card_batch_id = $model->card_batch_id;
  50. // 添加卡信息
  51. // CardService::insert($join_card_batch_id, $params);
  52. $cardModel = new Card();
  53. // 生成包含序号数组
  54. $card_sort = range($params['card_batch_serial_begin'], $params['card_batch_serial_end']);
  55. $insertData = [];
  56. if ($params['card_batch_validtimes'] != 0) { // 卡批次有效期 0=永久 天
  57. $params['card_batch_validtimes'] = (strtotime(date('Y-m-d 00:00:00')) + $params['card_batch_validtimes'] * 86400) - 1;
  58. }
  59. foreach ($card_sort as $value) {
  60. $info['join_card_batch_id'] = $join_card_batch_id;
  61. $info['join_card_category_id'] = $params['join_card_batch_category_id'];
  62. $info['card_status'] = "INIT";
  63. $info['card_category'] = $params['card_batch_category'] ?? "NORMAL"; // 批次类型 NORMAL
  64. $info['card_name'] = $params['card_batch_name'];
  65. $info['card_prefix'] = $params['card_batch_prefix'];
  66. $info['card_suffix'] = $params['card_batch_suffix'];
  67. $info['card_pass'] = mt_rand(100000, 999999);// 卡密
  68. $info['card_amount'] = $params['card_batch_amount'];
  69. $info['card_validtimes'] = $params['card_batch_validtimes']; // 卡批次有效期 0=永久 天
  70. $info['card_json'] = "{}";
  71. $info['card_assign_json'] = "{}";
  72. $info['card_process_json'] = "{}";
  73. $info['card_sort'] = $value;
  74. $info['card_remark'] = $params['card_batch_remark'] ?? ""; // 备注
  75. $info['card_extend_json'] = "{}";
  76. $info['card_addtimes'] = time();
  77. $insertData[] = $info;
  78. }
  79. $cardModel->insert($insertData);
  80. }
  81. Db::commit();
  82. } catch (\PDOException $e) {
  83. Db::rollBack();
  84. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  85. return json_fail('数据写入失败~');
  86. } catch (BusinessException $e) {
  87. Db::rollBack();
  88. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  89. return json_fail($e->getMessage());
  90. } catch (\Exception $e) {
  91. Db::rollBack();
  92. dump($e->getTrace());
  93. return json_fail('数据写入失败~');
  94. }
  95. _syslog("添加储值卡批次", "卡批次名称【" . $params['card_batch_name'] . "】");
  96. return json_success('success');
  97. }
  98. /**
  99. * Notes: 修改卡批次
  100. * User: ZhouBenXu
  101. * DateTime: 2024/6/27 下午4:32
  102. * @param $params
  103. * @return Response
  104. */
  105. public static function save($params)
  106. {
  107. Db::beginTransaction();
  108. try {
  109. $cardBatchModel = new CardBatch();
  110. $model = $cardBatchModel::find($params['card_batch_id']);
  111. $model->card_batch_name = $params['card_batch_name'];
  112. $model->card_batch_remark = $params['card_batch_remark'] ?? "";
  113. $params['card_batch_status'] && $model->card_batch_status = $params['card_batch_status']; // 批次状态
  114. // 是否有激活的卡片
  115. if (Card::where('join_card_batch_id', $params['card_batch_id'])->where('card_status', 'ACTIVED')->exists()) {
  116. return json_fail('存在已激活卡片不可以修改状态');
  117. }
  118. if ($params['validtimes_status'] == 0) {
  119. $model->card_batch_validtimes = 0; // 卡批次有效期 0=永久 天
  120. } elseif ($params['validtimes_status'] == 1) {
  121. $model->card_batch_validtimes = bcadd($model->card_batch_validtimes, $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
  122. } elseif ($params['validtimes_status'] == 2) {
  123. $end_date = (string)bcadd(strtotime($model->card_batch_addtimes), (bcsub($model->card_batch_validtimes, $params['card_batch_validtimes']) * 86400));
  124. if ($end_date <= time()) {
  125. return json_fail('卡批次有效期必须大于当前时间~');
  126. }
  127. $model->card_batch_validtimes = bcsub($model->card_batch_validtimes, $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
  128. }
  129. if ($model->save()) {
  130. // 修改卡批次下面的卡信息
  131. // CardService::saveCardByCardBatchId($params['card_batch_id'], $params);
  132. // 修改卡批次下面的卡信息
  133. $model = new Card();
  134. // $data['card_name'] = $params['card_batch_name'];
  135. $card_status = array_keys($model::CARD_STATUS);
  136. if (in_array($params['card_batch_status'], $card_status)) {
  137. $data['card_status'] = $params['card_batch_status']; // 卡批次状态
  138. }
  139. if ($params['validtimes_status'] == 0) { // 卡批次有效期 0 永久天 1 增加 2减少
  140. $data['card_validtimes'] = 0;
  141. } elseif ($params['validtimes_status'] == 1) {
  142. $data['card_validtimes'] = Db::raw('card_validtimes+' . $params['card_batch_validtimes'] * 86400);
  143. } elseif ($params['validtimes_status'] == 2) {
  144. $data['card_validtimes'] = Db::raw('card_validtimes-' . $params['card_batch_validtimes'] * 86400);;
  145. }
  146. $model->where('join_card_batch_id', $params['card_batch_id'])->update($data);
  147. }
  148. Db::commit();
  149. } catch (\PDOException $e) {
  150. Db::rollBack();
  151. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  152. return json_fail('数据写入失败~');
  153. } catch (BusinessException $e) {
  154. Db::rollBack();
  155. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  156. return json_fail($e->getMessage());
  157. } catch (\Exception $e) {
  158. Db::rollBack();
  159. dump($e->getTrace());
  160. return json_fail('数据写入失败~');
  161. }
  162. _syslog("修改储值卡批次", "卡批次名称【" . $params['card_batch_name'] . "】");
  163. return json_success('success');
  164. }
  165. /**
  166. * Notes: 删除卡批次
  167. * User: ZhouBenXu
  168. * DateTime: 2024/6/27 下午5:53
  169. * @param $card_batch_ids
  170. * @return Response
  171. */
  172. public static function delete($card_batch_ids)
  173. {
  174. if (!is_array($card_batch_ids)) {
  175. $card_batch_ids = [$card_batch_ids];
  176. }
  177. $cardBatch = CardBatch::whereIn('card_batch_id', $card_batch_ids)->get()->toArray();
  178. if (!$cardBatch) {
  179. return json_fail("数据错误~");
  180. }
  181. // 是否有激活的卡片
  182. if (Card::whereIn('join_card_batch_id', $card_batch_ids)->where('card_status', 'ACTIVED')->exists()) {
  183. return json_fail('存在已激活卡片不可以删除');
  184. }
  185. Db::beginTransaction();
  186. try {
  187. CardBatch::whereIn('card_batch_id', $card_batch_ids)->delete();
  188. Card::whereIn('join_card_batch_id', $card_batch_ids)->delete();
  189. Db::commit();
  190. _syslog("删除储值卡批次", "删除的卡批次ID【" . implode(',', $card_batch_ids) . '】');
  191. return json_success("卡批次删除成功");
  192. } catch (\Exception $e) {
  193. Db::rollBack();
  194. return json_fail("卡批次删除失败~");
  195. }
  196. }
  197. /**
  198. * Notes: 卡批次列表
  199. * User: ZhouBenXu
  200. * DateTime: 2024/6/28 上午9:43
  201. * @param $params
  202. * @return Response
  203. */
  204. public static function getList($params)
  205. {
  206. $cardBatchModel = new CardBatch();
  207. $page = $params['page'];
  208. $page_size = $params['pageSize'];
  209. $card_batch_name = $params['card_batch_name'] ?? "";
  210. $card_batch_status = !empty($params['card_batch_status']) ? $cardBatchModel::CARD_BATCH_STATUS[$params['card_batch_status']] : "";
  211. $join_card_batch_category_id = $params['join_card_batch_category_id'] ?? "";
  212. $rows = $cardBatchModel::select('*')
  213. ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
  214. $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
  215. })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
  216. $query->where('card_batch_status', $card_batch_status);
  217. })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
  218. $query->where('join_card_batch_category_id', $join_card_batch_category_id);
  219. })
  220. ->orderBy('card_batch_id', 'DESC')
  221. ->forPage($page, $page_size)
  222. ->get()
  223. ->toArray();
  224. $total = $cardBatchModel::select('*')
  225. ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
  226. $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
  227. })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
  228. $query->where('card_batch_status', $card_batch_status);
  229. })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
  230. $query->where('join_card_batch_category_id', $join_card_batch_category_id);
  231. })->count();
  232. if (!empty($rows)) {
  233. // 查询所有分类信息
  234. $category = array_column($rows, 'join_card_batch_category_id');
  235. $categoryInfos = CategoryService::getCategoryInfos($category) ?? [];
  236. $categoryInfos && $categoryInfos = array_column($categoryInfos, null, 'category_id');
  237. // 查询求和数据信息
  238. $card_batch_id_string = implode(',', array_column($rows, 'card_batch_id'));
  239. $sql = "select join_card_batch_id,
  240. -- INIT
  241. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN counts ELSE 0 END),0) as status_INIT_counts,
  242. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN amounts ELSE 0 END),0) as status_INIT_amounts,
  243. -- PRODUCTED
  244. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN counts ELSE 0 END),0) as status_PRODUCTED_counts,
  245. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN amounts ELSE 0 END),0) as status_PRODUCTED_amounts,
  246. -- ENABLED
  247. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN counts ELSE 0 END),0) as status_ENABLED_counts,
  248. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN amounts ELSE 0 END),0) as status_ENABLED_amounts,
  249. -- WAITING
  250. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN counts ELSE 0 END),0) as status_WAITING_counts,
  251. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN amounts ELSE 0 END),0) as status_WAITING_amounts,
  252. -- PENDING
  253. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN counts ELSE 0 END),0) as status_PENDING_counts,
  254. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN amounts ELSE 0 END),0) as status_PENDING_amounts,
  255. -- ACTIVED
  256. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN counts ELSE 0 END),0) as status_ACTIVED_counts,
  257. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN amounts ELSE 0 END),0) as status_ACTIVED_amounts,
  258. -- DISABLED
  259. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN counts ELSE 0 END),0) as status_DISABLED_counts,
  260. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN amounts ELSE 0 END),0) as status_DISABLED_amounts,
  261. -- EXPIRED
  262. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN counts ELSE 0 END),0) as status_EXPIRED_counts,
  263. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN amounts ELSE 0 END),0) as status_EXPIRED_amounts,
  264. -- PAUSED
  265. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN counts ELSE 0 END),0) as status_PAUSED_counts,
  266. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN amounts ELSE 0 END),0) as status_PAUSED_amounts
  267. from (select join_card_batch_id, card_status, COUNT(*) as counts, SUM(card_amount) as amounts, SUM(card_added) as addeds from app_card where join_card_batch_id in (" . $card_batch_id_string . ") group by join_card_batch_id, card_status) group_data
  268. group by join_card_batch_id";
  269. $card_status_total = Db::select($sql);
  270. $card_status_total = array_column($card_status_total, null, 'join_card_batch_id');
  271. foreach ($rows as $key => &$value) {
  272. $value['card_batch_status_value'] = $cardBatchModel::CARD_BATCH_STATUS[$value['card_batch_status']];
  273. $value['category_name'] = $categoryInfos[$value['join_card_batch_category_id']]['category_name'] ?? "";
  274. if ($value['card_batch_validtimes'] == 0) {
  275. $value['card_batch_validtimes_value'] = "永久";
  276. } else {
  277. $value['card_batch_validtimes_value'] = $value['card_batch_validtimes'];
  278. }
  279. !empty($value['card_batch_json']) && $value['card_batch_json'] = json_decode($value['card_batch_json'], true);
  280. !empty($value['card_batch_extend_json']) && $value['card_batch_extend_json'] = json_decode($value['card_batch_extend_json'], true);
  281. // 合并储值卡面值以及状态数量
  282. if (!empty($card_status_total[$value['card_batch_id']])) {
  283. if (is_array($card_status_total[$value['card_batch_id']])) {
  284. $value = array_merge($value, $card_status_total[$value['card_batch_id']]);
  285. } else {
  286. $value = array_merge($value, json_decode(json_encode($card_status_total[$value['card_batch_id']]), true));
  287. }
  288. }
  289. }
  290. }
  291. return json_success('', compact('rows', 'page', 'page_size', 'total'));
  292. }
  293. /**
  294. * Notes: 卡批次详情
  295. * User: ZhouBenXu
  296. * DateTime: 2024/6/28 上午10:50
  297. * @param $card_batch_id
  298. * @return Response
  299. */
  300. public static function getInfo($card_batch_id)
  301. {
  302. $cardBatch = CardBatch::where('card_batch_id', $card_batch_id)->first() ?? [];
  303. if (!empty($cardBatch)) {
  304. $cardBatch['card_batch_status_value'] = array_flip(CardBatch::CARD_BATCH_STATUS)[$cardBatch['card_batch_status']];
  305. !empty($cardBatch['card_batch_json']) && $cardBatch['card_batch_json'] = json_decode($cardBatch['card_batch_json'], true);
  306. !empty($cardBatch['card_batch_extend_json']) && $cardBatch['card_batch_extend_json'] = json_decode($cardBatch['card_batch_extend_json'], true);
  307. }
  308. return json_success('', $cardBatch);
  309. }
  310. /**
  311. * Notes: 卡状态下拉列表
  312. * User: ZhouBenXu
  313. * DateTime: 2024/7/1 上午9:27
  314. * @return Response
  315. */
  316. public static function getStatusList()
  317. {
  318. $data = [];
  319. $statusList = CardBatch::CARD_BATCH_STATUS;
  320. foreach ($statusList as $key => $value) {
  321. $info = [];
  322. $info['status'] = $key;
  323. $info['status_value'] = $value;
  324. $data[] = $info;
  325. }
  326. return json_success('', $data);
  327. }
  328. }