CardBatchService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. class CardBatchService
  17. {
  18. /**
  19. * Notes: 添加卡批次
  20. * User: ZhouBenXu
  21. * DateTime: 2024/6/27 上午10:04
  22. * @param $params
  23. */
  24. public static function insert($params)
  25. {
  26. Db::beginTransaction();
  27. try {
  28. // 添加卡批次
  29. $model = new CardBatch();
  30. $model->join_card_batch_user_id = $params['join_card_batch_user_id']; // 批次创建用户 user_id
  31. $model->join_card_batch_category_id = $params['join_card_batch_category_id']; // 批次卡分类ID
  32. $model->card_batch_status = $model::CARD_BATCH_STATUS[$params['card_batch_status']]; // 批次状态
  33. $model->card_batch_category = $params['card_batch_category'] ?? "NORMAL"; // 批次类型 NORMAL
  34. $model->card_batch_name = $params['card_batch_name']; // 卡批次名称
  35. $model->card_batch_prefix = $params['card_batch_prefix']; // 卡批次前缀
  36. $model->card_batch_suffix = $params['card_batch_suffix']; // 卡批次后缀
  37. $model->card_batch_serial_begin = $params['card_batch_serial_begin']; // 卡批次序号起始
  38. $model->card_batch_serial_end = $params['card_batch_serial_end']; // 卡批次序号结束
  39. $model->card_batch_amount = $params['card_batch_amount']; // 卡批次面额
  40. $model->card_batch_added = $params['card_batch_added']; // 卡批次增值赠送
  41. $model->card_batch_validtimes = $params['card_batch_validtimes']; // 卡批次有效期 0=永久 天
  42. $model->card_batch_json = $params['card_batch_json'] ?? "{}"; // 卡JSON {}
  43. $model->card_batch_remark = $params['card_batch_remark'] ?? ""; // 备注
  44. $model->card_batch_extend_json = $params['card_batch_extend_json'] ?? "{}"; // 扩展 {}
  45. if ($model->save()) {
  46. $join_card_batch_id = $model->card_batch_id;
  47. // 添加卡信息
  48. CardService::insert($join_card_batch_id, $params);
  49. }
  50. Db::commit();
  51. } catch (\PDOException $e) {
  52. Db::rollBack();
  53. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  54. return json_fail('数据写入失败~');
  55. } catch (BusinessException $e) {
  56. Db::rollBack();
  57. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  58. return json_fail($e->getMessage());
  59. } catch (\Exception $e) {
  60. Db::rollBack();
  61. dump($e->getTrace());
  62. return json_fail('数据写入失败~');
  63. }
  64. _syslog("添加储值卡批次", "卡批次名称【" . $params['card_batch_name'] . "】");
  65. return json_success('success');
  66. }
  67. /**
  68. * Notes: 修改卡批次
  69. * User: ZhouBenXu
  70. * DateTime: 2024/6/27 下午4:32
  71. * @param $params
  72. * @return Response
  73. */
  74. public static function save($params)
  75. {
  76. Db::beginTransaction();
  77. try {
  78. $cardBatchModel = new CardBatch();
  79. $model = $cardBatchModel::find($params['card_batch_id']);
  80. $model->card_batch_name = $params['card_batch_name'];
  81. $params['card_batch_status'] = $model->card_batch_status = $cardBatchModel::CARD_BATCH_STATUS[$params['card_batch_status']]; // 批次状态
  82. if ($params['validtimes_status'] == 0) {
  83. $model->card_batch_validtimes = 0; // 卡批次有效期 0=永久 天
  84. } elseif ($params['validtimes_status'] == 1) {
  85. $model->card_batch_validtimes = bcadd($model->card_batch_validtimes, $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
  86. } elseif ($params['validtimes_status'] == 2) {
  87. $end_date = (string)bcadd(strtotime($model->card_batch_addtimes), (bcsub($model->card_batch_validtimes, $params['card_batch_validtimes']) * 86400));
  88. if ($end_date <= time()) {
  89. return json_fail('卡批次有效期必须大于当前时间~');
  90. }
  91. $model->card_batch_validtimes = bcsub($model->card_batch_validtimes, $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
  92. }
  93. if ($model->save()) {
  94. // 修改卡批次下面的卡信息
  95. // CardService::saveCardByCardBatchId($params['card_batch_id'], $params);
  96. // 修改卡批次下面的卡信息
  97. $model = new Card();
  98. $data['card_name'] = $params['card_batch_name'];
  99. $card_status = array_values($model::CARD_STATUS);
  100. if (in_array($params['card_batch_status'], $card_status)) {
  101. $data['card_status'] = $params['card_batch_status']; // 卡批次状态
  102. }
  103. if ($params['validtimes_status'] == 0) { // 卡批次有效期 0 永久天 1 增加 2减少
  104. $data['card_validtimes'] = 0;
  105. } elseif ($params['validtimes_status'] == 1) {
  106. $data['card_validtimes'] = Db::raw('card_validtimes+' . $params['card_batch_validtimes'] * 86400);
  107. } elseif ($params['validtimes_status'] == 2) {
  108. $data['card_validtimes'] = Db::raw('card_validtimes-' . $params['card_batch_validtimes'] * 86400);;
  109. }
  110. $model->where('join_card_batch_id', $params['card_batch_id'])->update($data);
  111. }
  112. Db::commit();
  113. } catch (\PDOException $e) {
  114. Db::rollBack();
  115. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  116. return json_fail('数据写入失败~');
  117. } catch (BusinessException $e) {
  118. Db::rollBack();
  119. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  120. return json_fail($e->getMessage());
  121. } catch (\Exception $e) {
  122. Db::rollBack();
  123. dump($e->getTrace());
  124. return json_fail('数据写入失败~');
  125. }
  126. _syslog("修改储值卡批次", "卡批次名称【" . $params['card_batch_name'] . "】");
  127. return json_success('success');
  128. }
  129. /**
  130. * Notes: 删除卡批次
  131. * User: ZhouBenXu
  132. * DateTime: 2024/6/27 下午5:53
  133. * @param $card_batch_ids
  134. * @return Response
  135. */
  136. public static function delete($card_batch_ids)
  137. {
  138. if (!is_array($card_batch_ids)) {
  139. $card_batch_ids = [$card_batch_ids];
  140. }
  141. $cardBatch = CardBatch::whereIn('card_batch_id', $card_batch_ids)->get()->toArray();
  142. if (!$cardBatch) {
  143. return json_fail("数据错误~");
  144. }
  145. // 是否有激活的卡片
  146. if (Card::whereIn('join_card_batch_id', $card_batch_ids)->where('card_status', 'ACTIVED')->exists()) {
  147. return json_fail('存在已激活卡片不可以删除');
  148. }
  149. Db::beginTransaction();
  150. try {
  151. CardBatch::whereIn('card_batch_id', $card_batch_ids)->delete();
  152. Card::whereIn('join_card_batch_id', $card_batch_ids)->delete();
  153. Db::commit();
  154. _syslog("删除储值卡批次", "删除的卡批次ID【" . implode(',', $card_batch_ids) . '】');
  155. return json_success("卡批次删除成功");
  156. } catch (\Exception $e) {
  157. Db::rollBack();
  158. return json_fail("卡批次删除失败~");
  159. }
  160. }
  161. /**
  162. * Notes: 卡批次列表
  163. * User: ZhouBenXu
  164. * DateTime: 2024/6/28 上午9:43
  165. * @param $params
  166. * @return Response
  167. */
  168. public static function getList($params)
  169. {
  170. $cardBatchModel = new CardBatch();
  171. $page = $params['page'];
  172. $page_size = $params['pageSize'];
  173. $card_batch_name = $params['card_batch_name'] ?? "";
  174. $card_batch_status = !empty($params['card_batch_status']) ? $cardBatchModel::CARD_BATCH_STATUS[$params['card_batch_status']] : "";
  175. $join_card_batch_category_id = $params['join_card_batch_category_id'] ?? "";
  176. $rows = $cardBatchModel::select('*')
  177. ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
  178. $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
  179. })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
  180. $query->where('card_batch_status', $card_batch_status);
  181. })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
  182. $query->where('join_card_batch_category_id', $join_card_batch_category_id);
  183. })
  184. ->orderBy('card_batch_id', 'DESC')
  185. ->forPage($page, $page_size)
  186. ->get()
  187. ->toArray();
  188. $total = $cardBatchModel::select('*')
  189. ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
  190. $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
  191. })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
  192. $query->where('card_batch_status', $card_batch_status);
  193. })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
  194. $query->where('join_card_batch_category_id', $join_card_batch_category_id);
  195. })->count();
  196. if (!empty($rows)) {
  197. // 查询求和数据信息
  198. $card_batch_id_string = implode(',', array_column($rows, 'card_batch_id'));
  199. $sql = "select join_card_batch_id,
  200. -- INIT
  201. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN counts ELSE 0 END),0) as status_INIT_counts,
  202. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN amounts ELSE 0 END),0) as status_INIT_amounts,
  203. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN addeds ELSE 0 END),0) as status_INIT_addeds,
  204. -- PRODUCTED
  205. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN counts ELSE 0 END),0) as status_PRODUCTED_counts,
  206. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN amounts ELSE 0 END),0) as status_PRODUCTED_amounts,
  207. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN addeds ELSE 0 END),0) as status_PRODUCTED_addeds,
  208. -- ENABLED
  209. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN counts ELSE 0 END),0) as status_ENABLED_counts,
  210. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN amounts ELSE 0 END),0) as status_ENABLED_amounts,
  211. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN addeds ELSE 0 END),0) as status_ENABLED_addeds,
  212. -- WAITING
  213. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN counts ELSE 0 END),0) as status_WAITING_counts,
  214. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN amounts ELSE 0 END),0) as status_WAITING_amounts,
  215. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN addeds ELSE 0 END),0) as status_WAITING_addeds,
  216. -- PENDING
  217. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN counts ELSE 0 END),0) as status_PENDING_counts,
  218. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN amounts ELSE 0 END),0) as status_PENDING_amounts,
  219. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN addeds ELSE 0 END),0) as status_PENDING_addeds,
  220. -- ACTIVED
  221. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN counts ELSE 0 END),0) as status_ACTIVED_counts,
  222. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN amounts ELSE 0 END),0) as status_ACTIVED_amounts,
  223. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN addeds ELSE 0 END),0) as status_ACTIVED_addeds,
  224. -- DISABLED
  225. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN counts ELSE 0 END),0) as status_DISABLED_counts,
  226. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN amounts ELSE 0 END),0) as status_DISABLED_amounts,
  227. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN addeds ELSE 0 END),0) as status_DISABLED_addeds,
  228. -- EXPIRED
  229. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN counts ELSE 0 END),0) as status_EXPIRED_counts,
  230. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN amounts ELSE 0 END),0) as status_EXPIRED_amounts,
  231. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN addeds ELSE 0 END),0) as status_EXPIRED_addeds,
  232. -- PAUSED
  233. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN counts ELSE 0 END),0) as status_PAUSED_counts,
  234. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN amounts ELSE 0 END),0) as status_PAUSED_amounts,
  235. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN addeds ELSE 0 END),0) as status_PAUSED_addeds
  236. 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
  237. group by join_card_batch_id";
  238. $card_status_total = Db::select($sql);
  239. $card_status_total = array_column($card_status_total, null, 'join_card_batch_id');
  240. foreach ($rows as $key => &$value) {
  241. $value['card_batch_status_value'] = $value['card_batch_status'];
  242. $value['card_batch_status'] = array_flip($cardBatchModel::CARD_BATCH_STATUS)[$value['card_batch_status']];
  243. !empty($value['card_batch_json']) && $value['card_batch_json'] = json_decode($value['card_batch_json'], true);
  244. !empty($value['card_batch_extend_json']) && $value['card_batch_extend_json'] = json_decode($value['card_batch_extend_json'], true);
  245. // 合并储值卡面值以及状态数量
  246. if (!empty($card_status_total[$value['card_batch_id']])) {
  247. if (is_array($card_status_total[$value['card_batch_id']])) {
  248. $value = array_merge($value, $card_status_total[$value['card_batch_id']]);
  249. } else {
  250. $value = array_merge($value, json_decode(json_encode($card_status_total[$value['card_batch_id']]), true));
  251. }
  252. }
  253. }
  254. }
  255. return json_success('', compact('rows', 'page', 'page_size', 'total'));
  256. }
  257. /**
  258. * Notes: 卡批次详情
  259. * User: ZhouBenXu
  260. * DateTime: 2024/6/28 上午10:50
  261. * @param $card_batch_id
  262. * @return Response
  263. */
  264. public static function getInfo($card_batch_id)
  265. {
  266. $cardBatch = CardBatch::where('card_batch_id', $card_batch_id)->first() ?? [];
  267. if (!empty($cardBatch)) {
  268. $cardBatch['card_batch_status_value'] = $cardBatch['card_batch_status'];
  269. $cardBatch['card_batch_status'] = array_flip(CardBatch::CARD_BATCH_STATUS)[$cardBatch['card_batch_status']];
  270. !empty($cardBatch['card_batch_json']) && $cardBatch['card_batch_json'] = json_decode($cardBatch['card_batch_json'], true);
  271. !empty($cardBatch['card_batch_extend_json']) && $cardBatch['card_batch_extend_json'] = json_decode($cardBatch['card_batch_extend_json'], true);
  272. }
  273. return json_success('', $cardBatch);
  274. }
  275. }