DiverseYouthNightSchool/application/common/model/school/classes/activity/ActivityItem.php

261 lines
9.2 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\activity;
use app\admin\model\manystore\Shop;
use app\admin\model\school\classes\activity\Order;
use app\common\model\BaseModel;
use app\common\model\dyqc\ManystoreShop;
use app\common\model\school\classes\ClassesSpec;
use app\manystore\model\Manystore;
use think\Model;
use traits\model\SoftDelete;
class ActivityItem extends BaseModel
{
use SoftDelete;
// 表名
protected $name = 'school_classes_activity_item';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
'status_text',
'sex_text'
];
protected static function init()
{
self::afterInsert(function ($row) {
if (!$row['weigh']) {
$pk = $row->getPk();
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
}
});
}
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2')];
}
public function getSexList()
{
return ['1' => __('Sex 1'), '2' => __('Sex 2'), '3' => __('Sex 3')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getSexTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
$list = $this->getSexList();
return isset($list[$value]) ? $list[$value] : '';
}
public function manystore()
{
return $this->belongsTo(Manystore::class, 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function shop()
{
return $this->belongsTo(ManystoreShop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function activity()
{
return $this->belongsTo(Activity::class, 'classes_activity_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function specCheck(&$params,$shop_id=null,$row=null,$check=true,$oper_type='user',$oper_id=0)
{
//限定人数必须大于0
$limit_num = $params["limit_num"];
if($limit_num<=0){
throw new \Exception("{$params["name"]}限定人数必须大于0");
}
//收费免费判断
if($params["price"]==0){
$params["feel"] = "1";
}else{
//个人认证无法下付费课程
$shop = ManystoreShop::where("id",$params["shop_id"] )->find();
if($shop["type"]=="1"){
throw new \Exception("这个机构属于个人认证,无法发布付费活动!");
}
$params["feel"] = "0";
}
//开始和结束时间不能为空
// $time = $params["time"];
//
// if(empty($time))throw new \Exception("{$params["name"]}请选择开始和结束时间".$time);
// $split_line = " - ";
// $time_arr = explode($split_line,$time);
// $params["start_time"] = $time_arr[0] ;
// $params["end_time"] = $time_arr[1];
// unset($params["time"]);
//
// $start_time = $params["start_time"];
// $end_time = $params["end_time"];
//
// if(empty($start_time) || empty($end_time)){
// throw new \Exception("{$params["name"]}请选择开始和结束时间".$time);
// }
// //转化时间戳
// $start_time = $params["start_time"] && !is_numeric($params["start_time"]) ? strtotime($params["start_time"]) : $params["start_time"];
// $end_time = $params["end_time"] && !is_numeric($params["end_time"]) ? strtotime($params["end_time"]) : $params["end_time"];
// //结束时间不能小于开始时间
// if($end_time<=$start_time){
// throw new \Exception("{$params["name"]}结束时间不能小于开始时间");
// }
//
// //结束时间不能是已经过去的时间
// $now_time = time();
// if($end_time<=$now_time){
// throw new \Exception("{$params["name"]}结束时间不能是已经过去的时间");
// }
// //如果是员工操作则结束时间必须大于当前时间n秒
// if($oper_type == "user" && $oper_id){
// $classes_timeout_time = config("site.classes_timeout_time") ?:0;
// if($classes_timeout_time > 0) {
// $now_times = time() + $classes_timeout_time;
// if($start_time<=$now_times){
// //$now_time时间格式化
// $now_time_text = date("Y-m-d H:i",$now_times);
// throw new \Exception("{$params["name"]}开始时间必须在{$now_time_text}之后");
// }
// }
//
// }
//开始和结束时间必须是同一天
// $start_time_date = date("Y-m-d",$start_time);
// $end_time_date = date("Y-m-d",$end_time);
// if($start_time_date!=$end_time_date){
// throw new \Exception("{$params["name"]}开始和结束时间必须是同一天");
// }
//
// $params["start_time"] = $start_time;
// $params["end_time"] = $end_time;
// $orderTimeTableName = (new ClassesSpec)->getWithAlisaName();
//修改
if($row){
//修复旧数据时间戳
// $row["start_time"]= strtotime(date("Y-m-d H:i",$row["start_time"]));
// $row["end_time"]= strtotime(date("Y-m-d H:i",$row["end_time"]));
//
$this->updateCheck($row->id,$params,$row);
//规格名字不能一样(同课程下)
$spec_name = $params["name"];
$classes_lib_id = $params["classes_activity_id"];
$spec_name_exist = self::where("name",$spec_name)->where("classes_activity_id",$classes_lib_id)->where("id","<>",$row->id)->find();
if($spec_name_exist){
throw new \Exception("该活动下已存在该课时规格名称请重新输入1");
}
// //同课程下,各个课时的开始和结束时间不能有重叠(出现时间交叠也不行)
// $spec_time_exist = ClassesSpec::where(function ($query) use ($orderTimeTableName,$start_time,$end_time) {
// //兩個時間區間重合 存在任意交集 都不行
// $query->where("start_time BETWEEN {$start_time} AND {$end_time}");
// $query->whereOr("end_time BETWEEN {$start_time} AND {$end_time}");
// $query->whereOr("start_time <= {$start_time} AND end_time >= {$end_time}");
// $query->whereOr("start_time >= {$start_time} AND end_time <= {$end_time}");
// })
// ->where("classes_lib_id",$classes_lib_id)
// ->where("id","<>",$row->id)->find();
//
// if($check && $spec_time_exist){
// throw new \Exception("该课程下,{$spec_time_exist['name']}课时的开始和结束时间与你有重叠");
// }
}else{
//规格名字不能一样(同课程下)
$spec_name = $params["name"];
$classes_lib_id = $params["classes_activity_id"];
$spec_name_exist = self::where("name",$spec_name)->where("classes_activity_id",$classes_lib_id)->find();
if($spec_name_exist){
throw new \Exception("该活动下已存在该课时规格名称请重新输入2");
}
// //同课程下,各个课时的开始和结束时间不能有重叠(出现时间交叠也不行)
// $spec_time_exist = ClassesSpec::where(function ($query) use ($orderTimeTableName,$start_time,$end_time) {
// //兩個時間區間重合 存在任意交集 都不行
// $query->where("start_time BETWEEN {$start_time} AND {$end_time}");
// $query->whereOr("end_time BETWEEN {$start_time} AND {$end_time}");
// $query->whereOr("start_time <= {$start_time} AND end_time >= {$end_time}");
// $query->whereOr("start_time >= {$start_time} AND end_time <= {$end_time}");
// })
// ->where("classes_lib_id",$classes_lib_id)
// ->find();
//
// if($check && $spec_time_exist){
// throw new \Exception("该课程下,{$spec_time_exist['name']}课时的开始和结束时间与你有重叠");
// }
}
}
// //不用审核允许修改的字段
public $no_auth_fields = ["name","price"];
public function updateCheck($id,$params=[],$row=null){
if($params && $row){
// var_dump($this->no_auth_fields_check($params,$row));
if(!$this->no_auth_fields_check($params,$row)){
return true;
}
}
// 课程存在售后订单则不允许操作
$order = Order::where("classes_activity_item_id",$id)->where("status","not in","-3,6,9")->find();
if($order)throw new \Exception("存在正在使用中的订单报名学员,规格无法继续操作,如规格有误请下架!");
}
}