123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- /**
- * CardService.php
- * User: ZhouBenXu
- * Date: 2024/6/27
- * Time: 上午9:46
- * Notes: 卡管理
- */
- namespace app\admin\service\card;
- use support\Db;
- use support\exception\BusinessException;
- use app\model\Card;
- class CardService
- {
- /**
- * Notes: 添加卡信息
- * User: ZhouBenXu
- * DateTime: 2024/6/27 下午2:58
- * @param $join_card_batch_id 卡批次id
- * @param $card_info 卡信息
- * @return \support\Response
- */
- public static function insert($join_card_batch_id, $card_info)
- {
- Db::beginTransaction();
- try {
- $model = new Card();
- // 生成包含100到200的数组
- $card_sort = range($card_info['card_batch_serial_begin'], $card_info['card_batch_serial_end']);
- $insertData = [];
- if ($card_info['card_batch_validtimes'] != 0) { // 卡批次有效期 0=永久 天
- $card_info['card_batch_validtimes'] = (strtotime(date('Y-m-d 00:00:00')) + $card_info['card_batch_validtimes'] * 86400) - 1;
- }
- foreach ($card_sort as $value) {
- $info['join_card_batch_id'] = $join_card_batch_id;
- $info['join_card_category_id'] = $card_info['join_card_batch_category_id'];
- $info['card_status'] = $model::CARD_STATUS[1];
- $info['card_category'] = $card_info['card_batch_category'];
- $info['card_name'] = $card_info['card_batch_name'];
- $info['card_prefix'] = $card_info['card_batch_prefix'];
- $info['card_suffix'] = $card_info['card_batch_suffix'];
- $info['card_pass'] = mt_rand(10000000, 99999999);// 卡密
- $info['card_amount'] = $card_info['card_batch_amount'];
- $info['card_added'] = $card_info['card_batch_added'];
- $info['card_validtimes'] = $card_info['card_batch_validtimes']; // 卡批次有效期 0=永久 天
- $info['card_json'] = "{}";
- $info['card_assign_json'] = "{}";
- $info['card_process_json'] = "{}";
- $info['card_sort'] = $value;
- $info['card_remark'] = "";
- $info['card_extend_json'] = "{}";
- $info['card_addtimes'] = time();
- $insertData[] = $info;
- }
- $model->insert($insertData);
- Db::commit();
- } catch (\PDOException $e) {
- Db::rollBack();
- dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
- return json_fail('数据写入失败~');
- } catch (BusinessException $e) {
- Db::rollBack();
- dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getTrace());
- return json_fail('数据写入失败~');
- }
- _syslog("添加储值卡", "卡批次名称【" . $card_info['card_batch_name'] . "】", $insertData);
- return json_success('success');
- }
- /**
- * Notes: 根据卡批次修改卡信息
- * User: ZhouBenXu
- * DateTime: 2024/6/27 下午5:03
- * @param $join_card_batch_id
- * @param $save
- * @return \support\Response
- */
- public static function saveCardByCardBatchId($join_card_batch_id, $save)
- {
- Db::beginTransaction();
- try {
- $model = new Card();
- $data['card_name'] = $save['card_batch_name'];
- $card_status = array_values($model::CARD_STATUS);
- if (in_array($save['card_batch_status'], $card_status)) {
- $data['card_status'] = $save['card_batch_status']; // 卡批次状态
- }
- if ($save['validtimes_status'] == 0) { // 卡批次有效期 0 永久天 1 增加 2减少
- $data['card_validtimes'] = 0;
- } elseif ($save['validtimes_status'] == 1) {
- $data['card_validtimes'] = Db::raw('card_validtimes+' . $save['card_batch_validtimes'] * 86400);
- } elseif ($save['validtimes_status'] == 2) {
- $data['card_validtimes'] = Db::raw('card_validtimes-' . $save['card_batch_validtimes'] * 86400);
- }
- $model->where('join_card_batch_id', $join_card_batch_id)->update($data);
- Db::commit();
- } catch (\PDOException $e) {
- Db::rollBack();
- dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
- return json_fail('数据写入失败~');
- } catch (BusinessException $e) {
- Db::rollBack();
- dump($e->getFile() . '(' . $e->getLine() . '):' . $e->getMessage());
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- Db::rollBack();
- dump($e->getTrace());
- return json_fail('数据写入失败~');
- }
- _syslog("修改储值卡", "卡批次名称【" . $save['card_batch_name'] . "】");
- return json_success('success');
- }
- }
|