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

247 lines
9.1 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\common\model\BaseModel;
use think\Model;
use traits\model\SoftDelete;
class ClassesSpec extends BaseModel
{
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' ,
'time'
];
public $no_auth_fields = ['name','limit_num','status','weigh',"start_time","end_time"];
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 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;
}
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()
{
return $this->belongsTo(ClassesLib::class, 'classes_lib_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
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 = \app\common\model\school\classes\hour\Order::where("classes_lib_spec_id",$id)->where("status","in","-1,0")->find();
if($order)throw new \Exception("{$order['name']}存在正在使用中的课时订单报名学员,课时规格无法继续操作,如规格有误请下架!");
}
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");
}
//开始和结束时间不能为空
$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_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();
if($check && $spec_time_exist){
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();
if($check && $spec_time_exist){
throw new \Exception("该课程下,{$spec_time_exist['name']}课时的开始和结束时间与你有重叠");
}
}
}
}