dd/app/Models/Book.php
2025-06-16 16:03:33 +08:00

237 lines
7.3 KiB
PHP
Executable File

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
//use BookOption as ModelBookOption;
class Book extends Model
{
protected $table = 'book';
// public $timestamps = false;
protected static $sFieldNick = [
'text' => 'title',
'memo' => 'title',
'txt' => 'title',
't' => 'title',
];
public static function _mapField($sField)
{
if (isset(self::$sFieldNick[$sField])) {
return self::$sFieldNick[$sField];
} else {
return $sField;
}
}
public static function _no($sPath, $iNo, $iIsDel = 0)
{
$oSelf = self::where("path", $sPath)->where("no", $iNo)->orderBy("no")->first();
return $oSelf;
}
public static function _no_arr($sPath, $aNo, $iIsDel = 0)
{
$oSelf = self::where("path", $sPath)->whereIn("no", $aNo)->orderBy("no")->get();
return $oSelf;
}
public static function _id_arr($sPath, $aId, $iIsDel = 0)
{
$oSelf = self::where("path", $sPath)->whereIn("id", $aId)->orderBy("no")->get();
return $oSelf;
}
public static function _path($sPath, $iPage = 1, $iIsDel = 0)
{
// 获取上级id
if ($sPath == "/") {
$iParentId = "1";
} else {
$tmp = trim($sPath, "/");
$aPath = explode("/", $tmp);
$tmp = count($aPath);
$iParentId = $aPath[$tmp-1];
}
// 获取上级rag
$oParentRag = self::where("id", $iParentId)->first();
// 上级属性
$sOrder = $oParentRag->order ?? "no";
$sOrderType = $oParentRag->order_type ?? "asc";
// 上级option
$aRagOption = json_decode($oParentRag->option, 1);
if (empty($oParentRag->limit)) {
$iLimit = BookOption::_hit("page_limit");
} else {
$iLimit = $oParentRag->limit;
}
$oSelf = self::where("path", $sPath)->where("is_del", $iIsDel);
// where
if ($iPage !== "all") {
// dir特殊
if ($oParentRag->type == "dir") {
if (!empty($oParentRag->sort)) {
$aSort = explode("_", $oParentRag->sort);
$oSelf = $oSelf->orWhere("path", $oParentRag->path);
$oSelf = $oSelf->where($aSort[0], $aSort[1]);
}
} else if ($oParentRag->type == "todo") {
if (!isset($oParentRag->sort) && !isset($oParentRag->like)) {
$sTodoShowRank = BookOption::_hit("todo_show_rank");
$aTodoShowRank = explode(",", $sTodoShowRank);
if ($oParentRag->case == "A") {
$oSelf = $oSelf->where(function($query) use ($aTodoShowRank) {
$query->whereIn("rank", $aTodoShowRank)
->orWhereNull("rank");
});
} else if ($oParentRag->case == "B") {
$oSelf = $oSelf->where(function($query) use ($aTodoShowRank) {
$query->whereNotIn("rank", $aTodoShowRank)
->where("rank", "!=", "done")
->orWhereNull("rank");
});
} else if ($oParentRag->case == "DONE") {
$oSelf = $oSelf->where("rank", "done");
}
}
}
if (isset($aRagOption["where"])) {
$oSelf = $oSelf->whereRaw($aRagOption["where"]);
} else {
if ($oParentRag->sort) {
// tomd(11, 1);
$aSort = explode("_", $oParentRag->sort);
$oSelf = $oSelf->where($aSort[0], $aSort[1]);
} if ($oParentRag->like) {
$oSelf = $oSelf->where("rag", "like", "%".$oParentRag->like."%");
}
}
// sort覆盖特殊规则
// if ($oParentRag->type == "dir") {
//
// if (!empty($oParentRag->sort)) {
//
// $aSort = explode("_", $oParentRag->sort);
// $oSelf = $oSelf->orWhere("path", $oParentRag->path);
// $oSelf = $oSelf->where($aSort[0], $aSort[1]);
//
// }
//
// } else if ($oParentRag->sort) {
//
// $aSort = explode("_", $oParentRag->sort);
// $oSelf = $oSelf->where($aSort[0], $aSort[1]);
//
// } else {
//
// if (isset($aRagOption["where"])) {
// $oSelf = $oSelf->whereRaw($aRagOption["where"]);
// }
//
// if ($oParentRag->type == "todo") {
//
// $sTodoShowRank = BookOption::_hit("todo_show_rank");
// $aTodoShowRank = explode(",", $sTodoShowRank);
// $oSelf = $oSelf->where(function($query) use ($aTodoShowRank) {
// $query->whereIn("rank", $aTodoShowRank)
// ->orWhereNull("rank");
// });
//
// }
//
// }
}
// order
if ($oParentRag->order == "rank_todo") {
$sOrderRank = BookOption::_hit("order_rank_todo");
// 默认也rank排序
} else {
$sOrderRank = BookOption::_hit("order_rank");
}
$aOrderRank = explode(",", $sOrderRank);
$sOrderRank = implode(",", array_reverse($aOrderRank));
$oSelf = $oSelf->orderByRaw("FIELD(rank, " . $sOrderRank . ") DESC, (rank IS NULL) ASC");
$oSelf = $oSelf->orderByRaw("fire DESC, (fire IS NULL) ASC");
$sOrder = "no";
$sOrderType = "asc";
$oSelf = $oSelf->orderBy($sOrder, $sOrderType);
$oSelf = $oSelf->orderBy("updated_at", "desc");
// tomd($oSelf->toSql(), 1);
if ($iPage == "all") {
$oSelf = $oSelf->get();
} else {
$oSelf = $oSelf->paginate($iLimit, ['*'], 'page', $iPage);
}
return $oSelf;
}
public static function _noByChecked($sPath, $sType = "no")
{
$aNo = self::where("path", $sPath)->where("checked", 1)->pluck($sType)->toArray();
return $aNo;
}
}