1])->max('card_no'); if(empty($gift_card_max_no)) { $start_no = 100000001; }else{ $start_no = $gift_card_max_no+1; } $end_no = $start_no + $num -1; $nowtime = time(); $todaytime = date('Y-m-d',$nowtime); $where = []; $where[]=['create_time','>',strtotime($todaytime)]; $card_info = GiftCard::where($where)->order('create_time desc')->find(); if($card_info){ $date = $card_info['date'] +1; }else{ $date =date('Ymd',$nowtime).'00001'; } //添加批次信息 $card_data = [ 'date' =>$date, 'type' =>1, 'card_remark' => $start_no.'~'.$end_no, 'card_money' => $money, 'total_num' => $num, 'create_time' => time(), ]; $gift_card = GiftCard::create($card_data); if(!$gift_card){ Db::rollback(); self::setError('插入批次信息错误!'); } $gc_id = $gift_card['id']; $gcidata = []; $saveData = []; $saveData['gc_id'] = $gc_id; $saveData['type'] = 1; $saveData['card_money'] = $money; $saveData['is_used'] = 0; $saveData['create_time'] = time(); for($i = 1;$i<=$num;$i++){ $saveData['card_no'] = $start_no; $pass = gift_card_pass(); $saveData['card_pass'] = $pass; // // 生成小程序二维码并上传到七牛云 $qrCodePath = GiftCardQrCodeService::generateAndUploadQrCode($start_no, $pass); $saveData['qr_code_path'] = $qrCodePath ?: ''; $start_no ++; $gcidata[]=$saveData; } if(!empty($gcidata)){ $gCobj = new GiftCardInfo(); $card_add_info = $gCobj->saveAll($gcidata); if(!$card_add_info){ Db::rollback(); self::setError('插入礼品卡信息错误,请重试!'); } } // 提交事务 Db::commit(); return true; } catch (\Exception $e) { // 回滚事务 Db::rollback(); return $e->getMessage(); } } /** * @notes 删除批次信息 * @param $params * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author ljj * @date 2022/3/31 2:37 下午 */ public function deleteGiftCard($params) { Db::startTrans(); try { // $res = GiftCard::destroy($params['id']); $res = GiftCard::where(['id'=>$params['id']])->update(['delete_time'=>time()]); // 提交事务 if(!$res){ Db::rollback(); self::setError('删除批次信息错误!'); } $list = GiftCardInfo::where(['gc_id'=>$params['id'],'is_used'=>0])->findOrEmpty(); if(!$list->isEmpty()){ $res2 = GiftCardInfo::where(['gc_id'=>$params['id'],'is_used'=>0])->update(['delete_time'=>time()]); if(!$res2){ Db::rollback(); self::setError('删除礼品卡信息错误!'); } } // 提交事务 Db::commit(); return true; } catch (\Exception $e) { // 回滚事务 Db::rollback(); return $e->getMessage(); } } /** * @notes 删除礼品卡信息 * @param $params * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author ljj * @date 2022/3/31 2:37 下午 */ public function deleteGiftCardInfo($params) { Db::startTrans(); try { $res = GiftCardInfo::destroy($params['id']); if(!$res){ Db::rollback(); self::setError('删除礼品卡信息错误!'); } // 提交事务 Db::commit(); return true; } catch (\Exception $e) { // 回滚事务 Db::rollback(); return $e->getMessage(); } } }