dd/app/Services/TG/Hook/Mod/BladeOld.php
2025-07-16 18:55:53 +08:00

208 lines
5.2 KiB
PHP

<?php
namespace App\Services\TG\Hook\Mod;
use App\Models\Blade as ModelBlade;
class Blade extends Base {
private function subStr($string, $length = 50) {
if (strlen($string) > $length) {
return mb_substr($string, 0, $length) . '...'; // 添加省略号
}
return $string;
}
private function _filter($oBladeRow)
{
$oBladeRow->content = $this->subStr($oBladeRow->content);
unset($oBladeRow->id);
return $oBladeRow;
}
private function filter($oBlade, $sMod = "row")
{
if ($sMod == "row") {
$oBlade = $this->_filter($oBlade);
} else if ($sMod == "list") {
foreach ($oBlade as &$oBladeRow) {
$oBladeRow = $this->_filter($oBladeRow);
}
}
return $oBlade;
}
public function List()
{
$oBlade = ModelBlade::all();
$oBlade = $this->filter($oBlade, "list");
return $this->toJson($oBlade);
}
public function Find($aParam)
{
$sField = $aParam["aOption"][1] ?? "id";
$sValue = $aParam["aArg"][1] ?? false;
if ($sValue == "?") {
return "/blade#find__{field?} {value}";
}
$oBlade = ModelBlade::where($sField, $sValue)->first();
return $this->toJson($oBlade);
}
public function Content($aParam)
{
$sField = $aParam["aOption"][1] ?? "code";
$sValue = $aParam["aArg"][1] ?? false;
if ($sValue == "?") {
return "/blade#content__{field?} {value}";
}
$oBlade = ModelBlade::where($sField, $sValue)->first();
return $oBlade->content;
}
public function Like($aParam)
{
$sField = $aParam["aOption"][1] ?? false;
$sValue = $aParam["aArg"][1] ?? false;
if ($sValue == "?") {
return "/blade#like__{field} {value}";
}
if (!$sField) {
return "需要field";
}
if (!$sValue) {
return "需要value";
}
$oBlade = ModelBlade::where($sField, "like", "%".$sValue."%")->get();
$oBlade = $this->filter($oBlade);
return $this->toJson($oBlade);
}
public function Add($aParam)
{
$sCode = $aParam["aArg"][1] ?? false;
if ($sCode == "?") {
return "/blade#add <code>";
}
if (!$sCode) {
return "需要code";
}
$bExists = ModelBlade::where("code", $sCode)->exists();
if ($bExists) {
return "code以存在";
}
$oBlade = new ModelBlade();
$oBlade->code = $sCode;
$oBlade->save();
$aParam["aOption"][1] = "code";
$aParam["aArg"][1] = $sCode;
return $this->Find($aParam);
}
public function Del($aParam)
{
$sCode = $aParam["aArg"][1] ?? false;
$sWhereField = $aParam["aOption"][1] ?? false;
if ($sCode == "?") {
return "/blade#del__<whereField> <code>";
}
if (!$sCode) {
return "需要code";
}
$oBlade = ModelBlade::where("code", $sCode);
if (!$oBlade->exists()) {
return "没有这个code";
}
$r = $oBlade->delete();
return $r;
}
public function Set($aParam)
{
$sSetValue = $aParam["aArg"] ?? false;
$sSetValue = implode(" ", $sSetValue);
if ($sSetValue == "?") {
$tmp = "/blade#set__<whereField>__<WhereValue>__<setField> <setValue>";
$tmp .= "\n /blade#set__<code>__<setField> <serValue>";
return $tmp;
}
// 有第三参数说明是findSet模式
if (isset($aParam["aOption"][3])) {
$sWhereField = $aParam["aOption"][1] ?? false;
$sWhereValue = $aParam["aOption"][2] ?? false;
$sSetField = $aParam["aOption"][3] ?? false;
$sMod = "findSet";
} else {
$sWhereField = "code";
$sWhereValue = $aParam["aOption"][1] ?? false;
$sSetField = $aParam["aOption"][2] ?? false;
$sMod = "set";
}
if (!$sWhereField) {
return "需要whereField";
}
if (!$sWhereValue) {
return "需要whereValue";
}
if (!$sSetField) {
return "需要setValue";
}
$oBlade = ModelBlade::where($sWhereField, $sWhereValue)->get();
if ($oBlade->isEmpty()) {
return "没找到这个blade";
}
$i = 0;
foreach ($oBlade as $oBladeRow) {
$oBladeRow->$sSetField = $sSetValue;
$oBladeRow->save();
$i++;
}
if ($i == 1) {
$aParam["aOption"][1] = "code";
$aParam["aArg"][1] = $oBladeRow->code;
return $this->find($aParam);
}
return "影响".$i."条数据";
}
}