CardBatchService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. $model->card_batch_validtimes = bcsub($model->card_batch_validtimes, $params['card_batch_validtimes']); // 卡批次有效期 0=永久 天
  88. }
  89. if ($model->save()) {
  90. // 修改卡批次下面的卡信息
  91. CardService::saveCardByCardBatchId($params['card_batch_id'], $params);
  92. }
  93. Db::commit();
  94. } catch (\PDOException $e) {
  95. Db::rollBack();
  96. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  97. return json_fail('数据写入失败~');
  98. } catch (BusinessException $e) {
  99. Db::rollBack();
  100. dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
  101. return json_fail($e->getMessage());
  102. } catch (\Exception $e) {
  103. Db::rollBack();
  104. dump($e->getTrace());
  105. return json_fail('数据写入失败~');
  106. }
  107. _syslog("修改储值卡批次", "卡批次名称【" . $params['card_batch_name'] . "】");
  108. return json_success('success');
  109. }
  110. /**
  111. * Notes: 删除卡批次
  112. * User: ZhouBenXu
  113. * DateTime: 2024/6/27 下午5:53
  114. * @param $card_batch_ids
  115. * @return Response
  116. */
  117. public static function delete($card_batch_ids)
  118. {
  119. if (!is_array($card_batch_ids)) {
  120. $card_batch_ids = [$card_batch_ids];
  121. }
  122. $cardBatch = CardBatch::whereIn('card_batch_id', $card_batch_ids)->get()->toArray();
  123. if (!$cardBatch) {
  124. return json_fail("数据错误~");
  125. }
  126. // 是否有激活的卡片
  127. if (Card::whereIn('join_card_batch_id', $card_batch_ids)->where('card_status', 'ACTIVED')->exists()) {
  128. return json_fail('存在已激活卡片不可以删除');
  129. }
  130. Db::beginTransaction();
  131. try {
  132. CardBatch::whereIn('card_batch_id', $card_batch_ids)->delete();
  133. Card::whereIn('join_card_batch_id', $card_batch_ids)->delete();
  134. Db::commit();
  135. _syslog("删除储值卡批次", "删除的卡批次ID【" . implode(',', $card_batch_ids) . '】');
  136. return json_success("卡批次删除成功");
  137. } catch (\Exception $e) {
  138. Db::rollBack();
  139. return json_fail("卡批次删除失败~");
  140. }
  141. }
  142. /**
  143. * Notes: 卡批次列表
  144. * User: ZhouBenXu
  145. * DateTime: 2024/6/28 上午9:43
  146. * @param $params
  147. * @return Response
  148. */
  149. public static function getList($params)
  150. {
  151. $cardBatchModel = new CardBatch();
  152. $page = $params['page'];
  153. $page_size = $params['pageSize'];
  154. $card_batch_name = $params['card_batch_name'] ?? "";
  155. $card_batch_status = !empty($params['card_batch_status']) ? $cardBatchModel::CARD_BATCH_STATUS[$params['card_batch_status']] : "";
  156. $join_card_batch_category_id = $params['join_card_batch_category_id'] ?? "";
  157. $rows = $cardBatchModel::select('*')
  158. ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
  159. $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
  160. })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
  161. $query->where('card_batch_status', $card_batch_status);
  162. })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
  163. $query->where('join_card_batch_category_id', $join_card_batch_category_id);
  164. })
  165. ->orderBy('card_batch_id', 'DESC')
  166. ->forPage($page, $page_size)
  167. ->get()
  168. ->toArray();
  169. $total = $cardBatchModel::select('*')
  170. ->when(!empty($card_batch_name) && $card_batch_name != '', function ($query) use ($card_batch_name) {
  171. $query->where('card_batch_name', 'like', '%' . $card_batch_name . '%');
  172. })->when(!empty($card_batch_status), function ($query) use ($card_batch_status) {
  173. $query->where('card_batch_status', $card_batch_status);
  174. })->when(!empty($join_card_batch_category_id), function ($query) use ($join_card_batch_category_id) {
  175. $query->where('join_card_batch_category_id', $join_card_batch_category_id);
  176. })->count();
  177. if (!empty($rows)) {
  178. // 查询求和数据信息
  179. $card_batch_id_string = implode(',', array_column($rows, 'card_batch_id'));
  180. $sql = "select join_card_batch_id,
  181. -- INIT
  182. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN counts ELSE 0 END),0) as status_INIT_counts,
  183. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN amounts ELSE 0 END),0) as status_INIT_amounts,
  184. IFNULL(SUM(CASE WHEN card_status = 'INIT' THEN addeds ELSE 0 END),0) as status_INIT_addeds,
  185. -- PRODUCTED
  186. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN counts ELSE 0 END),0) as status_PRODUCTED_counts,
  187. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN amounts ELSE 0 END),0) as status_PRODUCTED_amounts,
  188. IFNULL(SUM(CASE WHEN card_status = 'PRODUCTED' THEN addeds ELSE 0 END),0) as status_PRODUCTED_addeds,
  189. -- ENABLED
  190. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN counts ELSE 0 END),0) as status_ENABLED_counts,
  191. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN amounts ELSE 0 END),0) as status_ENABLED_amounts,
  192. IFNULL(SUM(CASE WHEN card_status = 'ENABLED' THEN addeds ELSE 0 END),0) as status_ENABLED_addeds,
  193. -- WAITING
  194. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN counts ELSE 0 END),0) as status_WAITING_counts,
  195. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN amounts ELSE 0 END),0) as status_WAITING_amounts,
  196. IFNULL(SUM(CASE WHEN card_status = 'WAITING' THEN addeds ELSE 0 END),0) as status_WAITING_addeds,
  197. -- PENDING
  198. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN counts ELSE 0 END),0) as status_PENDING_counts,
  199. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN amounts ELSE 0 END),0) as status_PENDING_amounts,
  200. IFNULL(SUM(CASE WHEN card_status = 'PENDING' THEN addeds ELSE 0 END),0) as status_PENDING_addeds,
  201. -- ACTIVED
  202. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN counts ELSE 0 END),0) as status_ACTIVED_counts,
  203. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN amounts ELSE 0 END),0) as status_ACTIVED_amounts,
  204. IFNULL(SUM(CASE WHEN card_status = 'ACTIVED' THEN addeds ELSE 0 END),0) as status_ACTIVED_addeds,
  205. -- DISABLED
  206. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN counts ELSE 0 END),0) as status_DISABLED_counts,
  207. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN amounts ELSE 0 END),0) as status_DISABLED_amounts,
  208. IFNULL(SUM(CASE WHEN card_status = 'DISABLED' THEN addeds ELSE 0 END),0) as status_DISABLED_addeds,
  209. -- EXPIRED
  210. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN counts ELSE 0 END),0) as status_EXPIRED_counts,
  211. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN amounts ELSE 0 END),0) as status_EXPIRED_amounts,
  212. IFNULL(SUM(CASE WHEN card_status = 'EXPIRED' THEN addeds ELSE 0 END),0) as status_EXPIRED_addeds,
  213. -- PAUSED
  214. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN counts ELSE 0 END),0) as status_PAUSED_counts,
  215. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN amounts ELSE 0 END),0) as status_PAUSED_amounts,
  216. IFNULL(SUM(CASE WHEN card_status = 'PAUSED' THEN addeds ELSE 0 END),0) as status_PAUSED_addeds
  217. 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
  218. group by join_card_batch_id";
  219. $card_status_total = Db::select($sql);
  220. $card_status_total = array_column($card_status_total, null, 'join_card_batch_id');
  221. foreach ($rows as $key => &$value) {
  222. $value['card_batch_status'] = array_flip($cardBatchModel::CARD_BATCH_STATUS)[$value['card_batch_status']];
  223. !empty($value['card_batch_json']) && $value['card_batch_json'] = json_decode($value['card_batch_json'], true);
  224. !empty($value['card_batch_extend_json']) && $value['card_batch_extend_json'] = json_decode($value['card_batch_extend_json'], true);
  225. // 合并储值卡面值以及状态数量
  226. if (!empty($card_status_total[$value['card_batch_id']])) {
  227. if (is_array($card_status_total[$value['card_batch_id']])) {
  228. $value = array_merge($value, $card_status_total[$value['card_batch_id']]);
  229. } else {
  230. $value = array_merge($value, json_decode(json_encode($card_status_total[$value['card_batch_id']]), true));
  231. }
  232. }
  233. }
  234. }
  235. return json_success('', compact('rows', 'page', 'page_size', 'total'));
  236. }
  237. /**
  238. * Notes: 卡批次详情
  239. * User: ZhouBenXu
  240. * DateTime: 2024/6/28 上午10:50
  241. * @param $card_batch_id
  242. * @return Response
  243. */
  244. public static function getInfo($card_batch_id)
  245. {
  246. $cardBatch = CardBatch::where('card_batch_id', $card_batch_id)->first()->toArray();
  247. if (!empty($cardBatch)) {
  248. $cardBatch['card_batch_status'] = array_flip(CardBatch::CARD_BATCH_STATUS)[$cardBatch['card_batch_status']];
  249. !empty($cardBatch['card_batch_json']) && $cardBatch['card_batch_json'] = json_decode($cardBatch['card_batch_json'], true);
  250. !empty($cardBatch['card_batch_extend_json']) && $cardBatch['card_batch_extend_json'] = json_decode($cardBatch['card_batch_extend_json'], true);
  251. }
  252. return json_success('',$cardBatch);
  253. }
  254. }