hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'image_id'); } /** * 关联分享海报 * @return \think\model\relation\HasOne */ public function pic() { return $this->hasOne('app\\common\\model\\file\\UploadFile', 'file_id', 'pic_id'); } /** * 关联活动分类表 * @return \think\model\relation\BelongsTo */ public function category() { return $this->BelongsTo("app\\common\\model\\branch\\Category", 'category_id', 'category_id'); } /** * 关联连盟 * @return \think\model\relation\BelongsTo */ public function branch() { return $this->BelongsTo("app\\common\\model\\branch\\Branch", 'branch_id', 'branch_id'); } /** * 关联走访企业 * @return \think\model\relation\BelongsTo */ public function visitSupplier() { return $this->BelongsTo("app\\common\\model\\supplier\\Supplier", 'visit_supplier_id', 'shop_supplier_id'); } /** * 关联提供场地企业 * @return \think\model\relation\BelongsTo */ public function spaceSupplier() { return $this->BelongsTo("app\\common\\model\\supplier\\Supplier", 'space_supplier_id', 'shop_supplier_id'); } /** *报名时间 * @param $value * @param $data * @return string */ public function getRegisterTimeTextAttr($value, $data) { if(isset($data['register_start_time'])) { return $data['register_start_time'] ? date('Y-m-d H:i', $data['register_start_time']).' - ' . date('Y-m-d H:i', $data['register_end_time']) : ''; } return ''; } /** * 活动时间 * @param $value * @param $data * @return string */ public function getActivityTimeTextAttr($value, $data) { if(isset($data['activity_start_time'])) { return $data['activity_start_time'] ? date('Y-m-d H:i', $data['activity_start_time']).' - ' . date('Y-m-d H:i', $data['activity_end_time']) : ''; } return ''; } /** * 活动状态 * @param $value * @param $data * @return string */ public function getStatusTextAttr($value, $data) { if (!isset($data['activity_start_time'])) { return ''; } if (time() >= $data['activity_start_time'] && ($data['activity_end_time']) >= time()) { $status_text = "进行中"; $status = 1; } elseif (time() > ($data['activity_end_time'])){ $status_text = "已结束"; $status = 2; } else { $status_text = "未开始"; $status = 0; } if (time() >= $data['register_start_time'] && ($data['register_end_time']) >= time()) { $reg_status_text = "进行中"; $reg_status = 1; } elseif (time() > ($data['register_end_time'])) { $reg_status_text = "已结束"; $reg_status = 2; } else { $reg_status_text = "未开始"; $reg_status = 0; } $result = [ "status" => $status, "status_text" => $status_text, "reg_status" => $reg_status, "reg_status_text" => $reg_status_text, "act_start_time" => date("Y-m-d H:i", $data['activity_start_time']), "act_end_time" => date("Y-m-d H:i", $data['activity_end_time']), "reg_end_time" => date("Y-m-d H:i", $data['register_end_time']) ]; return $result; } /** * 活动详情 */ public static function detail($activity_id) { return (new static())->with(['image', 'category', 'branch', 'pic', 'visitSupplier', 'spaceSupplier'])->find($activity_id); } /** * 更新报名人数 */ public static function incTotal($activity_id, $inc = 1) { $res = self::detail($activity_id); if ($res) { $total = $res['total'] + $inc; // if ($res['limit_num'] && $res['limit_num'] < $total) { // $total = $res['limit_num']; // } $res->save(['total' => $total]); } } /** * 批量更新活动 * @param $activityIds * @param $data * @return bool */ public static function onBatchUpdate($activityIds, $data) { $model = new static(); return $model->where('activity_id', 'in', $activityIds)->save($data); } /** * 获取活动数据 (可指定某天) */ public function getActivityData($startDate = null, $endDate = null, $type, $branch_id = 0) { $model = $this; !is_null($startDate) && $model = $model->where('create_time', '>=', strtotime($startDate)); if (is_null($endDate)) { !is_null($startDate) && $model = $model->where('create_time', '<', strtotime($startDate) + 86400); } else { $model = $model->where('create_time', '<', strtotime($endDate) + 86400); } if ($branch_id > 0) { $model = $model->where('branch_id', '=', $branch_id); } $model = $model->where('is_delete', '=', 0); if ($type == 'activity_total') { return $model->count(); } return 0; } /** * 分会排行榜 */ public function getBranchRankList($date, $parent_branch_id, $limit = 7) { return $this->alias('A') ->join('branch B', 'A.branch_id=B.branch_id') ->field('count(A.activity_id) as total,B.name as branch_name') ->when($date, function ($query, $date) { getModelTime($query, $date, 'A.create_time'); }) ->where('B.parent_branch_id', '=', $parent_branch_id) ->where('A.is_delete', '=', 0) ->group('A.branch_id') ->order('total DESC') ->limit($limit) ->select(); } }