2024-11-07 18:11:08 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\model\school\classes;
|
|
|
|
|
2024-11-27 18:15:32 +08:00
|
|
|
use app\common\model\BaseModel;
|
2024-11-07 18:11:08 +08:00
|
|
|
use think\Model;
|
|
|
|
use traits\model\SoftDelete;
|
|
|
|
|
2024-11-27 18:15:32 +08:00
|
|
|
class ClassesSpec extends BaseModel
|
2024-11-07 18:11:08 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
use SoftDelete;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 表名
|
|
|
|
protected $name = 'school_classes_lib_spec';
|
|
|
|
|
|
|
|
// 自动写入时间戳字段
|
|
|
|
protected $autoWriteTimestamp = 'integer';
|
|
|
|
|
|
|
|
// 定义时间戳字段名
|
|
|
|
protected $createTime = 'createtime';
|
|
|
|
protected $updateTime = 'updatetime';
|
|
|
|
protected $deleteTime = 'deletetime';
|
|
|
|
|
|
|
|
// 追加属性
|
|
|
|
protected $append = [
|
|
|
|
'start_time_text',
|
|
|
|
'end_time_text',
|
|
|
|
'status_text'
|
|
|
|
];
|
|
|
|
|
2024-11-29 18:05:29 +08:00
|
|
|
public $no_auth_fields = ['name','limit_num','status','weigh'];
|
2024-11-07 18:11:08 +08:00
|
|
|
protected static function init()
|
|
|
|
{
|
|
|
|
self::afterInsert(function ($row) {
|
2024-11-29 18:05:29 +08:00
|
|
|
if (empty($row['weigh'])) {
|
2024-11-07 18:11:08 +08:00
|
|
|
$pk = $row->getPk();
|
|
|
|
$row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-12-04 18:17:08 +08:00
|
|
|
|
|
|
|
public function getTimeAttr($value, $data)
|
|
|
|
{
|
|
|
|
$start_time = isset($data['start_time']) ? $data['start_time'] : '';
|
|
|
|
$end_time = isset($data['end_time']) ? $data['end_time'] : '';
|
|
|
|
$start_time = is_numeric($start_time) ? date("Y/m/d H:i", $start_time) : $start_time;
|
|
|
|
$end_time = is_numeric($end_time) ? date("Y/m/d H:i", $end_time) : $end_time;
|
|
|
|
return $start_time .' - '.$end_time;
|
|
|
|
}
|
|
|
|
|
2024-11-07 18:11:08 +08:00
|
|
|
|
|
|
|
public function getStatusList()
|
|
|
|
{
|
|
|
|
return ['1' => __('Status 1'), '2' => __('Status 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getStatusTextAttr($value, $data)
|
|
|
|
{
|
|
|
|
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
|
|
|
|
$list = $this->getStatusList();
|
|
|
|
return isset($list[$value]) ? $list[$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 lib()
|
|
|
|
{
|
2024-11-21 18:03:23 +08:00
|
|
|
return $this->belongsTo(ClassesLib::class, 'classes_lib_id', 'id', [], 'LEFT')->setEagerlyType(0);
|
2024-11-07 18:11:08 +08:00
|
|
|
}
|
2024-11-28 18:47:18 +08:00
|
|
|
|
|
|
|
|
2024-11-29 18:05:29 +08:00
|
|
|
public function updateCheck($id,$params=[],$row=null){
|
2024-11-28 18:47:18 +08:00
|
|
|
if($params && $row){
|
2024-11-29 18:05:29 +08:00
|
|
|
// var_dump($this->no_auth_fields_check($params,$row));
|
2024-11-28 18:47:18 +08:00
|
|
|
if(!$this->no_auth_fields_check($params,$row)){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 课程存在售后订单则不允许操作
|
|
|
|
$order = \app\common\model\school\classes\hour\Order::where("classes_lib_spec_id",$id)->where("status","in","-1,0")->find();
|
2024-12-11 19:03:47 +08:00
|
|
|
if($order)throw new \Exception("{$order['name']}存在正在使用中的课时订单报名学员,课时规格无法继续操作,如规格有误请下架!");
|
2024-11-28 18:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-04 18:17:08 +08:00
|
|
|
public function specCheck(&$params,$shop_id=null,$row=null,$check=true)
|
2024-11-28 18:47:18 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
//限定人数必须大于0
|
|
|
|
$limit_num = $params["limit_num"];
|
|
|
|
if($limit_num<=0){
|
2024-12-11 19:03:47 +08:00
|
|
|
throw new \Exception("{$params["name"]}限定人数必须大于0");
|
2024-11-28 18:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//开始和结束时间不能为空
|
|
|
|
$time = $params["time"];
|
2024-12-11 19:03:47 +08:00
|
|
|
|
|
|
|
if(empty($time))throw new \Exception("{$params["name"]}请选择开始和结束时间".$time);
|
2024-11-28 18:47:18 +08:00
|
|
|
$split_line = " - ";
|
|
|
|
$time_arr = explode($split_line,$time);
|
2024-11-29 18:05:29 +08:00
|
|
|
$params["start_time"] = $time_arr[0] ;
|
2024-11-28 18:47:18 +08:00
|
|
|
$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)){
|
2024-12-11 19:03:47 +08:00
|
|
|
throw new \Exception("{$params["name"]}请选择开始和结束时间".$time);
|
2024-11-28 18:47:18 +08:00
|
|
|
}
|
|
|
|
//转化时间戳
|
|
|
|
$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){
|
2024-12-11 19:03:47 +08:00
|
|
|
throw new \Exception("{$params["name"]}结束时间不能小于开始时间");
|
2024-11-28 18:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//结束时间不能是已经过去的时间
|
|
|
|
$now_time = time();
|
|
|
|
if($end_time<=$now_time){
|
2024-11-29 18:05:29 +08:00
|
|
|
// throw new \Exception("结束时间不能是已经过去的时间");
|
2024-11-28 18:47:18 +08:00
|
|
|
}
|
|
|
|
|
2024-12-11 19:03:47 +08:00
|
|
|
|
|
|
|
//开始和结束时间必须是同一天
|
|
|
|
$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"]}开始和结束时间必须是同一天");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-29 18:05:29 +08:00
|
|
|
$params["start_time"] = $start_time;
|
|
|
|
$params["end_time"] = $end_time;
|
|
|
|
|
2024-11-28 18:47:18 +08:00
|
|
|
|
|
|
|
$orderTimeTableName = (new ClassesSpec)->getWithAlisaName();
|
|
|
|
//修改
|
|
|
|
if($row){
|
2024-11-29 18:05:29 +08:00
|
|
|
//修复旧数据时间戳
|
|
|
|
$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);
|
2024-11-28 18:47:18 +08:00
|
|
|
|
|
|
|
//规格名字不能一样(同课程下)
|
|
|
|
$spec_name = $params["name"];
|
|
|
|
$classes_lib_id = $params["classes_lib_id"];
|
|
|
|
$spec_name_exist = ClassesSpec::where("name",$spec_name)->where("classes_lib_id",$classes_lib_id)->where("id","<>",$row->id)->find();
|
|
|
|
if($spec_name_exist){
|
|
|
|
throw new \Exception("该课程下已存在该课时规格名称,请重新输入");
|
|
|
|
}
|
|
|
|
|
|
|
|
//同课程下,各个课时的开始和结束时间不能有重叠(出现时间交叠也不行)
|
|
|
|
$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();
|
|
|
|
|
2024-12-04 18:17:08 +08:00
|
|
|
if($check && $spec_time_exist){
|
2024-11-28 18:47:18 +08:00
|
|
|
throw new \Exception("该课程下,{$spec_time_exist['name']}课时的开始和结束时间与你有重叠");
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
//规格名字不能一样(同课程下)
|
|
|
|
$spec_name = $params["name"];
|
|
|
|
$classes_lib_id = $params["classes_lib_id"];
|
|
|
|
$spec_name_exist = ClassesSpec::where("name",$spec_name)->where("classes_lib_id",$classes_lib_id)->find();
|
|
|
|
if($spec_name_exist){
|
|
|
|
throw new \Exception("该课程下已存在该课时规格名称,请重新输入");
|
|
|
|
}
|
|
|
|
|
|
|
|
//同课程下,各个课时的开始和结束时间不能有重叠(出现时间交叠也不行)
|
|
|
|
$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();
|
|
|
|
|
2024-12-04 18:17:08 +08:00
|
|
|
if($check && $spec_time_exist){
|
2024-11-28 18:47:18 +08:00
|
|
|
throw new \Exception("该课程下,{$spec_time_exist['name']}课时的开始和结束时间与你有重叠");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-07 18:11:08 +08:00
|
|
|
}
|