284 lines
7.7 KiB
PHP
Executable File
284 lines
7.7 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Article;
|
|
|
|
use App\Models\SiteMap as ModelSiteMap;
|
|
use App\Models\ArticleSchema as ModelArticleSchema;
|
|
use App\Models\ArticleSiteMapGroup as ModelArticleSiteMapGroup;
|
|
//use App\Services\SlaveMap as ServiceSlaveMap;
|
|
use App\Services\TomTool\Flow;
|
|
|
|
/**
|
|
|
|
简易:
|
|
Cmd::start($sArticleCode)->save("new")->syncOpen();
|
|
|
|
说明:
|
|
设置goupCode设置整租
|
|
设置siteCode设置单site
|
|
都不合适为设置全部
|
|
|
|
new:
|
|
start($sArticleCode)
|
|
|
|
set:
|
|
setSiteCode()
|
|
setSiteGroupCode()
|
|
|
|
action:
|
|
del($sCmdKey)
|
|
moveToLog($sCmdKey)
|
|
save($sCmdKey, $sCmdValue)
|
|
syncOpen(bool)
|
|
reset($sCmdKey, $sCmdValue)
|
|
clear()
|
|
|
|
helper:
|
|
::helper_buildCmd($sCmdKey, $sCmd = '', $aOpt = '')
|
|
::helper_buildCmdKey($sSiteGroupCode, $sSiteCode)
|
|
::helper_cmdKeyByArticleGroup($sArticleGoupCode, $xDefultCmd = '', $xDefultValue = '')
|
|
|
|
*/
|
|
|
|
final class Cmd
|
|
{
|
|
public string $sArticleCode = '';
|
|
public string $sSiteGroupCode = '';
|
|
public string $sSiteCode = '';
|
|
public string $sCmd = '';
|
|
|
|
public $oArticle = null;
|
|
|
|
public static function start(string $sArticleCode)
|
|
{
|
|
$oInstance = new self();
|
|
$oInstance->sArticleCode = $sArticleCode;
|
|
return $oInstance;
|
|
}
|
|
|
|
public static function startWithO($oArticleSchema)
|
|
{
|
|
$oInstance = new self();
|
|
$oInstance->sArticleCode = $oArticleSchema->code;
|
|
$oInstance->oArticle = $oArticleSchema;
|
|
return $oInstance;
|
|
}
|
|
|
|
public function setSiteCode(string $sSiteCode): self
|
|
{
|
|
$this->sSiteCode = $sSiteCode;
|
|
return $this;
|
|
}
|
|
|
|
public function setSiteGroupCode(string $sSiteGroupCode): self
|
|
{
|
|
$this->sSiteGroupCode = $sSiteGroupCode;
|
|
return $this;
|
|
}
|
|
|
|
public function del($sCmdKey)
|
|
{
|
|
$this->useArticle();
|
|
$aCmdList = json_decode($this->oArticle->cmd ?? '', true);
|
|
|
|
unset($aCmdList[$sCmdKey]);
|
|
|
|
$this->oArticle->cmd = json_encode($aCmdList);
|
|
$this->oArticle->save();
|
|
|
|
return true;
|
|
}
|
|
|
|
public function moveToLog($sCmdKey)
|
|
{
|
|
$oFlow = Flow::start("save");
|
|
|
|
$this->useArticle();
|
|
$aCmdList = json_decode($this->oArticle->cmd ?? '[]', true);
|
|
$aCmdLog = json_decode($this->oArticle->cmd_log ?? '[]', true);
|
|
|
|
$aCmdList[$sCmdKey]["log_date"] = date("Y-m-d H:i:s");
|
|
|
|
$aCmdLog[$sCmdKey] = $aCmdList[$sCmdKey];
|
|
unset($aCmdList[$sCmdKey]);
|
|
|
|
if (count($aCmdLog) >= 50) {
|
|
array_shift($aCmdLog);
|
|
}
|
|
|
|
if (count($aCmdList) <= 0) {
|
|
$this->syncOpen(false);
|
|
}
|
|
|
|
$this->oArticle->cmd = json_encode($aCmdList);
|
|
$this->oArticle->cmd_log = json_encode($aCmdLog);
|
|
$this->oArticle->save();
|
|
|
|
return $oFlow->done();
|
|
}
|
|
|
|
public function save(string $sCmd = '', $aOpt = []): Flow
|
|
{
|
|
$oFlow = Flow::start("save");
|
|
|
|
$this->useArticle();
|
|
$aCmdList = json_decode($this->oArticle->cmd ?? '', true);
|
|
|
|
$this->useSiteGroupCode();
|
|
|
|
if ($this->sSiteCode) {
|
|
|
|
$sCmdKey = self::helper_buildCmdKey($this->sSiteGroupCode, $this->sSiteCode);
|
|
$aCmd = self::helper_buildCmd($sCmdKey, $sCmd, $aOpt, $sSiteCode);
|
|
$aCmdList[$sCmdKey] = $aCmd;
|
|
|
|
} else if ($this->sSiteGroupCode) {
|
|
|
|
$aSiteList = ModelSiteMap::where("group_code", $this->sSiteGroupCode)->pluck("code")->toArray();
|
|
|
|
foreach ($aSiteList as $sSiteCode) {
|
|
$sCmdKey = self::helper_buildCmdKey($this->sSiteGroupCode, $sSiteCode);
|
|
$aCmd = self::helper_buildCmd($sCmdKey, $sCmd, $aOpt, $sSiteCode);
|
|
$aCmdList[$sCmdKey] = $aCmd;
|
|
}
|
|
|
|
} else {
|
|
$aCmdListNew = self::helper_cmdKeyByArticleGroup($this->oArticle->group_code, $sCmd, $aOpt);
|
|
foreach ($aCmdListNew as $sCmdKey => $aCmdListNewRow) {
|
|
$aCmdList[$sCmdKey] = $aCmdListNewRow;
|
|
}
|
|
}
|
|
|
|
$this->oArticle->cmd = json_encode($aCmdList);
|
|
$r = $this->oArticle->save();
|
|
|
|
if (!$r) {
|
|
return $oFlow->fail("更新cmd失败");
|
|
}
|
|
|
|
$this->_syncAuto();
|
|
|
|
return $oFlow->done($this->oArticle->id);
|
|
}
|
|
|
|
private function _syncAuto()
|
|
{
|
|
$this->useArticle();
|
|
$aCmd = json_decode($this->oArticle->cmd, true) ?? [];
|
|
|
|
if (count($aCmd) > 0) {
|
|
$this->oArticle->enable_sync = 1;
|
|
} else {
|
|
$this->oArticle->enable_sync = 0;
|
|
}
|
|
|
|
return $this->oArticle->save();
|
|
}
|
|
|
|
public function syncOpen(bool $enable = true): Self
|
|
{
|
|
$this->useArticle();
|
|
$this->oArticle->enable_sync = $enable ? 1 : 0;
|
|
$this->oArticle->save();
|
|
return $this;
|
|
}
|
|
|
|
public function clear()
|
|
{
|
|
$this->useArticle();
|
|
$this->oArticle->cmd = null;
|
|
$this->oArticle->save();
|
|
return true;
|
|
}
|
|
|
|
public static function helper_buildCmd($sCmdKey, $sCmd = '', $aOpt = [], $sSiteCode = '')
|
|
{
|
|
$arr = [
|
|
"site_code"=> $sSiteCode,
|
|
"cmd" => $sCmd,
|
|
"opt" => $aOpt,
|
|
"date" => date("Y-m-d H:i:s"),
|
|
];
|
|
|
|
return $arr;
|
|
}
|
|
|
|
public function reset($sCmd = "new", $sCmdValue = "")
|
|
{
|
|
$this->useArticle();
|
|
|
|
$aCmdList = self::helper_cmdKeyByArticleGroup($this->oArticle->group_code, "new", $sCmdValue);
|
|
|
|
$this->oArticle->cmd = json_encode($aCmdList);
|
|
$this->oArticle->save();
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function helper_buildCmdKey($sSiteGroupCode, $sSiteCode)
|
|
{
|
|
$sCmdKey = "|{$sSiteGroupCode}|{$sSiteCode}|".date("YmdHis").'|';
|
|
return $sCmdKey;
|
|
}
|
|
|
|
public static function helper_cmdKeyByArticleGroup(string $sArticleGoupCode, $xDefultCmd = '', $xDefultValue = ''): array
|
|
{
|
|
|
|
$aBotCode = explode("::", $sArticleGoupCode);
|
|
$sArticleGoupCode = $aBotCode[0];
|
|
|
|
$aSiteGroupList = ModelArticleSiteMapGroup::where("article_group_code", $sArticleGoupCode)->pluck("site_group_code")->toArray();
|
|
|
|
$aCmdList = [];
|
|
foreach ($aSiteGroupList as $sSiteGroupCode) {
|
|
|
|
$aSiteList = ModelSiteMap::where("group_code", $sSiteGroupCode)->pluck("code")->toArray();
|
|
|
|
foreach ($aSiteList as $sSiteCode) {
|
|
$sCmdKey = self::helper_buildCmdKey($sSiteGroupCode, $sSiteCode);
|
|
|
|
$aCmdList[$sCmdKey] = [
|
|
"site_code"=> $sSiteCode,
|
|
"cmd" => $xDefultCmd,
|
|
"value" => $xDefultValue,
|
|
"date" => date("Y-m-d H:i:s"),
|
|
];
|
|
}
|
|
|
|
}
|
|
|
|
return $aCmdList;
|
|
}
|
|
|
|
private function useSiteGroupCode(): void
|
|
{
|
|
if (!$this->sSiteGroupCode) {
|
|
$this->sSiteGroupCode = $this->_getSiteGroupCode($this->sSiteCode);
|
|
}
|
|
}
|
|
|
|
private function _getSiteGroupCode($sSiteCode): string
|
|
{
|
|
return ModelSiteMap::where("code", $sSiteCode)->first()?->group_code ?? '';
|
|
}
|
|
|
|
private function useArticle(): bool
|
|
{
|
|
if (!$this->oArticle) {
|
|
$oArticleSchema = ModelArticleSchema::where("code", $this->sArticleCode)->first();
|
|
|
|
if (!$oArticleSchema) {
|
|
throw new \Exception("没找到文章");
|
|
}
|
|
|
|
$this->oArticle = $oArticleSchema;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|