find($this->image_up_id)->ali_url??''; // $b = UploadFile::field('file_id,file_type,storage,domain,file_path')->find($this->image_down_id)->ali_url??''; // return [$a,$b]; // } // public function imageshare(){ // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_share_id'); // } // public function imageup(){ // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_up_id'); // } // public function imagedown(){ // return $this->hasOne('app\common\model\UploadFile', 'file_id', 'image_down_id'); // } // public static function getCurrent(){ // $now = Date("Y-m-d H:i:s",time()); // // $filter['audit_status_zg'] = 10; // $filter['is_up'] = 1; // $one = ZaActivity::where('start_time','<',$now)->where('end_time','>',$now)->where($filter)->find(); // if($one){ // return $one; // }else{ // $one = ZaActivity::find(); // return $one; // } // return false; // } /** * 一对多关联商品表 */ public function goods(){ return $this->hasMany('ZaActivityGood', 'za_activity_id') // ->order('is_end','asc') // ->order('start_time', 'asc') ; } public function goodsInfos(){ return $this->hasMany('ZaActivityGood', 'za_activity_id') // ->order('is_end','asc') // ->order('start_time', 'asc') ; } // /** // * 一对多关联商品表 // */ // public function imagelist(){ // return $this->hasMany('MsActivityImage', 'ms_activity_id')->order('id', 'desc'); // } /** * 作废 * @param array $data * @return bool */ public function down(array $data): bool { $this->transaction(function () use ($data) { $id = (int)$this['id']; $this->save($data); }); return true; } // //获取秒杀价 // public function getMsPrice($goods_id,$goods_price_min){ // $now = Date("Y-m-d H:i:s",time()); // $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')]; // $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')]; // $filter[] = ['activity.is_up','=',1]; // $filter[] = ['activity.audit_status','=',1]; // $filter[] = ['ms_activity_goods.goods_id','=',$goods_id]; // $filter[] = ['ms_activity_goods.end_time','>',$now]; // $goods = $this->field('ms_activity_goods.*')->alias('activity')->leftjoin('ms_activity_goods','ms_activity_goods.ms_activity_id=activity.id')->where($filter)->find(); // $type = 0; // if($goods){ // $now = date("Y-m-d H:i:s",time()); // if ($now < $goods->start_time) { // $d = Date("d",time()); // // 2022-01-01 15:00:00; // if($d==substr($goods->start_time,8,2)){ // $type = 1; // $text = substr($goods->start_time,11,5).'开始秒杀'; // }else{ // $type = 2; // $text = Date('Y-m-d H:i',strtotime($goods->start_time)).'开始秒杀'; // } // } // if ($now > $goods->end_time) { // $type = 3; // $text = '秒杀结束'; // } // if($now>$goods->start_time&&$now<$goods->end_time){ // $type = 4; // $text = '限时秒杀'; // } // $data['ms_price'] = $goods->ms_price; //秒杀价 // $data['type'] = $type; //类型 1 当天开始秒杀,2 某天某时开始秒杀 4秒杀中 // $data['text'] = $text; //文字 // $data['dif_end_time'] = strtotime($goods->end_time) - time(); // $data['limit_mount'] = $goods->limit_mount; // $data['stock_num'] = $goods->stock_num; // $data['ms_id'] = $goods->id; // $data['start_time'] = substr($goods->start_time,0,16); // $data['zj_price'] = helper::bcsub($goods_price_min - $goods->ms_price,0,2); // $data['ms_activity_id'] = $goods->ms_activity_id; // return $data; // } // return null ; // } /** * 获取列表 * @param array $param * @return \think\Paginator * @throws \think\db\exception\DbException */ public function getList($param = []) { // 检索查询砍价列表 $filter = $this->getFilter($param); // 排序条件 // $sort = $this->setQuerySort($param); $sort = ['activity.id'=>'desc']; // 查询列表数据 $param['status'] = $param['status']??-1; // $list = $this->field('activity.*') ->alias('activity') ->leftjoin('za_activity_goods','za_activity_goods.za_activity_id=activity.id') ->where($filter) ->with(['goods']) ->group('activity.id') ->order($sort) ->paginate(15) ->each(function($item) { // 查询下单人数 $orderCount = ZaActivityRelationModel::where('za_activity_id', $item['id']) ->where('is_pay', "=", "1")->count(); $item['order_count'] = $orderCount; // 查询领取人数 $getCount = ZaActivityRelationModel::where('za_activity_id', $item['id']) ->where('is_pay', "=", "1")->where('receive_state', "=", "1")->count(); $item['get_count'] = $getCount; // 库存 $stock_num = 0; foreach($item['goods'] as $k){ $stock_num += $k['stock_num']; } $item['stock_num'] = $stock_num; }); return $list ; } /** * 检索查询条件 * @param array $param * @return array */ private function getFilter($param = []) { // 默认查询条件 $params = $this->setQueryDefaultValue($param, ['name'=>'','audit_status'=>-1,'status'=>-1]); // 检索查询条件 $filter = []; if(isset($params['audit_status']) && $params['audit_status']>-1){ $filter[] = ['audit_status', '=', $params['audit_status']]; } if(isset($params['name']) && !empty($params['name'])){ $filter[] = ['activity.name', 'like','%'.$params['name'].'%']; } if (!empty($params['betweenTime'])) { $times = between_time_format($params['betweenTime']); $filter[] = ['activity.start_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])]; $filter[] = ['activity.end_time', '<', Date('Y-m-d H:i:s',$times['end_time'])]; } //活动状态 if(isset($params['status'])&&$params['status']>-1){ switch ($params['status']){ case 1://进行中 $filter[] = ['activity.start_time','<=',date('Y-m-d H:i:s')]; $filter[] = ['activity.end_time','>=',date('Y-m-d H:i:s')]; break; case 2://未开始 $filter[] = ['activity.start_time','>',date('Y-m-d H:i:s')]; break; case 3://已结束 $filter[] = ['activity.end_time','<',date('Y-m-d H:i:s')]; break; } } return $filter; } //活动状态 即将开始或已结束 public function getStatusAttr() { $now = date("Y-m-d H:i:s",time()); if($this->is_up==0){ return 4; } if ($now < $this->start_time) { return 2; } if ($now > $this->end_time) { return 3; } if($now>$this->start_time&&$now<$this->end_time){ return 1; } return 0; } /** * 审核 * @param array $data * @return bool */ public function audit(array $data): bool { $this->transaction(function () use ($data) { $user = StoreUserService::getLoginInfo(); $real_name = $user['user']['real_name']??''; $id = (int)$this['id']; $this->save($data); $id = (int)$this['id']; if($data['audit_status']==1){ (new CommonHis())->add(4, $id, $real_name,'同意并发布活动'); } if($data['audit_status']==2){ $remark = '不同意'." ".$data['audit_reason']; (new CommonHis())->add(4, $id, $real_name,$remark); } }); return true; } /** * 详情 * @param int $id * @return null|static */ public static function detail(int $id,array $with=[]) { return self::get($id,$with); } /** * 添加 * @param $data * @return false|int */ public function addOne($data) { $times = between_time_format($data['between_time']); $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']); $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']); isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null; isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null; $this->transaction(function () use ($data) { if(isset($data['goods'])){ $this->save($data); $goods = new ZaActivityGoodModel; $flag = $goods->add($this->id,$data['goods']); if(!$flag){ $this->error = $goods->error; Db::rollback(); return false; } } else { $this->error = '请添加活动商品'; return false; } $user = StoreUserService::getLoginInfo(); $real_name = $user['user']['real_name']??''; $id = (int)$this['id']; (new CommonHis())->add(4, $id, $real_name,'提交审核'); }); return true; } /** * 编辑 * @param $data * @return bool|int */ public function edit($data) { $times = between_time_format($data['between_time']); $data['start_time'] = Date('Y-m-d H:i:s',$times['start_time']); $data['end_time'] = Date('Y-m-d H:i:s',$times['end_time']); isset($data['start_time']) && empty($data['start_time']) && $data['start_time'] = null; isset($data['end_time']) && empty($data['end_time']) && $data['end_time'] = null; $this->transaction(function () use ($data) { $this->save($data); if(isset($data['goods'])){ $goods = new ZaActivityGoodModel; $flag = $goods->add($this->id,$data['goods']); if(!$flag){ $this->error = $goods->error; Db::rollback(); return false; } } $user = StoreUserService::getLoginInfo(); $real_name = $user['user']['real_name']??''; $id = (int)$this['id']; (new CommonHis())->add(4, $id, $real_name,'再次编辑'); }); return true; } /** * 关联记录 * @return \think\model\relation\BelongsTo */ public function his() { return $this->hasMany('app\common\model\CommonHis','target_id','id')->where('target_type',4)->order('id', 'desc'); } // /** // * 删除 // * @param $id // * @return bool|int // */ // public function remove() // { // return $this->delete(); // } public function orderSt(int $id) { $data = []; // 查询订单数 $orderCount = ZaActivityRelationModel::where('za_activity_id', $id) ->count(); $data['st']['order_count'] = $orderCount; // 查询下单人数 $payCount = ZaActivityRelationModel::where('za_activity_id', $id) ->where('is_pay', "=", "1")->count(); $data['st']['pay_count'] = $payCount; // 查询领取人数 $getCount = ZaActivityRelationModel::where('za_activity_id', $item['id']) ->where('is_pay', "=", "1") ->where('receive_state', "=", "1")->count(); $data['st']['get_count'] = $getCount; $data['st']['total_price'] = '0.00'; $list = ZaActivityRelationModel::where('za_activity_id', $id); $data['list'] = $list; return $data; } }