DiverseYouthNightSchool/application/common/model/school/classes/ClassesLib.php

1820 lines
60 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\model\school\classes;
use app\admin\model\Admin;
use app\common\library\Virtual;
use app\common\model\BaseModel;
use app\common\model\dyqc\ManystoreShop;
use app\common\model\manystore\Shop;
use app\common\model\manystore\UserAuth;
use app\common\model\school\Area;
use app\common\model\school\classes\lib\Spec;
use app\common\model\User;
use app\manystore\model\Manystore;
use think\Model;
use traits\model\SoftDelete;
class ClassesLib extends BaseModel
{
use SoftDelete;
// 表名
protected $name = 'school_classes_lib';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $classes_lib_ids = [];
// 追加属性
protected $append = [
'add_type_text',
'type_text',
'address_type_text',
'status_text',
'auth_status_text',
'auth_time_text',
'recommend_text',
'hot_text',
'new_text',
'selfhot_text',
'distance_text',
'start_time_text',
'end_time_text',
'has_expire',
];
public function getHasExpireList()
{
return ['1' => __('Has_expire 1'), '2' => __('Has_expire 2')];
}
public function getHasExpireAttr($value, $data)
{
$end_time = (isset($data['end_time']) ? $data['end_time'] : '');
if(!$end_time) return '2';
if( $end_time < time()) {
return '1';
}else{
return '2';
}
}
public function getStartTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getEndTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setStartTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
protected function setEndTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
public function getDistanceTextAttr($value, $data) {
$distance_text = '';
$distance = $data['distance'] ?? 0;
switch (true) {
case $distance >= 1000;
$distance_text = round(($distance / 1000), 2) . 'km';
break;
default :
$distance_text = $distance . 'm';
break;
}
return $distance_text;
}
protected static function init()
{
self::afterInsert(function ($row) {
if (!empty($row['weigh'])) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
}
});
}
public function getAddTypeList()
{
return ['1' => __('Add_type 1'), '2' => __('Add_type 2')];
}
public function getTypeList()
{
return ['out' => __('Type out'), 'in' => __('Type in')];
}
public function getAddressTypeList()
{
return ['1' => __('Address_type 1'), '2' => __('Address_type 2')];
}
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
}
public function getAuthStatusList()
{
return ['0' => __('Auth_status 0'), '1' => __('Auth_status 1'), '2' => __('Auth_status 2')];
}
public function getRecommendList()
{
return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
}
public function getHotList()
{
return ['0' => __('Hot 0'), '1' => __('Hot 1')];
}
public function getNewList()
{
return ['0' => __('New 0'), '1' => __('New 1')];
}
public function getSelfhotList()
{
return ['0' => __('Selfhot 0'), '1' => __('Selfhot 1')];
}
public function getAddTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['add_type']) ? $data['add_type'] : '');
$list = $this->getAddTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
$list = $this->getTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getAddressTypeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['address_type']) ? $data['address_type'] : '');
$list = $this->getAddressTypeList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getAuthStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['auth_status']) ? $data['auth_status'] : '');
$list = $this->getAuthStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getAuthTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['auth_time']) ? $data['auth_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getRecommendTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['recommend']) ? $data['recommend'] : '');
$list = $this->getRecommendList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getHotTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
$list = $this->getHotList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getNewTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['new']) ? $data['new'] : '');
$list = $this->getNewList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSelfhotTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['selfhot']) ? $data['selfhot'] : '');
$list = $this->getSelfhotList();
return isset($list[$value]) ? $list[$value] : '';
}
protected function setAuthTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
public function getHeadimageAttr($value, $data)
{
if (!empty($value)) return cdnurl($value, true);
}
public function getImagesAttr($value, $data)
{
$imagesArray = [];
if (!empty($value)) {
$imagesArray = explode(',', $value);
foreach ($imagesArray as &$v) {
$v = cdnurl($v, true);
}
return $imagesArray;
}
return $imagesArray;
}
public function setImagesAttr($value, $data)
{
$imagesArray = $value;
if (!empty($value) && is_array($value)) {
//转成逗号拼接字符串
$imagesArray = implode(',', $value);
}
return $imagesArray;
}
public function manystore()
{
return $this->belongsTo('app\admin\model\Manystore', 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function shop()
{
return $this->belongsTo(ManystoreShop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function user()
{
return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function admin()
{
return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function teacher()
{
return $this->belongsTo('app\common\model\school\classes\Teacher', 'teacher_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function specs()
{
return $this->hasMany(ClassesSpec::class,'classes_lib_id')->order("weigh desc,id desc");
}
// public function collect()
// {
// return $this->hasMany(Collect::class,'classes_lib_id');
// }
//
/** 课程详情
* @param $id
* @return ClassesLib|null
* @throws \think\exception\DbException
*/
public function detail($id,$user_id=0,$oper_type='user',$trans=false){
$self = $this->get($id,['shop','teacher','user','specs']);
//是否收藏
$self['is_collect'] = 0;
//是否购买
$self['have_buy'] = 0;
if($user_id){
//判断是否收藏
$self['is_collect'] = Collect::where("user_id",$user_id)->where("classes_lib_id",$id)->count() ? 1:0 ;
//判断用户是否已报名
$self['have_buy'] = \app\common\model\school\classes\order\Order::where("classes_lib_id",$id)
->where("user_id",$user_id)
->where("status","in",["3","6","9"])
->count() ? 1 : 0 ;
//$self['virtual_people'] = $self['virtual_people'] + 1;
//有用户进来,参与人数+1
if(!$self['have_buy'])$self['virtual_people'] = $self['virtual_people'] + 1;
}
$cate_list = Cate::where("status",'1')->column("name","id");
$label_list = Label::where("status",'1')->column("name","id");
$type_list = Label::where("status",'1')->column("name","id");
$classes_label_ids = $self['classes_label_ids'];
$classes_cate_ids = $self['classes_cate_ids'];
$classes_type_ids = $self['classes_type'];
$classes_label = $classes_cate = $classes_type = [];
foreach (explode(",", $classes_label_ids) as $classes_label_id){
if(isset($label_list[$classes_label_id]))$classes_label[] = $label_list[$classes_label_id];
}
foreach (explode(",", $classes_cate_ids) as $classes_cate_id){
if(isset($cate_list[$classes_cate_id]))$classes_cate[] = $cate_list[$classes_cate_id];
}
foreach (explode(",", $classes_type_ids) as $classes_type_id){
if(isset($type_list[$classes_type_id]))$classes_type[] = $type_list[$classes_type_id];
}
$self['classes_label'] = $classes_label;
$self['classes_cate'] = $classes_cate;
$self['classes_type_name'] = $classes_type;
//下架判断
// if($self['status'] != '1'){
// $this->error("该课程已下架");
// }
//
$this->setViews($id,$user_id,$oper_type,$user_id,$trans);
$self->getRelation('user')->visible(['nickname','realname','mobile','avatar']);
//参与人数 = 虚拟人数 + 平台人数
return $self;
}
/**设置浏览量
* @param $id
* @param int $user_id
* @param bool $check
* @param bool $trans
* @throws \Exception
*/
public function setViews($id,$user_id,$oper_type='user',$oper_id=0,$trans=false){
$classes_lib = self::where("id",$id)->find();
if(!$classes_lib)throw new \Exception("找不到课程!");
//判断逻辑
if($trans){
self::beginTrans();
}
try{
//事务逻辑
$classes_lib->views = $classes_lib->views + 1;
//查询是否已收藏
$classes_lib->save();
//调用事件
$data = ['classes' => $classes_lib,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id];
\think\Hook::listen('classes_view_after', $data);
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage());
}
return $classes_lib;
}
/**课程参与人员信息
* @param $id
*
*/
public function virtualParticipants($id,$user_id=0,$have_real=false){
//虚拟用户生成数量
$v_num = 10;
$unpaid_user_data = $paid_user_data = [];
$selfuser = null;
if($user_id){
$selfuser = User::get($user_id);
}
$user_unpaid_order = $user_paid_order =null;
if($selfuser){
$user_unpaid_order = \app\common\model\school\classes\order\Order::where("classes_lib_id",$id)
->where("status","in",["0",'-3'])
->where("user_id",$user_id)
->order("createtime desc")
->value("createtime");
$user_paid_order = \app\common\model\school\classes\order\Order::where("classes_lib_id",$id)
->where("status","in",["3",'6','9'])
->where("user_id",$user_id)
->order("createtime desc")
->value("createtime");
}
//参与者头像
//先从订单里取出最新的10个未支付的用户不足的用虚拟用户填充
$user_ids = \app\common\model\school\classes\order\Order::where("classes_lib_id",$id)->where("status","in",["0",'-3'])->order("createtime desc")->limit($v_num)->column("createtime","user_id");
if($user_ids){
$users = User::where("id","in",array_keys($user_ids))->select();
foreach ($users as $user){
$unpaid_user_data[] = [
"nickname"=>$user['nickname'],
"head_image"=>$user['avatar'],
"classes_lib_id"=>$id,
"time"=>$user_ids[$user['id']],
"jointype"=>'1',
"havetype"=>'0'
];
}
}
if(!$user_paid_order && $selfuser){
if($user_unpaid_order){
$unpaid_user_data[] = [
"nickname"=>$selfuser['nickname'],
"head_image"=>$selfuser['avatar'],
"classes_lib_id"=>$id,
"time"=>$user_unpaid_order,
"jointype"=>'1',
"havetype"=>'0'
];
$user_ids[$selfuser['id']] = $user_unpaid_order;
}else{
$unpaid_user_data[] = [
"nickname"=>$selfuser['nickname'],
"head_image"=>$selfuser['avatar'],
"classes_lib_id"=>$id,
"time"=>time(),
"jointype"=>'1',
"havetype"=>'0'
];
$user_ids[$selfuser['id']] = time();
}
}
//计算需要生成的虚拟用户数量
$v_num = $v_num - count($user_ids);
if($v_num<0)$v_num =0;
if($v_num && !$have_real){
$unpaid_user = VirtualUser::where("classes_lib_id",$id)->where("jointype",'1')->order("time desc")->limit($v_num)->select();
$unpaid_user_data = array_merge($unpaid_user_data,$unpaid_user);
}
//再从订单里取出最新的10个使用中已完成 和 已退款 的用户,不足的用虚拟用户填充
$user_ids = \app\common\model\school\classes\order\Order::where("classes_lib_id",$id)->where("status","in",["3",'6','9'])->order("createtime desc")->limit($v_num)->column("createtime","user_id");
if($user_ids){
$users = User::where("id","in",array_keys($user_ids))->select();
foreach ($users as $user){
$paid_user_data[] = [
"nickname"=>$user['nickname'],
"head_image"=>$user['avatar'],
"classes_lib_id"=>$id,
"time"=>$user_ids[$user['id']],
"jointype"=>'0',
"havetype"=>'1'
];
}
}
//计算需要生成的虚拟用户数量
$v_num = 10;
$v_num = $v_num - count($user_ids);
if($v_num<0)$v_num =0;
if($v_num && !$have_real){
$unpaid_user = VirtualUser::where("classes_lib_id",$id)->where("havetype",'1')->order("time desc")->limit($v_num)->select();
$paid_user_data = array_merge($paid_user_data,$unpaid_user);
}
//进行昵称加*和按时间重新排序
array_multisort(array_column($unpaid_user_data, 'time'), SORT_ASC, $unpaid_user_data);
array_multisort(array_column($paid_user_data, 'time'), SORT_ASC, $paid_user_data);
foreach ($unpaid_user_data as &$userss){
//substr_replace($username, '****', 3, 4);
$userss["nickname"] = $this->nickname_filter($userss["nickname"]);
}
foreach ($paid_user_data as &$usersss){
//substr_replace($username, '****', 3, 4);
$usersss["nickname"] = $this->nickname_filter($usersss["nickname"]);
}
return compact("unpaid_user_data","paid_user_data");
}
public function nickname_filter($nickname){
$nickname_len = mb_strlen($nickname);
if($nickname_len<=2){
return $nickname;
}else{
return mb_substr($nickname,0,1).str_repeat("*",$nickname_len-2).mb_substr($nickname,-1);
}
}
/**课程规格
* @param $id
*
*/
public function spec($id,$user_id=0){
//课程规格
$spec_data = ClassesSpec::where("classes_lib_id",$id)->where("status",'1')->order("weigh desc")->select();
foreach ($spec_data as &$spec){
//默认未下单预约
$spec["user_reservation"] = null;
if($user_id){
$spec["user_reservation"] = \app\common\model\school\classes\hourorder\Order::where("classes_lib_id",$id)->where("classes_lib_spec_id",$spec['id'])->where("user_id",$user_id)->where("status","in",["0",'-1'])->find();
}
}
return $spec_data;
}
/** 管理的课程ids
* @param $user_id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getClassesAuthIds($user_id){
//必须是授权过的用户
if(config("site.shop_auth_user_check")){
$auth_shop_ids = UserAuth::where("status" ,1)->where("user_id",$user_id)->column("shop_id");
$shop_where = ["shop_id" ,"in" ,$auth_shop_ids];
}else{
$shop_where = [[]];
}
//核销员有核销全部课程权力
$classes_lib_ids = [];
$shop_id = Verification::where("user_id",$user_id)->where(...$shop_where)->where("status",'1')->value("shop_id");
if($shop_id){
$classes_libids = ClassesLib::where("shop_id",$shop_id)->column("id");
if($classes_libids)$classes_lib_ids[] = implode(",", $classes_libids);
}
if($classes_lib_ids){
//遍历课程ids
foreach ($classes_lib_ids as $classes_lib_id){
$classes_lib_id_arr = explode(",",$classes_lib_id)?:[];
foreach ($classes_lib_id_arr as $v){
$this->classes_lib_ids[] = $v;
}
}
}
//判断是否有老师身份
$teacher = Teacher::where("user_id",$user_id)->where(...$shop_where)->find();
if($teacher){
$lib_ids = ClassesLib::where("teacher_id",$teacher['id'])->column('id');
$this->classes_lib_ids = array_merge($this->classes_lib_ids,$lib_ids);
}
$this->classes_lib_ids = array_unique($this->classes_lib_ids);
return $this->classes_lib_ids;
}
/**
* 获取所有课程列表
*/
public static function getVaildList($params) {
extract($params);
$a = (new self)->getWithAlisaName().'.';
$with = ['teacher'];
if (isset($has_shop) && $has_shop) {
$with[] = 'shop';
}
$cate_list = Cate::where("status",'1')->column("name","id");
$label_list = Label::where("status",'1')->column("name","id");
$type_list = Type::where("status",'1')->column("name","id");
$selfetch = self::with($with);
// 查询自提点
if($status == "-2"){
$selfetch = $selfetch->where($a.'status', '1');
}
if($auth_status == "-2"){
$selfetch = $selfetch->where("{$a}auth_status",1);
}
$order = $order?? 'normal';
$per_page = $limit ?? 10;
$field = "{$a}id,{$a}shop_id,{$a}user_id,{$a}teacher_id,{$a}classes_cate_ids,{$a}classes_label_ids,{$a}self_label_tag,{$a}title,{$a}headimage,{$a}type,{$a}classes_date_text,{$a}classes_time_text,{$a}virtual_num,{$a}sale,{$a}price,{$a}underline_price,{$a}virtual_collect,{$a}status,{$a}auth_status,{$a}weigh,{$a}recommend,{$a}hot,{$a}new,{$a}selfhot,{$a}createtime,{$a}virtual_people,{$a}feel,{$a}limit_num,{$a}sign_num,{$a}verification_num";
//得到距离
if (isset($latitude) && isset($longitude) && $latitude && $longitude) {
$field .= ', '.getDistanceBuilder($latitude, $longitude);
}else{
$field .= ', 0 as distance';
}
//得到每个
$selfetch = $selfetch->field($field);
if (isset($keywords) && $keywords) {
$selfetch = $selfetch->where("{$a}title|{$a}address|{$a}address_detail|{$a}address_city", 'like', '%' . $keywords . '%');
}
if(isset($start_time) && isset($end_time) && ($end_time || $start_time)) {
if($start_time){
$selfetch = $selfetch->where("{$a}start_time", '>=', $start_time);
}
if($end_time){
$selfetch = $selfetch->where("{$a}end_time", '<=', $end_time);
}
}
// is_expire 是否查过期1只查过期,2只查不过期,0全查
if(isset($is_expire) && $is_expire) {
if($is_expire == 1){
$selfetch = $selfetch->where("{$a}end_time", '<', time());
}else{
$selfetch = $selfetch->where("{$a}end_time", '>=', time());
}
}
if (isset($manystore_id) && $manystore_id) {
$selfetch = $selfetch->where("{$a}manystore_id", 'in', ''.$manystore_id);
}
if (isset($shop_id) && $shop_id) {
$selfetch = $selfetch->where("{$a}shop_id", 'in', ''.$shop_id);
}
if (isset($user_id) && $user_id) {
$selfetch = $selfetch->where("{$a}user_id", 'in', ''.$user_id);
}
if (isset($my) && $my && isset($my_user_id) && $my_user_id) {
$selfetch = $selfetch->where("{$a}user_id", 'in', ''.$my_user_id);
}
if (isset($teacher_id) && $teacher_id) {
$selfetch = $selfetch->where("{$a}teacher_id", 'in', ''.$teacher_id);
}
if (isset($type) && $type) {
$selfetch = $selfetch->where("{$a}type", 'in', ''.$type);
}
if (isset($address_type) && $address_type) {
$selfetch = $selfetch->where("{$a}address_type", 'in', ''.$address_type);
}
if (isset($recommend) && $recommend) {
$selfetch = $selfetch->where("{$a}recommend", 'in', ''.$recommend);
}
if (isset($hot) && $hot) {
$selfetch = $selfetch->where("{$a}hot", 'in', ''.$hot);
}
if (isset($new) && $new) {
$selfetch = $selfetch->where("{$a}new", 'in', ''.$new);
}
if (isset($feel) && $feel) {
$selfetch = $selfetch->where("{$a}feel", 'in', ''.$feel);
}
//设置订单信息
if(isset($auth_status) && $auth_status != -1 && $auth_status != -2){
$selfetch = $selfetch->where("{$a}auth_status", 'in', ''.$auth_status);
}
//设置订单信息
if(isset($status) && $status != -1 && $status != -2){
$selfetch = $selfetch->where("{$a}status", 'in', ''.$status);
}
//区域搜索
if (isset($province) && $province) {
$selfetch = $selfetch->where("{$a}province", 'in', ''.$province);
}
if (isset($city) && $city) {
$selfetch = $selfetch->where("{$a}city", 'in', ''.$city);
}
if (isset($district) && $district) {
$selfetch = $selfetch->where("{$a}district", 'in', ''.$district);
}
if (isset($self_label_tag) && $self_label_tag) {
$self_label_tag = implode("|",explode(',',$self_label_tag));
$selfetch = $selfetch->whereRaw(" {$a}self_label_tag REGEXP '({$self_label_tag})'");
}
if (isset($classes_type) && $classes_type) {
$classes_type = implode("|",explode(',',$classes_type));
$selfetch = $selfetch->whereRaw(" {$a}classes_type REGEXP '({$classes_type})'");
}
if (isset($classes_cate_ids) && $classes_cate_ids) {
$classes_cate_ids = implode("|",explode(',',$classes_cate_ids));
$selfetch = $selfetch->whereRaw(" {$a}classes_cate_ids REGEXP '({$classes_cate_ids})'");
}
if (isset($classes_label_ids) && $classes_label_ids) {
$classes_label_ids = implode("|",explode(',',$classes_label_ids));
$selfetch = $selfetch->whereRaw(" {$a}classes_label_ids REGEXP '({$classes_label_ids})'");
}
$collect_classes_lib_ids = [-1];
//需登录查询条件:
if(isset($my_user_id) && $my_user_id){
//得到我收藏的课程ids
$collect_classes_lib_ids = Collect::where("user_id",$my_user_id)->column("classes_lib_id");
//专查我的收藏
if(isset($collect) && $collect){
$selfetch = $selfetch->where("{$a}id","in",$collect_classes_lib_ids);
}
}
//排序
switch ($order) {
case "normal": //综合排序(推薦優先)
$selfetch = $selfetch->order("{$a}sale desc,{$a}sign_num desc,{$a}new desc,{$a}weigh desc,{$a}id desc");
break;
case "distance": //距离优先 权重
$selfetch = $selfetch->order("distance asc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
break;
case "hot": //熱門优先
$selfetch = $selfetch->order("{$a}hot desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
break;
case "new": //平台最新优先
$selfetch = $selfetch->order("{$a}new desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
break;
case "selfhot": //机构热门优先
$selfetch = $selfetch->order("{$a}selfhot desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
break;
case "recommend": //推薦优先
$selfetch = $selfetch->order("{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
break;
case "sale": //銷量优先
$selfetch = $selfetch->order("{$a}sale desc,{$a}verification_num desc,{$a}sign_num desc,{$a}recommend desc,{$a}weigh desc");
break;
case "views": //浏览量优先
$selfetch = $selfetch->order("{$a}views desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc");
break;
case "collect": //收藏量优先
$selfetch = $selfetch->order("{$a}collect desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc");
break;
default:
throw new \Exception("不支持的排序类型");
}
if(isset($nearby) && $nearby) {
$selfetch = $selfetch->having("distance <= {$nearby}");
}
$selfetch = $selfetch->paginate($per_page);
//额外附加数据
foreach ($selfetch as $row) { //迭代器魔术方法遍历,填值自动引用传值
//设置是否已收藏
$row->is_collect = in_array($row->id,$collect_classes_lib_ids) ? 1 : 0;
if($row->is_collect){
$row["collect"] = Collect::where("user_id",$my_user_id)->find();
}
$classes_label_ids = $row['classes_label_ids'];
$classes_cate_ids = $row['classes_cate_ids'];
$classes_type_ids = $row['classes_type'];
$classes_label = $classes_cate = $classes_type = [];
foreach (explode(",", $classes_label_ids) as $classes_label_id){
if(isset($label_list[$classes_label_id]))$classes_label[] = $label_list[$classes_label_id];
}
foreach (explode(",", $classes_cate_ids) as $classes_cate_id){
if(isset($cate_list[$classes_cate_id]))$classes_cate[] = $cate_list[$classes_cate_id];
}
foreach (explode(",", $classes_type_ids) as $classes_type_id){
if(isset($type_list[$classes_type_id]))$classes_type[] = $type_list[$classes_type_id];
}
$row['classes_label'] = $classes_label;
$row['classes_cate'] = $classes_cate;
$row['classes_type_name'] = $classes_type;
}
return $selfetch;
}
public static function update_classes($classes_lib_id){
//更新课程规格库存
$classes_lib = self::get($classes_lib_id);
if($classes_lib){
//所有课时加起来
$classes_lib->limit_num = ClassesSpec::where("classes_lib_id",$classes_lib_id)->sum( "limit_num");
//更新虚拟用户数据
//得到课程所有虚拟参与者数量
$virtual_people = VirtualUser::where("jointype",'1')->where("classes_lib_id",$classes_lib_id)->count();
//如果课程虚拟参与者字段 数量小于 虚拟参与者实际数量 ,则等于虚拟参与者实际数量
$classes_lib->virtual_people = $classes_lib->virtual_people < $virtual_people ? $virtual_people : $classes_lib->virtual_people;
// //得到课程所有虚拟报名者数量
// $virtual_num = VirtualUser::where("havetype",'1')->where("classes_lib_id",$classes_lib_id)->count();
// //如果课程虚拟报名者字段 数量小于 虚拟报名者实际数量 ,则等于虚拟报名者实际数量 havetype
// $classes_lib->virtual_num = $classes_lib->virtual_num < $virtual_num ? $virtual_num : $classes_lib->virtual_num;
//更新课程信息开始和结束时间信息
//课程开始和结束时间等于所有课时的最早开始和最晚结束时间
$classes_lib->start_time = ClassesSpec::where("classes_lib_id",$classes_lib_id)->min("start_time");
$classes_lib->end_time = ClassesSpec::where("classes_lib_id",$classes_lib_id)->max("end_time");
//设置课程收藏
$classes_lib->collect = Collect::where("classes_lib_id",$classes_lib_id)->count();
$classes_lib->save();
//将课程信息和课时信息同步到所有已下单的订单信息中
self::orderInfoSync($classes_lib_id);
}
}
public static function orderInfoSync($classes_lib_id){
$classes_lib = self::get($classes_lib_id);
if($classes_lib){
//查询所有课程订单更新课程单信息
$order = \app\common\model\school\classes\order\OrderDetail::where("classes_lib_id",$classes_lib_id)->select();
foreach ($order as $row){
$update = $classes_lib->toArray();
$update_data = $row->checkAssemblyParameters($update,["id","status","auth_status","createtime","updatetime"]);
$row->save($update_data);
}
//同步更新订单课时信息
$specs = $classes_lib->specs;
foreach ($specs as $spec){
$hourorders = \app\common\model\school\classes\hourorder\Order::where("classes_lib_spec_id",$spec->id)->select();
foreach ($hourorders as $hourorder){
$update = $spec->toArray();
$update_data = $hourorder->checkAssemblyParameters($update,["id","status","auth_status","createtime","updatetime"]);
$hourorder->save($update_data);
}
}
}
}
public static function add_virtual_init($classes_lib_id){
//更新课程规格库存
$classes_lib = self::get($classes_lib_id);
if($classes_lib){
//如果课程虚拟参与者字300-2000随机
$classes_lib->virtual_people = mt_rand(10, 50);
//如果课程虚拟报名者字段
$classes_lib->virtual_num = 0;
$classes_lib->save();
//生成20个虚拟参与者
(new Virtual())->getVirtualUser(20,$classes_lib_id,time(),true);
}
}
/**设置收藏
* @param $id
* @param int $user_id
* @param int $collect 0取消收藏 1收藏
* @param bool $check
* @param bool $trans
* @throws \Exception
*/
public function collect($id,$user_id,$collect,$oper_type='user',$oper_id=0,$trans=false){
$classes_lib = self::where("id",$id)->find();
if(!$classes_lib)throw new \Exception("找不到课程!");
//判断逻辑
if($trans){
self::beginTrans();
}
try{
//事务逻辑
$where = [
"user_id"=>$user_id,
"classes_lib_id"=>$id,
// "weigh"=>$classes_lib["weigh"],
];
//查询是否已收藏
$res1 = Collect::where($where)->find();
if($collect){
if(!$res1){
$where["weigh"] = $classes_lib["weigh"];
//未收藏,添加收藏
$res1 = new Collect();
$res1->allowField(true)->save($where);
}
\app\common\model\school\classes\ClassesLib::update_classes($classes_lib["id"]);
//调用事件
$data = ['classes' => $classes_lib,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id];
\think\Hook::listen('classes_collect_success_after', $data);
}else{
unset($where["weigh"]);
//取消收藏
$res1 = Collect::where($where)->delete();
\app\common\model\school\classes\ClassesLib::update_classes($classes_lib["id"]);
//调用事件
$data = ['classes' => $classes_lib,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id];
\think\Hook::listen('classes_collect_cancel_after', $data);
}
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage());
}
$where["is_collect"] = $collect;
return $where;
}
protected function updateCheck($id,$params=[],$row=null){
if($params && $row){
// var_dump(111);
if(!$this->no_auth_fields_check($params,$row)){
return true;
}
}
// 课程存在未完成订单则不允许操作
$order = Order::where("classes_lib_id",$id)->where("status","in","0,3")->find();
if($order)throw new \Exception("存在正在使用中的课程订单或存在正在售后中的课程订单无法继续操作!");
// 课程存在售后订单则不允许操作
}
//不用审核允许修改的字段
public $no_auth_fields = ["user_id","teacher_id","classes_type","classes_cate_ids","classes_label_ids","self_label_tag",'headimage','images','notice','content',"virtual_num","virtual_collect","underline_price"];
public $need_auth = false;
public $have_auth = false;
public $success_auth = false;
public $error_auth = false;
public function classesCheck(&$params,$shop_id=null,$row=null,$oper_type='user',$oper_id=0)
{
if(!config("site.classes_edit_shop_limit")){
if($row)$params["auth_status"] = $params["auth_status"] ?? $row["auth_status"];
}
if(config("site.classes_number_only_one")){
$params["classes_num"] = 1;
}
$params["auth_status"] = $params["auth_status"] ?? '0';
$params["status"] = $params["status"] ?? '3';
if(!$shop_id)$shop_id = $params["shop_id"] ?? 0;
if(empty($params["weigh"]))$params["weigh"] = 0;
$manystore = Manystore::where("shop_id",$shop_id)->find();
if(!$manystore){
throw new \Exception("店铺不存在");
}
if(!(new \app\common\model\dyqc\ManystoreShop)->checkFull($shop_id))throw new \Exception("对方的认证信息未完善,请您先去帮忙完善!");
$params["manystore_id"] = $manystore["id"];
//验证老师id
$teacher_id = $params['teacher_id'];
$teacher = \app\admin\model\school\classes\Teacher::where("id",$teacher_id)->find();
if(!$teacher){
throw new \Exception("老师不存在");
}
if(!$teacher["user_id"]){
throw new \Exception("当前老师没有前端用户!请换一个!");
}
//老师与当前机构id不一致
if($teacher["manystore_id"] != $manystore["id"]){
throw new \Exception("当前老师与当前机构不匹配,请换一个老师!");
}
$params["user_id"] = $teacher["user_id"];
//课时必须大于等于1
// if($params["classes_num"] < 1) throw new \Exception("课时必须大于等于1");
if($params["price"] < 0) throw new \Exception("售价必须大于0");
//独立地点需传定位信息
if($params["address_type"] == "2"){
if(empty($params["address_city"])
&& !empty($params["province"])
&& !empty($params["city"])
&& !empty($params["district"])){
$province_name = Area::where("id" ,$params['province'])->value("name");
if(!$province_name) throw new \Exception("省份不存在");
$city_name = Area::where("id" ,$params['city'])->value("name");
if(!$city_name) throw new \Exception("市不存在");
$district_name = Area::where("id" ,$params['district'])->value("name");
if(!$district_name) throw new \Exception("区县不存在");
$params['address_city'] = $province_name."/".$city_name."/".$district_name;
}
if(empty($params["address_city"])
|| empty($params["province"])
|| empty($params["city"])
|| empty($params["district"])
|| empty($params["longitude"])
|| empty($params["latitude"])
|| empty($params["address"])
|| empty($params["address_detail"])
) throw new \Exception("独立地点需传定位信息");
}else{
//地址取机构的
$shop = ManystoreShop::where("id",$shop_id)->find();
if(!$shop){
throw new \Exception("店铺不存在");
}
if(empty($shop["address_city"])
|| empty($shop["province"])
|| empty($shop["city"])
|| empty($shop["district"])
|| empty($shop["longitude"])
|| empty($shop["latitude"])
|| empty($shop["address"])
|| empty($shop["address_detail"])
) throw new \Exception("当前机构地址并未完善,请在机构处完善!");
$params["address_city"] = $shop["address_city"];
$params["province"] = $shop["province"];
$params["city"] = $shop["city"];
$params["district"] = $shop["district"];
$params["longitude"] = $shop["longitude"];
$params["latitude"] = $shop["latitude"];
$params["address"] = $shop["address"];
$params["address_detail"] = $shop["address_detail"];
//address
// var_dump($params);
}
;
//收费免费判断
if($params["price"]==0){
$params["feel"] = "1";
}else{
//个人认证无法下付费课程
$shop = ManystoreShop::where("id",$shop_id)->find();
if($shop["type"]=="1"){
throw new \Exception("这个机构属于个人认证,无法发布付费课程!");
}
$params["feel"] = "0";
}
$rule = [
'manystore_id'=>'require',
'shop_id'=>'require',
'teacher_id'=>'require',
"classes_type" => 'require',
'headimage' => 'require',
'title' => 'require',
'images' => 'require',
'address' => 'require',
'address_detail' => 'require',
'longitude' => 'require',
'latitude' => 'require',
'province' => 'require',
'city' => 'require',
'district' => 'require',
'address_city' => 'require',
'type' => 'require',
'classes_num' => 'require',
'address_type' => 'require',
'content' => 'require',
'notice' => 'require',
'price' => 'require',
];
$rule_msg = [
"manystore_id.require"=>'机构id必填',
"shop_id.require"=>'机构id必填',
"teacher_id.require"=>'老师必填',
"classes_type.require"=>'课程类型必填',
"headimage.require"=>'课程头图必填',
'title.require' => '课程名称必须填写',
'images.require' => '课程轮播图必须上传',
'address.require' => '地址必填',
'address_detail.require' => '详细地址必填',
'longitude.require' => '经度必填',
'latitude.require' => '纬度必填',
'province.require' => '省编号必填',
'city.require' => '市编号必填',
'district.require' => '县区编号必填',
'address_city.require' => '城市选择必填',
'type.require' => '地点类型必填',
'classes_num.require'=> '核销数必填',
'address_type.require'=> '地址类型必填',
'content.require'=> '课程详情必填',
'notice.require'=> '课程须知必填',
'price.require'=> '售价必填',
];
self::check($params,$rule,$rule_msg);
//更新
if($row){
$this->have_auth = false;
if($this->need_auth){
//判断更新的变动数据
$this->no_auth_fields_check($params,$row);
if($this->have_auth){
$params['status'] = "3";
$params['auth_status'] = "0";
}
}
$this->updateCheck($row->id,$params,$row);
//名称title不能与其他课程重复
$check_title = self::where('id','<>',$row["id"])->where('title',$params["title"])->find();
if($check_title){
throw new \Exception("课程名称已存在或被其他机构占用,请更改!");
}
//如果存在课程规格,验证每个课时规格的合法性
if(isset($params["spec"])){
//如果存在&quot;的HTML转义字符先反转义再解析json
$params["spec"] = html_entity_decode($params["spec"]);
$spec = json_decode($params["spec"],true);
if(empty($spec))throw new \Exception("请至少添加一个课时规格");
foreach ($spec as $k=>&$v){
unset($v["limit"]);
unset($v["status_name"]);
unset($v["visible"]);
$v["classes_lib_id"] = $row->id;
//先不进行判定,交给提交后再判定
$classesSpec = new ClassesSpec;
$classesSpec->specCheck($v,$shop_id,empty($v["id"])? null : ClassesSpec::get($v["id"]),false,$oper_type,$oper_id);
}
$params["spec"] = $spec;
}
}else{
//新增
//名称title不能重复
$check_title = self::where('title',$params["title"])->find();
if($check_title){
throw new \Exception("课程名称已存在或被其他机构占用,请更改!");
}
//如果存在课程规格,验证每个课时规格的合法性
if(isset($params["spec"])){
//如果存在&quot;的HTML转义字符先反转义再解析json
$params["spec"] = html_entity_decode($params["spec"]);
$spec = json_decode($params["spec"],true);
if(empty($spec))throw new \Exception("请至少添加一个课时规格");
foreach ($spec as $k=>&$v){
unset($v["limit"]);
unset($v["status_name"]);
$v["classes_lib_id"] = 0;
(new ClassesSpec)->specCheck($v,$shop_id,null,true,$oper_type,$oper_id);
}
$params["spec"] = $spec;
}
}
$params["delete_spec_ids"] = [];
//按前端顺序分配显示权重
if(isset($params["spec"]) && $params["spec"]){
//数组倒过来
$params["spec"] = array_reverse($params["spec"]);
$insert_spec = [];
foreach ($params["spec"] as $k=>&$v){
$v["weigh"] = $k + 1;
if(!empty($v["id"]))$insert_spec[] = $v["id"];
}
if($insert_spec && $row){
$params["delete_spec_ids"] = ClassesSpec::where("classes_lib_id",$row->id)->where("id","not in",$insert_spec)->column("id");
}
}
//存在需要删除的规格则判断规格是否能删除
if($params["delete_spec_ids"] && $row){
$check_spec = \app\common\model\school\classes\ClassesSpec::where("classes_lib_id",$row->id)->where("id","in",$params["delete_spec_ids"])->select();
foreach ($check_spec as $k=>$vv){
(new \app\common\model\school\classes\ClassesSpec)->updateCheck($vv->id);
}
$params["delete_spec_ids"] = $check_spec;
}
//如果是上架,判断是否拥有课时规格,没有则无法上架
if(empty($params["spec"])){
if(!$row)throw new \Exception("新添加的课程请先至少添加一个课时规格后再上架!");
//判断是否拥有课时规格,没有则无法上架
$check_spec = \app\common\model\school\classes\ClassesSpec::where("classes_lib_id",$row->id)->count();
if(!$check_spec)throw new \Exception("新添加的课程请先至少添加一个课时规格后再上架!");
}
}
protected function update_check(&$params,$row=null,$shop_id=0, $oper_type="user",$oper_id=null)
{
if(!empty($params["weigh"]))$params["weigh"] = 0;
if(!empty($params["shop_id"]))$params["shop_id"] = $shop_id;
$this->classesCheck($params,$shop_id,$row,$oper_type,$oper_id);
//特有认证判断
// $this->authClasses($params,$row);
// var_dump($row);die;
//更新
if($row){
if($row['status'] == "3" && $params['status'] != $row['status']){
$this->error("平台下架审核的课程无法上架!");
}
//如果是审核失败,提交后自动更新成审核中
if($row['auth_status'] == 2){
$params['status'] = "3";
$params['auth_status'] = 0;
$params["reason"] = "";
}
}else{
//新增(需审核)
$params["add_type"] = '1';
$params["add_id"] = $oper_id ?: $this->auth->id;
$params['status'] = "3";//平台下架
$params['auth_status'] = 0;
$this->have_auth = true;
}
}
/** 操作权限检测
* @param $order 订单
* @param $oper_id 操作人id
* @param $oper_type 操作人类型user-用户或员工admin-管理员
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function checkOptionAuth($classes_id,$oper_id,$oper_type,$only_user=false,$only_shop=false,$only_admin=false){
$classes = null;
$shop_id = 0;
//课程是否存在并上架
if($classes_id){
$classes = self::get($classes_id);
if(!$classes) throw new \Exception("课程不存在或已下架!");
$shop_id = $classes["shop_id"];
}
switch ($oper_type) {
case 'user':
if($only_admin)throw new \Exception("您无权操作该订单!");
if($only_shop)throw new \Exception("您无权操作该订单!");
//自己或操作员
$Shop = Shop::where("user_id",$oper_id)->find();
if($Shop)$shop_id = $Shop["id"];
if($Shop && $classes){
if($Shop["id"] == $classes["shop_id"]){
break;
}
}
//说明是操作员
$help_user_info = User::where('id',$oper_id)->find();
if(!$help_user_info) throw new \Exception("代下单员工不存在!");
$classes_lib_ids = (new ClassesLib)->getClassesAuthIds($oper_id);
if(!$shop_id && $classes_lib_ids)$shop_id = self::where("id" ,"in", $classes_lib_ids)->value("shop_id");
//判断当前订单课程是否在此课程授权范围内
if($classes && !in_array($classes_id,$classes_lib_ids)) throw new \Exception("该课程不在您的授权范围内,无法代操作!");
break;
case 'admin':
$admin_info = Admin::where('id',$oper_id)->find();
if(!$admin_info) throw new \Exception("代下单管理员不存在!");
break;
case 'shop':
if($only_admin)throw new \Exception("您无权操作该订单!");
if($only_user) throw new \Exception("您无权操作该订单!");
$admin_info = Manystore::where('id',$oper_id)->find();
if(!$admin_info) throw new \Exception("代下单管理员不存在!");
$shop_id = $admin_info["shop_id"];
$classes_lib_ids = ClassesLib::where("manystore_id",$oper_id)->column("id");
//判断当前订单课程是否在此课程授权范围内
if($classes && !in_array($classes_id,$classes_lib_ids)) throw new \Exception("该课程不在您的授权范围内,无法代操作!");
break;
default:
throw new \Exception("请选择正确的代下单类型!");
}
if(!$shop_id) throw new \Exception("无法判定课程所属店铺!");
return $shop_id;
}
/** 小程序端操作课程添加
* @param bool $check
* @param bool $trans
* @return bool
* @throws \Exception
*/
public function createClassesByOper(&$params,$check=false,$oper_type='user',$oper_id=0,$trans=false){
$model = new self;
//用户操作权限检测
$shop_id = self::checkOptionAuth(0, $oper_id,$oper_type);
$params["shop_id"] = $shop_id;
//事务逻辑
$this->update_check($params,$row=null,$shop_id, $oper_type,$oper_id);
$spec = $params["spec"];
unset($params["spec"]);
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
$result = $model->allowField(true)->save($params);
\app\common\model\school\classes\ClassesLib::add_virtual_init($model["id"]);
//添加课程规格
foreach ($spec as $k=>$v){
$v["classes_lib_id"] = $model["id"];
\app\common\model\school\classes\ClassesSpec::create($v);
}
//因为是批量添加,所有规格重新进行检测,防止出现时间重叠
$specss = \app\common\model\school\classes\ClassesSpec::where("classes_lib_id",$model["id"])->select();
foreach ($specss as $k=>$specs){
$params =$specs;
(new \app\common\model\school\classes\ClassesSpec)->specCheck($params,null,$specs,true,$oper_type,$oper_id);
}
//课时数必须大于等于课时核销数
$count = \app\common\model\school\classes\ClassesSpec::where("classes_lib_id",$model["id"])->count();
$classes_num = \app\common\model\school\classes\ClassesLib::where("id",$model["id"])->value("classes_num");
if($count < $classes_num){
throw new \Exception("课时数必须大于等于课时核销数");
}
$this->update_classes($model["id"]);
if($this->have_auth){
//调用通过事件
$data = ['classes' => $model];
\think\Hook::listen('classes_auth_need_after', $data);
}
// if($this->success_auth){
// //调用通过事件
// $data = ['classes' => $model];
// \think\Hook::listen('classes_auth_success_after', $data);
// }
//
//
// if($this->error_auth){
// //调用通过事件
// $data = ['classes' => $model];
// \think\Hook::listen('classes_auth_fail_after', $data);
//
// }
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage());
}
return $model;
}
/** 小程序端操作课程修改
* @param bool $check
* @param bool $trans
* @return bool
* @throws \Exception
*/
public function updateClassesByOper($params,$ids,$check=false,$oper_type='user',$oper_id=0,$trans=false){
//用户操作权限检测
$shop_id = self::checkOptionAuth($ids, $oper_id,$oper_type);
$params["shop_id"] = $shop_id;
$row = self::where('id',$ids)->where("shop_id",$shop_id)->find();
if(!$row) throw new \Exception("课程不存在!");
$this->update_check($params,$row,$shop_id,$oper_type, $oper_id);
$spec = $params["spec"] ?? [];//$params["delete_spec_ids"]
$delete_spec_ids = $params["delete_spec_ids"] ?? [];
unset($params["spec"]);
unset($params["delete_spec_ids"]);
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
//事务逻辑
$result = $row->allowField(true)->save($params);
foreach ($spec as $k=>$v){
$v["classes_lib_id"] = $row["id"];
//有id更新否则新增
if(isset($v["id"]) && $v["id"]){
\app\common\model\school\classes\ClassesSpec::update((new \app\common\model\school\classes\ClassesSpec)->checkAssemblyParameters($v));
}else{
\app\common\model\school\classes\ClassesSpec::create((new \app\common\model\school\classes\ClassesSpec)->checkAssemblyParameters($v));
}
}
//删除规格
foreach ($delete_spec_ids as $k=>$delete_spec){
(new \app\common\model\school\classes\ClassesSpec)->updateCheck($delete_spec["id"]);
$delete_spec->delete();
}
//因为是批量添加,所有规格重新进行检测,防止出现时间重叠
$specss = \app\common\model\school\classes\ClassesSpec::where("classes_lib_id",$row["id"])->select();
foreach ($specss as $k=>$specs){
$params =$specs;
(new \app\common\model\school\classes\ClassesSpec)->specCheck($params,null,$specs,true,$oper_type,$oper_id);
}
$this->update_classes($row["id"]);
if($this->have_auth){
//调用通过事件
$data = ['classes' => $row];
\think\Hook::listen('classes_auth_need_after', $data);
}
// if($this->success_auth){
// //调用通过事件
// $data = ['classes' => $model];
// \think\Hook::listen('classes_auth_success_after', $data);
// }
//
//
// if($this->error_auth){
// //调用通过事件
// $data = ['classes' => $model];
// \think\Hook::listen('classes_auth_fail_after', $data);
//
// }
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
}
return $row;
}
/** 小程序端操作课程修改
* @param bool $check
* @param bool $trans
* @return bool
* @throws \Exception
*/
public function deleteClassesByOper($ids,$check=false,$oper_type='user',$oper_id=0,$trans=false){
//用户操作权限检测
$shop_id = self::checkOptionAuth($ids, $oper_id,$oper_type);
$row = self::where('id',$ids)->where("shop_id",$shop_id)->find();
if(!$row) throw new \Exception("课程不存在!");
$list = self::where("id", 'in', $ids)->select();
foreach ($list as $item) {
$this->updateCheck($item->id);
}
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
$count = 0;
//事务逻辑
//删除前检测
foreach ($list as $k => $v) {
//删除课程规格
Spec::where("classes_lib_id",$v->id)->delete();
$count += $v->delete();
}
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
}
return $count;
}
/** 小程序端操作课程上下架
* @param bool $check
* @param bool $trans
* @return bool
* @throws \Exception
*/
public function updateStatusByOper($status,$ids,$check=false,$oper_type='user',$oper_id=0,$trans=false){
//用户操作权限检测
if($check){
$shop_id = self::checkOptionAuth($ids, $oper_id,$oper_type);
if(!in_array($status,['1','2'] ))throw new \Exception("状态参数错误!");
//审核中无法操作上下架
$row = self::where('id',$ids)->where("shop_id",$shop_id)->find();
if(!$row) throw new \Exception("课程不存在或无权限!");
if($row["status"] == '3') throw new \Exception("已平台下架无法操作!");
if($row["auth_status"] != '1' && $status =="1")throw new \Exception("审核通过才能上架!");
}else{
$row = self::where('id',$ids)->find();
if(!$row) throw new \Exception("课程不存在或无权限!");
}
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
//事务逻辑
$params = [
"status"=>$status,
];
$result = $row->allowField(true)->save($params);
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
}
return $row;
}
}