dd/app/Services/TG/Hook/Mod/Book.php
2025-05-11 10:37:57 +08:00

318 lines
8.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\Services\TG\Hook\Mod;
use App\Models\Book as ModelBook;
//use App\Models\BookOption as ModelBookOption;
use App\Models\BookHook as ModelBookHook;
class Book extends Base {
public function cd($aParam)
{
$sKey = $aParam["aArg"][1];
$oBook = ModelBook::where("key", $sKey)->orderBy("no")->get();
if (!$oBook) {
return "没有";
}
ModelBookHook::where("type", "key")->delete();
$oBookHook = new ModelBookHook();
$oBookHook->key = $sKey;
$oBookHook->type = "key";
$r = $oBookHook->save();
return $this->bookToStr($oBook);
}
public function mark($aParam)
{
$iNo = $aParam["aArg"][1];
$sType = $aParam["sType"][1] ?? false;
if ($sType == "un") {
$iValue = 0;
} else {
$iValue = 1;
}
$sKey = ModelBookHook::_first_key("key");
$oBook = ModelBook::where("key", $sKey)->where("no", $iNo)->first();
$oBook->mark = $iValue;
$oBook->save();
$aParam["aArg"][1] = $sKey;
return $this->cd($aParam);
}
public function set($aParam)
{
$iNo = $aParam["aOption"][1];
$sValue = $aParam["aArg"][1];
$sKey = ModelBookHook::_first_key("key");
$oBook = ModelBook::where("key", $sKey)->where("no", $iNo)->first();
$oBook->title = $sValue;
$oBook->save();
$aParam["aArg"][1] = $sKey;
return $this->cd($aParam);
}
// public function setoa($aParam)
// {
// $iNo = $aParam["aOption"][1];
// $sValue = $aParam["aArg"][1];
//
// $sKey = ModelBookHook::_first_key("key");
//
// $oBook = ModelBook::where("key", $sKey)->where("no", $iNo)->first();
// $oBook->title = $sValue;
// $oBook->save();
//
// $aParam["aArg"][1] = $sKey;
// return $this->cd($aParam);
// }
public function o($aParam)
{
$iNo = $aParam["aArg"][1];
$sKey = ModelBookHook::_first_key("key");
$oBook = ModelBook::where("key", $sKey)->where("no", $iNo)->first();
if (!$oBook) {
return "没有";
}
return $oBook->content;
}
public function seto($aParam)
{
$iNo = $aParam["aOption"][1];
$sValue = $aParam["aArg"][1];
$sKey = ModelBookHook::_first_key("key");
$oBook = ModelBook::where("key", $sKey)->where("no", $iNo)->first();
$oBook->content = $sValue;
$r = $oBook->save();
$aParam["aArg"][1] = $sKey;
return $this->cd($aParam);
}
public function key_all($aParam)
{
$oBook = ModelBook::select('key')->distinct()->orderBy("key")->get();
return $this->keyToStr($oBook);
}
public function key_like($aParam)
{
$sLike = $aParam["aArg"][1];
$sType = $aParam["aOption"][1] ?? "a"; // r=向右l=向左a=全部默认a
switch ($sType) {
case "a" :
$sLikeSql = "%".$sLike."%";
break;
case "l" :
$sLikeSql = "%".$sLike;
break;
case "r" :
$sLikeSql = $sLike."%";
break;
default :
return "???";
}
$oKey = ModelBook::select('key')
->distinct()
->where('key', 'like', $sLikeSql) // 替换为你的条件
->orderBy('key')
->get();
return $this->keyToStr($oKey);
}
private function keyToStr($oKey)
{
$str = "";
foreach ($oKey as $row) {
$str .= "\n";
$str .= $row->key;
}
return $str;
}
public function add($aParam)
{
$sTitle = $aParam["aArg"][1];
$oKey = ModelBookHook::where("type", "key")->get();
if ($oKey) {
$i = 0;
foreach ($oKey as $row) {
$oBook = ModelBook::where("key", $row->key)->orderBy("no", "desc")->first();
if ($oBook->no) {
$iNo = (int)$oBook->no+1;
} else {
$iNo = 1;
}
$oModelBook = new ModelBook();
$oModelBook->title = $sTitle;
$oModelBook->key = $row->key;
$oModelBook->no = $iNo;
$oModelBook->save();
$i++;
}
} else {
return "得先cd";
}
$aParam["aArg"][1] = $oKey[0]->key;
return $this->cd($aParam);
}
public function del($aParam)
{
$sType = $aParam["aOption"][1] ?? "no"; // no,id
$iValue = $aParam["aArg"][1];
$oBookHook = ModelBookHook::where("type", "key")->first();
if (!$oBookHook->key) {
return "先cd";
}
$iDelCont = ModelBook::where($sType, $iValue)->where("key", $oBookHook->key)->delete();
// $this->oTGHttp->send("删除".$iDelCont."条");
$this->noReByKey($oBookHook->key);
$aParam["aArg"][1] = $oBookHook->key;
return $this->cd($aParam);
}
public function up($aParam)
{
$iNo = (int) $aParam["aArg"][1];
$iNoTo = $iNo-1;
if ($iNoTo <= 0) {
return "不能小于1";
}
$oBookHook = ModelBookHook::_first("key");
// 拿到他的真实id
$iId = ModelBook::where("key", $oBookHook->key)->where("no", $iNo)->first()?->id;
// 先所有大于noto的+1
$oBooks = ModelBook::where("key", $oBookHook->key)->where('no', '>=', $iNoTo)->get();
foreach ($oBooks as $row) {
$row->no += 1;
$row->save();
}
// 设置他
$oModelBook = ModelBook::where("id", $iId)->first();
$oModelBook->no = $iNoTo;
$oModelBook->save();
// 然后重新排序
$this->noReByKey($oBookHook->key);
$aParam["aArg"][1] = $oBookHook->key;
return $this->cd($aParam);
}
public function down($aParam)
{
$iNo = (int) $aParam["aArg"][1];
$iNoTo = $iNo+1;
$oBookHook = ModelBookHook::_first("key");
// 拿到他的真实id
$iId = ModelBook::where("key", $oBookHook->key)->where("no", $iNo)->first()?->id;
// 先所有大于noto的+1
$oBooks = ModelBook::where("key", $oBookHook->key)->where('no', '<=', $iNoTo)->get();
foreach ($oBooks as $row) {
$row->no -= 1;
$row->save();
}
// 设置他
$oModelBook = ModelBook::where("id", $iId)->first();
$oModelBook->no = $iNoTo;
$oModelBook->save();
// 然后重新排序
$this->noReByKey($oBookHook->key);
$aParam["aArg"][1] = $oBookHook->key;
return $this->cd($aParam);
}
private function bookToStr($oBook)
{
$sStr = "";
foreach ($oBook as $row) {
$sStr .= "\n";
$sStr .= $row->no;
// 可以扩充
// if ($row->content) {
// $sStr .= "::";
// }
// if ($row->content) {
// $sStr .= "🗞️";
// }
$sStr .= ":".$row->title;
if ($row->content) {
$sStr .= " 🗞️";
}
}
return $sStr;
}
private function noReByKey($sKey)
{
$oBook = ModelBook::where("key", $sKey)->orderBy("no")->get();
$i = 1;
foreach ($oBook as $row) {
$row->no = $i;
$row->save();
$i++;
}
return $i;
}
}