2025-05-22 17:53:57 +08:00

192 lines
5.3 KiB
PHP

<?php
namespace app\common\model\home;
use app\common\model\BaseModel;
use think\Model;
class LeaveWord extends BaseModel
{
// 表名
protected $name = 'leave_word';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'status_text'
];
public function getStatusList()
{
return ['1' => __('Status 1'), '2' => __('Status 2')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ?: ($data['status'] ?? '');
$list = $this->getStatusList();
return $list[$value] ?? '';
}
/** 通用新增(后台api版本)
* @param $params
* @param $trans
* @return $this
* @throws \Exception
*/
public function add($params,$trans=false){
if (empty($params)) {
throw new \Exception(__('Parameter %s can not be empty', ''));
}
// if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
// $params[$this->dataLimitField] = $this->auth->id;
// }
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
$params["status"] = '1';
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->validateFailException()->validate($validate);
}
$result = $this->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 $this;
}
/**得到基础条件
* @param $status
* @param null $model
* @param string $alisa
*/
public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false)
{
if (!$model) {
$model = new static;
if ($alisa&&!$with) $model = $model->alias($alisa);
}
if ($alisa) $alisa = $alisa . '.';
$tableFields = (new static)->getTableFields();
foreach ($tableFields as $fields)
{
if(in_array($fields, ["status","show"]))continue;
// if (isset($whereData[$fields]) && $whereData[$fields]) $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
if (isset($whereData[$fields]) && $whereData[$fields]){
if(is_array($whereData[$fields])){
$model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]);
}else{
$model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
}
}
}
if (isset($whereData['status']) && $whereData['status']!=="" && $whereData['status']!==null){
$model = $model->where("{$alisa}status", 'in', $whereData['status'] );
}
if (isset($whereData['show']) && $whereData['show']!=="" && $whereData['show']!==null){
$model = $model->where("{$alisa}show", 'in', $whereData['show'] );
}
if (isset($whereData['keywords'])&&$whereData['keywords']){
$model = $model->where("{$alisa}name|{$alisa}mobile|{$alisa}emil|{$alisa}question|{$alisa}message|{$alisa}answer", 'LIKE', "%{$whereData['keywords']}%" );
}
if (isset($whereData['time'])&&$whereData['time']){
$model = $model->time(["{$alisa}createtime",$whereData['time']]);
}
// if (isset($whereData['has_evaluate'])&&$whereData['has_evaluate']){
// //1查已评价 2查未评价
// if($whereData['has_evaluate'] == 1){
// //1查已评价
// $model = $model->where("{$alisa}classes_evaluate_id", '<>', 0);
// }else{
// //2查未评价
// $model = $model->whereExists(function ($query) use ($alisa) {
// $order_table_name = (new \app\common\model\school\classes\hourorder\Order())->getQuery()->getTable();
// $query->table($order_table_name)->where($order_table_name . '.classes_order_id=' . $alisa . 'id')->where('status', '=', '3');
// })->where("{$alisa}classes_evaluate_id", '=', 0);
//
// }
// }
return $model;
}
public static function allList($page, $limit,$keywords,$status="",$show="",$params=[]){
$with_field = [
// 'cate'=>['image','name'],
// 'attachment'=>['*'],
'base'=>['*'],
];
// $alisa = (new self)->getWithAlisaName();
// $sort = "{$alisa}.weigh desc,{$alisa}.id desc";
$sort = "weigh desc,id desc";
$serch_where = ['status'=>$status,'show'=>$show,'keywords'=>$keywords];
return (new self)->getBaseList(array_merge($serch_where,$params), $page, $limit,$sort,$with_field);
}
}