github/app/Services/TG/Hook/Mod/DOG.php
2025-07-17 20:29:22 +08:00

616 lines
15 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
declare(strict_types=1);
namespace App\Services\TG\Hook\Mod;
use App\Models\Me as ModelMe;
use App\Models\DogOption as ModelDogOption;
// $this->oDog->_setPrimaryKey("id"); // 可选默认id
// $this->oDog->_setStrField(["content"]); // 可选,指定长文本字段
// $this->oDog->_setDogName("hub"); // 必须me用option用
class DOG extends Base {
public $oModelSelf;
public $sPrimaryKey = "id";
public $iStrMaxLen = 50;
public $aStrField = [];
public $sDogName;
public function __construct($oModelSelf = false)
{
$this->_setModel($oModelSelf);
}
//// 定义系列
public function _setStrMaxLen($iStrMaxLen)
{
$this->iStrMaxLen = $iStrMaxLen;
}
public function _setStrField($aStrField)
{
$this->aStrField = $aStrField;
}
public function _setDogName($sDogName)
{
$this->sDogName = $sDogName;
}
public function _setModel($oModelSelf)
{
$this->oModelSelf = $oModelSelf;
}
public function _setPrimaryKey($key)
{
$this->sPrimaryKey = $key;
}
//// 定义系列 end
//// option 系列
public function option($aParam)
{
$aOption = ModelDogOption::_list($this->sDogName);
return $this->toJson($aOption);
}
public function optionAdd($aParam)
{
$k = $aParam["aArg"][1] ?? false;
$v = $aParam["aArg"][2] ?? false;
if ($k == "?") {
return "#optionAdd {k} {v}";
}
if (!$k) {
return "需要k";
}
if (!$v) {
return "需要v";
}
$oOption = new ModelDogOption();
$oOption->this_name = $this->sDogName;
$oOption->k = $k;
$oOption->v = $v;
$oOption->save();
return $this->toJson(ModelDogOption::_filter($oOption));
}
public function optionSet($aParam)
{
$k = $aParam["aOption"][1] ?? false;
$v = $aParam["aArg"][1] ?? false;
if ($v == "?") {
return "#optionSet__{k} {v}";
}
if (!$k) {
return "需要k";
}
if (!$v) {
return "需要v";
}
$oOption = ModelDogOption::where("this_name", $this->sDogName)->where("k", $k)->first();
if (!$oOption) {
return "没找到这个";
}
$oOption->v = $v;
$oOption->save();
return $this->toJson(ModelDogOption::_filter($oOption));
}
public function optionDel($aParam)
{
$k = $aParam["aArg"][1] ?? false;
$is_done = $aParam["is_done"] ?? false;
if ($k == "?") {
return "#optionDel {k}";
}
if (!$k) {
return "需要k";
}
$oOption = ModelDogOption::where("this_name", $this->sDogName)->where("k", $k)->first();
if (!$oOption) {
return "没找到这个";
}
if ($is_done === false) {
$sMsg = "匹配以下确认用optionDelDone()。";
$sOption = $this->toJson($oOption);
return $sMsg."\n\n".$sOption;
} else if ($is_done === true) {
$r = $oOption->delete();
return $r;
}
}
public function optionDelDone($aParam)
{
$aParam["is_done"] = true;
return $this->optionDel($aParam);
}
//// option 系列 end
//// 辅助系列
private function filterList($oSelfList)
{
foreach ($oSelfList as &$oSelfListRow) {
$oSelfListRow = $this->filterRow($oSelfListRow);
}
return $oSelfList;
}
private function meToStr_list($oMe)
{
$str = "";
$i = 0;
foreach ($oMe as $oMeRow) {
if ($i !== 0) {
$str .= "\n\n";
}
$str .= $this->meToStr($oMeRow);
$i++;
}
return $str;
}
private function meToStr($oMe)
{
$str = $oMe->id.":".$oMe->rag;
return $str;
}
//// 辅助系列 end
//// json系列
// private function
//// json系列end
//// me系列
public function me()
{
$oMe = ModelMe::where("class", $this->sDogName)->first();
if (!$oMe) {
return "";
}
return $oMe->rag;
}
public function meEdit($aParam)
{
$aValue = $aParam["aArg"] ?? false;
$sValue = implode(" ", $aValue);
if ($sValue == "?") {
return "/meEdit {value}";
}
$aWhere = [
"class" => $this->sDogName,
];
$aValue = [
"rag" => $sValue,
];
$oMe = ModelMe::updateOrCreate($aWhere, $aValue);
return $this->me();
}
//// me系列 end
private function filterRow($oSelfRow)
{
foreach ($this->aStrField as $v) {
$oSelfRow->{$v} = $this->subStr($oSelfRow->{$v});
}
return $oSelfRow;
}
private function subStr($input) {
if (mb_strlen($input, 'UTF-8') > $this->iStrMaxLen) {
return mb_substr($input, 0, $this->iStrMaxLen, 'UTF-8')."……";
}
return $input;
}
public function list()
{
$oSelf = $this->oModelSelf->all();
$oSelf = $this->filterList($oSelf);
return $this->toJson($oSelf);
}
public function add($aParam)
{
$sField = $aParam["aOption"][1] ?? false;
$sValue = $aParam["aArg"][1] ?? false;
if ($sValue == "?") {
return "#add__{field} {value}";
}
if (!$sValue) {
return "需要value";
}
if (!$sField) {
return "需要field";
}
$this->oModelSelf->$sField = $sValue;
$this->oModelSelf->save();
$aParam["aOption"][1] = $this->sPrimaryKey;
$aParam["aArg"][1] = $this->oModelSelf->{$this->sPrimaryKey};
return $this->find($aParam);
}
public function like($aParam)
{
$aParam["is_like"] = true;
return $this->find($aParam);
}
public function clone($aParam)
{
$iId = $aParam['aArg'][1] ?? false;
if ($iId == "?") {
return "#clone {主键值}";
}
if (!$iId) {
return "需要arg[1]";
}
// 查找文章
$oModelSelf = $this->oModelSelf->find($iId);
if (!$oModelSelf) {
return "没找到这个";
}
$oModelSelfNew = $oModelSelf->replicate();
$oModelSelfNew->save();
$aParam = [];
$aParam["aArg"][1] = $oModelSelfNew->{$this->sPrimaryKey};
return $this->find($aParam);
}
public function find($aParam)
{
$sField = $aParam["aOption"][1] ?? $this->sPrimaryKey;
$sValue = $aParam["aArg"][1] ?? false;
$isLike = $aParam["is_like"] ?? false;
$sHitField = $aParam["sHitField"] ?? false;
if ($sValue == "?") {
return "#find__{field?primaryKey} {value}";
}
if (!$sValue) {
return "需要value";
}
if (!$sField) {
return "需要field";
}
if ($isLike === true) {
$oSelf = $this->oModelSelf->where($sField, "like", "%".$sValue."%")->get();
} else {
$oSelf = $this->oModelSelf->where($sField, $sValue)->get();
}
if ($oSelf->isEmpty()) {
return "没找到这个";
}
// hit必然单体
if ($sHitField) {
return $oSelf[0]->$sHitField;
}
return $this->toJson($this->filterList($oSelf));
}
public function kv($aParam)
{
$k = $aParam["aOption"][1] ?? false;
$v = $aParam["aArg"][1] ?? false;
if ($v == "?") {
return "#kv__{k} {v}";
}
$aParam = [];
$aParam["aOption"][1] = $k;
$aParam["aOption"][2] = "v";
$aParam["aArg"][1] = $v;
return $this->set($aParam);
}
public function setJson($aParam)
{
$sWhereValue = $aParam["aOption"][1] ?? false;
$aJson = $aParam["aArg"] ?? false;
$sJson = implode(" ", $aJson);
if ($sJson == "?") {
return "#setJson__{whereValue} {json} | 绑定主键 单体";
}
if (!$sWhereValue) {
return "需要whereValue";
}
if (!$sJson) {
return "需要json";
}
$oModelSelf = $this->oModelSelf->where($this->sPrimaryKey, $sWhereValue)->first();
if (!$oModelSelf) {
return "没找到这个";
}
$aJson = json_decode($sJson, true);
foreach ($aJson as $k => $v) {
$oModelSelf->$k = $v;
}
$oModelSelf->save();
$aParam = [];
$aParam["aArg"][1] = $oModelSelf->{$this->sPrimaryKey};
return $this->find($aParam);
}
public function likeSet($aParam)
{
if (($aParam["aArg"][1] ?? false) == "?") {
return "原set走like匹配慎用";
}
$aParam["is_like"] = true;
return $this->set($aParam);
}
public function likeSetDone($aParam)
{
if (($aParam["aArg"][1] ?? false) == "?") {
return "原set走like匹配慎用";
}
$aParam["is_like"] = true;
$aParam["is_like_done"] = true;
return $this->set($aParam);
}
public function set($aParam)
{
$sSetValue = $aParam["aArg"] ?? false;
$sSetValue = implode(" ", $sSetValue);
$bIsLike = $aParam["is_like"] ?? false;
$bIsLikeDone = $aParam["is_like_done"] ?? false;
if ($sSetValue == "?") {
$tmp = "#set__{whereField}__{WhereValue}__{setField} {setValue} | 多条";
$tmp .= "\n\n";
$tmp .= "#set__{whereValue}__{setField} {setValue} | 主键";
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 = $this->sPrimaryKey;
$sWhereValue = $aParam["aOption"][1] ?? false;
$sSetField = $aParam["aOption"][2] ?? false;
$sMod = "set";
}
if (!$sWhereField) {
return "需要whereField";
}
if (!$sWhereValue) {
return "需要whereValue";
}
if (!$sSetField) {
return "需要setField";
}
if ($bIsLike === false) {
$oSelf = $this->oModelSelf->where($sWhereField, $sWhereValue)->get();
} else if ($bIsLike === true) {
$oSelf = $this->oModelSelf->where($sWhereField, "like", "%".$sWhereValue."%")->get();
if ($bIsLikeDone === false) {
$sMsg = "匹配以下数据确认修改用likeSetDone()。";
$sSelfStr = $this->toJson($oSelf);
return $sMsg.'\n\n'.$sSelfStr;
}
} else {
return "??????????????";
}
if ($oSelf->isEmpty()) {
return "没找到这个";
}
$i = 0;
foreach ($oSelf as $oSelfRow) {
$oSelfRow->$sSetField = $sSetValue;
$oSelfRow->save();
$i++;
}
if ($i == 1) {
$aParam = [];
$aParam["aOption"][1] = $this->sPrimaryKey;
$aParam["aArg"][1] = $oSelfRow->{$this->sPrimaryKey};
return $this->find($aParam);
}
return "影响".$i."条数据";
}
public function delDone($aParam)
{
$aParam["is_done"] = true;
return $this->del($aParam);
}
public function del($aParam)
{
$sField = $aParam["aOption"][1] ?? $this->sPrimaryKey;
$sValue = $aParam["aArg"][1] ?? false;
$bIsDone = $aParam["is_done"] ?? false;
if ($sValue == "?") {
return "#del__{field?默认primaryKey} {value}";
}
$oSelf = $this->oModelSelf->where($sField, $sValue)->get();
if ($oSelf->isEmpty()) {
return "没找到这个";
}
if ($bIsDone == true) {
$i = 0;
foreach ($oSelf as $oSelfRow) {
$ii = $oSelfRow->delete();
$i = $i + $ii;
}
return "删除".$i."条数据";
}
$sMsg = "匹配以下数据确认删除使用delDone()。\n\n";
$sStr = $this->toJson($oSelf);
return $sMsg.$sStr;
}
public function hit($aParam)
{
$sHitField = $aParam["aOption"][1] ?? false;
$sWhereField = $aParam["aOption"][2] ?? $this->sPrimaryKey;
$sWhereValue = $aParam["aArg"][1] ?? false;
if ($sWhereValue == "?") {
return "#hit__{hitField}__{whereField?primaryKey} {whereValue}";
}
if (!$sWhereValue) {
return "需要value";
}
$aParam["sHitField"] = $sHitField;
$aParam["aOption"][1] = $sWhereField;
return $this->find($aParam);
}
public function content($aParam)
{
$sWhereField = $aParam["aOption"][1] ?? $this->sPrimaryKey;
$sValue = $aParam["aArg"][1] ?? false;
if ($sValue == "?") {
return "#content__{whereField?primaryKey} {whereValue}";
}
if (!$sValue) {
return "需要value";
}
$aParam["aOption"][1] = "content";
$aParam["aOption"][2] = $sWhereField;
return $this->hit($aParam);
}
public function table()
{
$sTable = $this->oModelSelf->getTable();
$columns = \DB::select("SHOW CREATE TABLE `{$sTable}`");
return $columns[0]->{"Create Table"};
}
}