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

561 lines
13 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\GoodPool as ModelGoodPool;
//use App\Services\TomTool\Http;
//use App\Models\BladeJctj as ModelBladeJctj;
use App\Models\Me as ModelMe;
use App\Models\Option as ModelOption;
class DDT extends Base {
public $oModelSelf;
public $sPrimaryKey = "id";
public $iStrMaxLen = 50;
public $aStrField = [];
public $sThisName;
public function __construct($oModelSelf = false)
{
$this->_setModel($oModelSelf);
}
//// 定义系列
public function _setStrMaxLen($iStrMaxLen)
{
$this->iStrMaxLen = $iStrMaxLen;
}
public function _setStrField($aStrField)
{
$this->aStrField = $aStrField;
}
public function _setThisName($sThisName)
{
$this->sThisName = $sThisName;
}
public function _setModel($oModelSelf)
{
$this->oModelSelf = $oModelSelf;
}
public function _setPrimaryKey($key)
{
$this->sPrimaryKey = $key;
}
//// 定义系列 end
//// option 系列
public function option($aParam)
{
$aOption = ModelOption::_list($this->sThisName);
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 ModelOption();
$oOption->this_name = $this->sThisName;
$oOption->k = $k;
$oOption->v = $v;
$oOption->save();
return $this->toJson(ModelOption::_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 = ModelOption::where("this_name", $this->sThisName)->where("k", $k)->first();
if (!$oOption) {
return "没找到这个";
}
$oOption->v = $v;
$oOption->save();
return $this->toJson(ModelOption::_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 = ModelOption::where("this_name", $this->sThisName)->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
//// me系列
public function me()
{
$oMe = ModelMe::where("class", $this->sThisName)->get();
if ($oMe->isEmpty()) {
return "";
}
return $this->meToStr_list($oMe);
}
public function meAdd($aParam)
{
$aRag = $aParam["aArg"] ?? false;
$sRag = implode(" ", $aRag);
if ($sRag == "?") {
return "meAdd {value}";
}
if (!$sRag) {
return "需要value";
}
$oMe = new ModelMe();
$oMe->class = $this->sThisName;
$oMe->rag = $sRag;
$oMe->save();
return $this->toJson($oMe);
}
public function meLike($aParam)
{
$sValue = $aParam["aArg"][1] ?? false;
if ($sValue == "?") {
return "meLike {value}";
}
if (!$sValue) {
return "需要value";
}
$oMe = ModelMe::where("rag", "like", "%".$sValue."%")->get();
return $this->meToStr_list($oMe);
}
public function meSet($aParam)
{
$iId = $aParam["aOption"][1] ?? false;
$sValue = $aParam["aArg"][1] ?? false;
if ($sValue == "?") {
return "#meSet__{id} {value}";
}
if (!$sValue) {
return "需要value";
}
$oMe = ModelMe::find($iId);
if (!$oMe) {
return "没找到这个";
}
$oMe->rag = $sValue;
$oMe->save();
return $this->toJson($oMe);
}
public function meDelDone($aParam)
{
$aParam["isDone"] = true;
return $this->meDel($aParam);
}
public function meDel($aParam)
{
if ($aParam["aArg"][1] == "?") {
return "#meDel {id多个}";
}
$aId = $aParam["aArg"] ?? [];
$isDone = $aParam["isDone"] ?? false;
if (!$aId) {
return "需要id";
}
$oMe = ModelMe::whereIn("id", $aId)->get();
if ($oMe->isEmpty()) {
return "没找到这个";
}
if ($isDone === false) {
$sMsg = "删除这几个确认用meDelDone。\n\n";
$sMe = $this->meToStr_list($oMe);
return $sMsg.$sMe;
}
if ($isDone === true) {
$i = 0;
foreach ($oMe as $oMeRow) {
$r = $oMeRow->delete();
$i = $i+$r;
}
return "删除".$i."条数据";
}
}
//// 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] = "id";
$aParam["aArg"][1] = $this->oModelSelf->id;
return $this->find($aParam);
}
public function like($aParam)
{
$aParam["is_like"] = true;
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 set($aParam)
{
$sSetValue = $aParam["aArg"] ?? false;
$sSetValue = implode(" ", $sSetValue);
if ($sSetValue == "?") {
$tmp = "#set__{whereField}__{WhereValue}__{setField} {setValue}";
$tmp .= "\n\n";
$tmp .= "#set__{primaryValue主键值默认id}__{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 = $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 "需要setValue";
}
$oSelf = $this->oModelSelf->where($sWhereField, $sWhereValue)->get();
if ($oSelf->isEmpty()) {
return "没找到这个";
}
$i = 0;
foreach ($oSelf as $oSelfRow) {
$oSelfRow->$sSetField = $sSetValue;
$oSelfRow->save();
$i++;
}
if ($i == 1) {
$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 "没找到这个";
}
$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();
// tomd($sTable, 1);
// $columns = \DB::select("SHOW FULL COLUMNS FROM blade");
$columns = \DB::select("SHOW CREATE TABLE `{$sTable}`");
return $this->toJson($columns[0]->{"Create Table"});
// tomd($columns[0]->{"Create Table"}, 1);
}
}