123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\Redpacket\ActiveReward as Request;
- use App\Models\Custom;
- use App\Models\CustomAmount;
- use App\Models\Redpacket\ActiveRecord as Model;
- use App\Models\Redpacket\Active as RedpacketActive;
- use Illuminate\Support\Facades\DB;
- /**
- * 抽奖记录
- *
- * @author 刘相欣
- *
- */
- class RedpacketActiveRecord extends Auth{
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','红包活动');
- $this->assign('breadcrumb2','参与记录');
- }
- /**
- * 列表页
- *
- * */
- public function index(Model $Model,RedpacketActive $RedpacketActive,Custom $Custom){
- // 接受参数
- $activeId = request('active_id',0);
- $customCode = request('custom_code','');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- $status = request('status');
- // 查询条件
- $map = [['redpacket_active_record.reward_id','>',0]];
- // 编码转ID
- $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
- if( $customUid ) $map[] = ['redpacket_active_record.custom_uid','=',$customUid];
- if( $activeId ) $map[] = ['redpacket_active_record.active_id','=',$activeId];
- if( $startTime ) $map[] = ['redpacket_active_record.insert_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['redpacket_active_record.insert_time','<=',strtotime($endTime)];
- if( !is_null($status) ) $map[] = ['redpacket_active_record.status','=',$status];
- // 查询数据
- $list = $Model->query()
- ->join('redpacket_active','redpacket_active.id','=','redpacket_active_record.active_id')
- ->join('custom','custom.uid','=','redpacket_active_record.custom_uid')
- ->where($map)
- ->select(['redpacket_active_record.*','custom.weiban_extid','custom.username','redpacket_active.name as active_name'])
- ->orderByDesc('id')
- ->paginate(config('page_num',10))->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 组合数据
- $value['state'] = $Model->getRecordState($value['status'],'name');
- // id转编号
- $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
- // 重组
- $list[$key] = $value;
- }
- // 获取列表
- $statusList = $Model->getRecordStateList();
- // 获取列表
- $activeList = $RedpacketActive->query()->get(['id','name'])->toArray();
- // 分配数据
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $this->assign('list',$list);
- $this->assign('statusList',$statusList);
- $this->assign('activeList',$activeList);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 修改状态
- *
- * */
- public function set_status(Request $request,Model $Model,CustomAmount $CustomAmount ){
- // 验证参数
- $request->scene('set_status')->validate();
- // 设置状态
- $id = request('id',0);
- $status = request('status',0);
- // 查询用户
- $oldData = $Model->where(['id'=>$id])->first();
- // 如果用户不存在
- if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
- // 开启事务
- DB::beginTransaction();
- try {
- // 如果设置成已完成
- if( $status == 8 ){
- // 发放红包
- $CustomAmount->trade($oldData['custom_uid'],$oldData['active_id'],$oldData['money'],8,1,'红包活动');
- // 发放失败,改为未中奖
- if( isset($result['error']) ) {
- // 事务回滚
- DB::rollBack();
- // 提示失败
- return json_send(['code'=>'error','msg'=>'发放失败,请重试']);
- }
- }
- // 执行修改
- $result = $Model->edit($id,['status'=>$status]);
- // 提示新增失败
- if( !$result ) {
- // 事务回滚
- DB::rollBack();
- // 提示失败
- return json_send(['code'=>'error','msg'=>'状态设置失败,请重试']);
- }
- // 提交事务
- DB::commit();
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'发放成功','path'=>'']);
- } catch (\Throwable $th) {
- // 事务回滚
- DB::rollBack();
- // 提示失败
- return json_send(['code'=>'error','msg'=>'发放失败,请重试']);
- }
- }
- /**
- * 导出表格
- *
- * */
- public function down_excel(Model $Model,Custom $Custom){
- // 接受参数
- $activeId = request('active_id',0);
- $customCode = request('custom_code','');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- $status = request('status');
- // 查询条件
- $map = [['redpacket_active_record.reward_id','>',0]];
- // 编码转ID
- $customUid = $customCode ? $Custom->codeToId($customCode) : 0;
- if( $customUid ) $map[] = ['redpacket_active_record.custom_uid','=',$customUid];
- if( $activeId ) $map[] = ['redpacket_active_record.active_id','=',$activeId];
- if( $startTime ) $map[] = ['redpacket_active_record.insert_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['redpacket_active_record.insert_time','<=',strtotime($endTime)];
- if( !is_null($status) ) $map[] = ['redpacket_active_record.status','=',$status];
- // 查询数据
- $list = $Model->query()
- ->join('redpacket_active','redpacket_active.id','=','redpacket_active_record.active_id')
- ->join('custom','custom.uid','=','redpacket_active_record.custom_uid')
- ->where($map)
- ->select([
- 'redpacket_active_record.id',
- 'redpacket_active.id as active_id',
- 'redpacket_active.name as active_name',
- 'custom.uid as custom_uid',
- 'custom.username',
- 'redpacket_active_record.money',
- 'redpacket_active_record.status',
- 'redpacket_active_record.insert_time',
- 'custom.weiban_extid',
- ])
- ->orderByDesc('id')->get()->toArray();
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 组合数据
- $value['status'] = $Model->getRecordState($value['status'],'name');
- $value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
- $value['username'] = hide_phone($value['username']);
- $value['insert_time'] = date('Y-m-d H:i:s',$value['insert_time']);
- // 重组
- $list[$key] = $value;
- }
- // 去下载
- $this->toDown($list);
- }
- /**
- * 去下载
- */
- private function toDown($data){
- // xlsx文件保存路径
- $excel = new \Vtiful\Kernel\Excel(['path' =>public_path().'/uploads/']);
- $filePath = $excel->fileName(uniqid().'.xlsx', 'sheet1')->header(['记录ID','活动ID','活动名称','客户编码','客户昵称','红包金额','发放状态','中奖时间','微伴ID'])->data($data)->output();
- header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- header('Content-Disposition: attachment;filename="红包活动中奖记录'.date('Y-m-d His').'.xlsx"');
- header('Content-Length: ' . filesize($filePath));
- header('Content-Transfer-Encoding: binary');
- header('Cache-Control: must-revalidate');
- header('Cache-Control: max-age=0');
- header('Pragma: public');
- ob_clean();
- flush();
- // 输出文件,成功删除文件路径
- if ( copy($filePath, 'php://output') ) @unlink($filePath);
- }
- }
|