AbstractOperate.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sunmoon<i@liming.me>
  5. * Date: 2019-07-31
  6. * Time: 09:22
  7. */
  8. namespace common\libs\logging\operate;
  9. use common\helpers\Form;
  10. use common\helpers\Tool;
  11. use common\libs\logging\operate\provider\BatchProvider;
  12. use common\libs\logging\operate\provider\SingleProvider;
  13. use common\models\User;
  14. use SebastianBergmann\CodeCoverage\Report\PHP;
  15. use yii\base\Component;
  16. use yii\base\ErrorException;
  17. use yii\base\Exception;
  18. use yii\helpers\ArrayHelper;
  19. abstract class AbstractOperate extends Component {
  20. /**
  21. * 保存日志的model
  22. * @var null
  23. */
  24. public $logModel = null;
  25. /**
  26. * 保存日志的form
  27. * @var null
  28. */
  29. public $logForm = null;
  30. /**
  31. * 用于查询保存存数据的model
  32. * @var
  33. */
  34. public $fetchClass = null;
  35. /**
  36. * @var array
  37. */
  38. public $attrLabels = [];
  39. /**
  40. * @var null
  41. */
  42. public $saveBeforeContent = null;
  43. /**
  44. * @var null
  45. */
  46. public $saveAfterContent = null;
  47. /**
  48. * @var array
  49. */
  50. public $columns = [];
  51. /**
  52. * @var null
  53. */
  54. public $data = null;
  55. /**
  56. * @var null
  57. */
  58. protected $_dataType = null;
  59. /**
  60. * @var array
  61. */
  62. public $params = [];
  63. /**
  64. * @var bool
  65. */
  66. protected $_isBatch = false;
  67. /**
  68. * @var string
  69. */
  70. protected $_batchField = 'ID';
  71. /**
  72. * @var string
  73. */
  74. protected $_optObjField = 'USER_ID';
  75. /**
  76. * @var null
  77. */
  78. protected $_errors = null;
  79. /**
  80. * Factory constructor.
  81. * @param array $config
  82. */
  83. public function __construct($config = []) {
  84. parent::__construct($config);
  85. }
  86. /**
  87. * @param array $format
  88. * @param array $params
  89. * @return array
  90. */
  91. public function paramsFormat(array $format, array $params = []){
  92. if(!$params){
  93. return $format;
  94. }
  95. return ArrayHelper::merge($format, $params);
  96. }
  97. /**
  98. * @return |null
  99. */
  100. public function getDataType(){
  101. return $this->_dataType;
  102. }
  103. /**
  104. * @param $value
  105. * @return $this
  106. */
  107. public function setDataType($value){
  108. $this->_dataType = $value;
  109. return $this;
  110. }
  111. /**
  112. * @return mixed
  113. */
  114. public function getIsBatch(){
  115. return $this->_isBatch;
  116. }
  117. /**
  118. * @param $value
  119. * @return $this
  120. */
  121. public function setIsBatch($value){
  122. $this->_isBatch = $value;
  123. return $this;
  124. }
  125. /**
  126. * @param $value
  127. * @return $this
  128. */
  129. public function setBatchField($value){
  130. $this->_batchField = $value;
  131. return $this;
  132. }
  133. /**
  134. * @param $value
  135. * @return $this
  136. */
  137. public function setOptObjField($value){
  138. $this->_optObjField = $value;
  139. return $this;
  140. }
  141. /**
  142. * @param $value
  143. * @return $this
  144. */
  145. public function setSaveBeforeContent($value){
  146. $this->saveBeforeContent = $value;
  147. return $this;
  148. }
  149. /**
  150. * @param $value
  151. * @return $this
  152. */
  153. public function setSaveAfterContent($value){
  154. $this->saveAfterContent = $value;
  155. return $this;
  156. }
  157. /**
  158. * 分发器
  159. * @param $method
  160. * @return array|null
  161. */
  162. public function dispatcher($method){
  163. $initParams = $this->paramsFormat([
  164. 'data' => $this->data,
  165. 'fetchClass' => $this->fetchClass,
  166. 'dataType' => $this->_dataType,
  167. 'attrLabels' => $this->attrLabels,
  168. ], $this->params);
  169. if($this->_isBatch){
  170. $entryClass = new BatchProvider($initParams);
  171. }else{
  172. $entryClass = new SingleProvider($initParams);
  173. }
  174. if(method_exists($entryClass, $method)){
  175. $entryClass->{$method}();
  176. return $entryClass->result;
  177. }
  178. return null;
  179. }
  180. /**
  181. * 更新之前
  182. * @param null $data
  183. * @param null $dataType
  184. * @param array $params
  185. * @return $this|bool
  186. */
  187. public function beforeUpdate($data = null, $dataType = null, array $params = []){
  188. if(!$data){
  189. return false;
  190. }
  191. $this->data = $data;
  192. $this->_dataType = $dataType;
  193. $this->params = $params;
  194. $this->saveBeforeContent = $this->dispatcher('beforeUpdate');
  195. return $this;
  196. }
  197. /**
  198. * @param $data
  199. * @param null $dataType
  200. * @param array $params
  201. * @return $this|bool
  202. */
  203. public function afterUpdate($data, $dataType = null, array $params = []){
  204. if(!$data){
  205. return false;
  206. }
  207. $this->data = $data;
  208. $this->_dataType = $dataType;
  209. $this->params = $params;
  210. $this->saveAfterContent = $this->dispatcher('afterUpdate');
  211. return $this;
  212. }
  213. /**
  214. * @param $data
  215. * @param null $dataType
  216. * @param array $params
  217. * @return $this|bool
  218. */
  219. public function beforeDelete($data, $dataType = null, array $params = []){
  220. if(!$data){
  221. return false;
  222. }
  223. $this->data = $data;
  224. $this->params = $params;
  225. $this->_dataType = $dataType;
  226. $this->saveBeforeContent = $this->dispatcher('beforeDelete');
  227. return $this;
  228. }
  229. /**
  230. * @param $data
  231. * @param string $dataType
  232. * @param array $params
  233. * @return $this|bool
  234. */
  235. public function afterInsert($data, $dataType = 'ID', array $params = []){
  236. if(!$data || !$dataType){
  237. return false;
  238. }
  239. $this->data = $data;
  240. $this->params = $params;
  241. $this->_dataType = $dataType;
  242. $this->saveAfterContent = $this->dispatcher('afterInsert');
  243. return $this;
  244. }
  245. /**
  246. * @return $this
  247. */
  248. public function clean(){
  249. $this->columns = [];
  250. return $this;
  251. }
  252. /**
  253. * @param array $params
  254. * @return bool
  255. */
  256. public function save(array $params = []){
  257. $this->setColumn($params);
  258. if(!$this->columns){
  259. return false;
  260. }
  261. // $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle('log/save', [
  262. // 'logModel' => $this->logModel,
  263. // 'logForm' => $this->logForm,
  264. // 'isBatch' => $this->_isBatch,
  265. // 'batchField' => $this->_batchField,
  266. // 'optObjField' => $this->_optObjField,
  267. // 'columns' => $this->columns,
  268. // ]);
  269. // if ($taskKey === false) {
  270. // $this->setErrors('异步请求日志服务失败');
  271. // return false;
  272. // }
  273. if($this->_isBatch){
  274. $modelClass = $this->logModel;
  275. // mongodb 的批量添加要特殊处理
  276. $db = $modelClass::getDb();
  277. $collectionName = $modelClass::collectionName();
  278. if(is_array($collectionName)){
  279. $collection = count($collectionName) > 1 ? $collectionName[1] : $collectionName[0];
  280. }else{
  281. $collection = $collectionName;
  282. }
  283. $batchList = [];
  284. $saveAfterContent = $this->columns[0]['save_after_content'] ?? [];
  285. $saveBeforeContent = $this->columns[0]['save_before_content'] ?? [];
  286. if( $saveBeforeContent && $saveAfterContent ) {
  287. $saveBeforeContentList = [];
  288. foreach ($saveBeforeContent as $key => $beforeItem) {
  289. if (isset($beforeItem[$this->_batchField])) {
  290. $saveBeforeContentList[$beforeItem[$this->_batchField]['value']] = $beforeItem;
  291. } else {
  292. $saveBeforeContentList[$key] = $beforeItem;
  293. }
  294. }
  295. foreach ($saveAfterContent as $key => $afterItem) {
  296. if (isset($afterItem[$this->_batchField])) {
  297. if (!isset($saveBeforeContentList[$afterItem[$this->_batchField]['value']])) continue;
  298. $beforeItem = $saveBeforeContentList[$afterItem[$this->_batchField]['value']];
  299. } else {
  300. if (!isset($saveBeforeContentList[$key])) continue;
  301. $beforeItem = $saveBeforeContentList[$key];
  302. }
  303. $mergeData = [
  304. 'save_before_content' => $beforeItem,
  305. 'save_after_content' => $afterItem,
  306. ];
  307. //记录opt_obj_id 和 opt_obj_name @todo待优化
  308. $this->_fixOptObjField($collection, $beforeItem);
  309. $batchList[] = array_merge($this->columns[0], $mergeData);
  310. }
  311. unset($saveBeforeContentList);
  312. }else if( $saveBeforeContent && !$saveAfterContent ) {
  313. foreach ($saveBeforeContent as $key => $beforeItem) {
  314. $mergeData = [
  315. 'save_before_content' => $beforeItem,
  316. 'save_after_content' => null,
  317. ];
  318. //记录opt_obj_id 和 opt_obj_name @todo待优化
  319. $this->_fixOptObjField($collection, $beforeItem);
  320. $batchList[] = array_merge($this->columns[0], $mergeData);
  321. }
  322. }else if( !$saveBeforeContent && $saveAfterContent ) {
  323. foreach ($saveAfterContent as $key => $afterItem) {
  324. $mergeData = [
  325. 'save_before_content' => null,
  326. 'save_after_content' => $afterItem,
  327. ];
  328. //记录opt_obj_id 和 opt_obj_name @todo待优化
  329. $this->_fixOptObjField($collection, $afterItem);
  330. $batchList[] = array_merge($this->columns[0], $mergeData);
  331. }
  332. }else {
  333. $this->setErrors('日志批量写入错误');
  334. return false;
  335. }
  336. unset($saveBeforeContent, $saveAfterContent);
  337. if (!$batchList) {
  338. $this->setErrors('数据无效');
  339. return false;
  340. }
  341. if(!$db->createCommand()->batchInsert($collection, $batchList)){
  342. $this->setErrors('日志批量写入失败');
  343. return false;
  344. }
  345. // if(!$db->createCommand()->batchInsert($collection, $this->columns)){
  346. // $this->setErrors('日志批量写入失败');
  347. // return false;
  348. // }
  349. return true;
  350. }else {
  351. $modelFormClass = $this->logForm;
  352. $logForm = new $modelFormClass($this->columns[0]);
  353. if(!$logForm->add()){
  354. $this->setErrors($logForm->getErrors());
  355. return false;
  356. }
  357. }
  358. return true;
  359. }
  360. /**
  361. * 异步保存(异步控制器调用此方法)
  362. * @return bool
  363. */
  364. public function asyncSave() {
  365. try {
  366. if( !$this->columns || !isset($this->columns[0]) ) {
  367. throw new Exception('储存的数据错误');
  368. }
  369. if ($this->_isBatch) {
  370. $this->_batchInsert();
  371. } else {
  372. $modelFormClass = $this->logForm;
  373. $logForm = new $modelFormClass($this->columns[0]);
  374. if (!$logForm->add()) {
  375. throw new Exception(Form::formatErrorsForApi($logForm->getErrors()));
  376. }
  377. }
  378. return true;
  379. }catch (\Exception $e) {
  380. $this->setErrors($e->getMessage());
  381. return false;
  382. }
  383. }
  384. /**
  385. * console里面记日志,直接保存,无需调用异步,因为本身就是异步
  386. * @param array $params
  387. * @return bool
  388. */
  389. public function saveByConsole(array $params = []){
  390. $this->setColumn($params);
  391. if(!$this->columns){
  392. return false;
  393. }
  394. try {
  395. if( !$this->columns || !isset($this->columns[0]) ) {
  396. throw new Exception('储存的数据错误');
  397. }
  398. if ($this->_isBatch) {
  399. $this->_batchInsert();
  400. } else {
  401. $modelFormClass = $this->logForm;
  402. $logForm = new $modelFormClass($this->columns[0]);
  403. if (!$logForm->add()) {
  404. throw new Exception(Form::formatErrorsForApi($logForm->getErrors()));
  405. }
  406. }
  407. }catch (\Exception $e) {
  408. $this->setErrors($e->getMessage());
  409. return false;
  410. }
  411. return true;
  412. }
  413. /**
  414. * 批量写入
  415. * @return bool
  416. * @throws ErrorException
  417. * @throws \yii\base\InvalidConfigException
  418. */
  419. protected function _batchInsert() {
  420. $modelClass = $this->logModel;
  421. // mongodb 的批量添加要特殊处理
  422. $db = $modelClass::getDb();
  423. $collectionName = $modelClass::collectionName();
  424. if(is_array($collectionName)){
  425. $collection = count($collectionName) > 1 ? $collectionName[1] : $collectionName[0];
  426. }else{
  427. $collection = $collectionName;
  428. }
  429. $batchList = [];
  430. $saveAfterContent = $this->columns[0]['save_after_content'] ?? [];
  431. $saveBeforeContent = $this->columns[0]['save_before_content'] ?? [];
  432. if( $saveBeforeContent && $saveAfterContent ) {
  433. $saveBeforeContentList = [];
  434. foreach ($saveBeforeContent as $key => $beforeItem) {
  435. if (isset($beforeItem[$this->_batchField])) {
  436. $saveBeforeContentList[$beforeItem[$this->_batchField]['value']] = $beforeItem;
  437. } else {
  438. $saveBeforeContentList[$key] = $beforeItem;
  439. }
  440. }
  441. foreach ($saveAfterContent as $key => $afterItem) {
  442. if (isset($afterItem[$this->_batchField])) {
  443. if (!isset($saveBeforeContentList[$afterItem[$this->_batchField]['value']])) continue;
  444. $beforeItem = $saveBeforeContentList[$afterItem[$this->_batchField]['value']];
  445. } else {
  446. if (!isset($saveBeforeContentList[$key])) continue;
  447. $beforeItem = $saveBeforeContentList[$key];
  448. }
  449. $mergeData = [
  450. 'save_before_content' => $beforeItem,
  451. 'save_after_content' => $afterItem,
  452. ];
  453. //记录opt_obj_id 和 opt_obj_name @todo待优化
  454. $this->_fixOptObjField($collection, $beforeItem);
  455. $batchList[] = array_merge($this->columns[0], $mergeData);
  456. }
  457. unset($saveBeforeContentList);
  458. }else if( $saveBeforeContent && !$saveAfterContent ) {
  459. foreach ($saveBeforeContent as $key => $beforeItem) {
  460. $mergeData = [
  461. 'save_before_content' => $beforeItem,
  462. 'save_after_content' => null,
  463. ];
  464. //记录opt_obj_id 和 opt_obj_name @todo待优化
  465. $this->_fixOptObjField($collection, $beforeItem);
  466. $batchList[] = array_merge($this->columns[0], $mergeData);
  467. }
  468. }else if( !$saveBeforeContent && $saveAfterContent ) {
  469. foreach ($saveAfterContent as $key => $afterItem) {
  470. $mergeData = [
  471. 'save_before_content' => null,
  472. 'save_after_content' => $afterItem,
  473. ];
  474. //记录opt_obj_id 和 opt_obj_name @todo待优化
  475. $this->_fixOptObjField($collection, $afterItem);
  476. $batchList[] = array_merge($this->columns[0], $mergeData);
  477. }
  478. }else {
  479. throw new ErrorException('日志批量写入错误');
  480. }
  481. unset($saveBeforeContent, $saveAfterContent);
  482. if (!$batchList) {
  483. throw new ErrorException('数据无效' . var_export($this->columns, true));
  484. }
  485. if(!$db->createCommand()->batchInsert($collection, $batchList)){
  486. throw new ErrorException('日志批量写入失败');
  487. }
  488. return true;
  489. }
  490. /**
  491. * 修正批量时的补操作对象信息
  492. * @param $collection
  493. * @param $item
  494. * @return bool
  495. * @throws \yii\base\InvalidConfigException
  496. */
  497. private function _fixOptObjField($collection, $item) {
  498. if( $collection !== 'ar_bonus_admin_log') {
  499. return false;
  500. }
  501. if( !isset( $item[$this->_optObjField] ) ) {
  502. return false;
  503. }
  504. $this->columns[0]['opt_obj_id'] = $item[$this->_optObjField]['value'];
  505. if( isset( $item['USER_NAME'] ) ) {
  506. $this->columns[0]['opt_obj_name'] = $item['USER_NAME']['value'];
  507. }else {
  508. //@todo
  509. $userOne = User::find()->select(['USER_NAME'])->where('ID=:ID', ['ID'=>$item[$this->_optObjField]['value']])->asArray()->one();
  510. if( $userOne && isset($userOne['USER_NAME']) ) {
  511. $this->columns[0]['opt_obj_name'] = $userOne['USER_NAME'];
  512. }
  513. }
  514. return true;
  515. }
  516. /**
  517. * @param $error
  518. * @return $this
  519. */
  520. public function setErrors($error){
  521. $this->_errors = $error;
  522. return $this;
  523. }
  524. /**
  525. * @return array|null
  526. */
  527. public function getErrors(){
  528. return $this->_errors;
  529. }
  530. }