Maintain.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace tw\command;
  3. use app\admin\model\store\StoreProduct;
  4. use app\admin\model\store\StoreProductAttr;
  5. use app\admin\model\store\StoreProductAttrResult;
  6. use app\admin\model\store\StoreProductAttrValue;
  7. use app\admin\model\system\SystemLog;
  8. use app\models\store\StoreProductRule;
  9. use think\console\Command;
  10. use think\console\Input;
  11. use think\console\input\Argument;
  12. use think\console\Output;
  13. use \think\facade\Config;
  14. use MeiliSearch\Client;
  15. /**
  16. * 维护命令,使用 nginx 帐号运行
  17. *
  18. * sudo -u http php think maintain ***
  19. */
  20. class Maintain extends Command
  21. {
  22. const PREV_YEAR = 21; // 活动的上一年标志。比如`幸运2021` 更新为 `幸运2022`,这个值就是 21
  23. protected $NEW_YEAR; // new year value
  24. protected $LUCKY;
  25. protected function configure()
  26. {
  27. $this->setName('maintain')
  28. ->addArgument('category', Argument::REQUIRED, 'act|prod|trash|reindex|none')
  29. ->setDescription('maintain some application data.');
  30. }
  31. protected function init(Input $input, Output $output)
  32. {
  33. global $argv;
  34. $argv[1] = $input->getArgument('category') ?? 'none';
  35. }
  36. protected function execute(Input $input, Output $output)
  37. {
  38. $category = $input->getArgument('category');
  39. if (is_callable([$this, $category])) {
  40. $this->$category();
  41. } else {
  42. console::log('bad argument ', $category);
  43. }
  44. }
  45. /**
  46. * 修改幸运 2021 后面的 2021
  47. *
  48. * IMPORTANT:
  49. * 修改步骤:
  50. * 1. 修改 eb_store_category 中分类名称
  51. * 2. 修改 congit/activity 中配置
  52. * 3. 执行 php think maintain act
  53. */
  54. protected function act()
  55. {
  56. console::log(__FUNCTION__);
  57. $act_parts = Config::get('activity.lucky_spec_items', []);
  58. if (is_array($act_parts) && count($act_parts) > 1) {
  59. $this->NEW_YEAR = intval($act_parts[1]);
  60. } else {
  61. console::log('bad config: activity.lucky_spec_items. quit.');
  62. return;
  63. }
  64. $act_name = Config::get('activity.lucky_spec_name', '');
  65. $this->LUCKY = $act_name;
  66. $this->_update_StoreProductAttr();
  67. $this->_update_StoreProductAttrValue();
  68. $this->_update_StoreProductAttrResult();
  69. $this->_udpate_StoreProductRule();
  70. }
  71. /**
  72. * 修改
  73. */
  74. private function _update_StoreProductAttr()
  75. {
  76. $rows = StoreProductAttr::getLuckyAttrs($this->LUCKY);
  77. $changed = false;
  78. foreach ($rows as &$row) {
  79. $changed = false;
  80. //
  81. if (
  82. is_array($row['attr_values']) &&
  83. count($row['attr_values']) > 1 &&
  84. intval($row['attr_values'][1]) == self::PREV_YEAR
  85. ) {
  86. $row['attr_values'][1] = $this->NEW_YEAR;
  87. $changed = true;
  88. }
  89. // sql
  90. if ($changed) {
  91. try {
  92. $data['attr_values'] = implode(',', $row['attr_values']);
  93. // console::log($row);
  94. $ok = StoreProductAttr::where('product_id', $row['product_id'])
  95. ->where('attr_name', $row['attr_name'])
  96. ->update($data);
  97. if (!$ok) {
  98. console::log('execute sql failed.');
  99. }
  100. } catch (\Exception $e) {
  101. console::log('execute sql exception:', $e->getMessage());
  102. }
  103. }
  104. } // foreach
  105. console::log('table StoreProductAttr updated.');
  106. }
  107. private function _update_StoreProductAttrValue()
  108. {
  109. $rows = StoreProductAttrValue::where('suk', 'like', '%' . self::PREV_YEAR . '%')->select()->toArray();
  110. foreach ($rows as $row) {
  111. $changed = false;
  112. $suks = explode(',', $row['suk']); // array
  113. // replace
  114. for ($i = 0; $i < count($suks); $i++) {
  115. if (intval($suks[$i]) == self::PREV_YEAR) {
  116. $suks[$i] = $this->NEW_YEAR;
  117. $changed = true;
  118. }
  119. }
  120. if (!$changed) {
  121. continue;
  122. }
  123. // $data['unique'] = sp_random_string(); // $row['unique'];
  124. $data['suk'] = implode(',', $suks); // string again
  125. try {
  126. $ok = StoreProductAttrValue::edit($data, $row['unique'], 'unique');
  127. if (!$ok) {
  128. console::log('update StoreProductAttrValue failed.', $ok);
  129. }
  130. } catch (\Exception $e) {
  131. console::log('update StoreProductAttrValue exception:', $e->getMessage());
  132. }
  133. }
  134. }
  135. private function _update_StoreProductAttrResult()
  136. {
  137. $rows = StoreProductAttrResult::where("1=1")->field('product_id,result')->select()->toArray();
  138. $changed = false;
  139. foreach ($rows as $row) {
  140. $changed = false;
  141. $result = json_decode($row['result'], true);
  142. if (isset($result['attr']) && count($result['attr']) > 0) {
  143. foreach ($result['attr'] as &$attr) {
  144. if (!isset($attr['value']) || $attr['value'] != $this->LUCKY) {
  145. continue;
  146. }
  147. if (isset($attr['detail']) && count($attr['detail']) > 0) {
  148. for ($i = 0; $i < count($attr['detail']); $i++) {
  149. // update
  150. if (intval($attr['detail'][$i]) == self::PREV_YEAR) {
  151. $attr['detail'][$i] = strval($this->NEW_YEAR);
  152. $changed = true;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. if (isset($result['value']) && count($result['value']) > 0) {
  159. foreach ($result['value'] as &$value) {
  160. foreach ($value as $k => $v) {
  161. if (substr($k, 0, 5) == 'value' && intval($v) == self::PREV_YEAR) {
  162. $value[$k] = strval($this->NEW_YEAR);
  163. $changed = true;
  164. } else if ($k == 'detail') {
  165. foreach ($v as $kk => $vv) {
  166. // update
  167. if ($kk == $this->LUCKY && intval($vv) == self::PREV_YEAR) {
  168. $value[$k][$kk] = strval($this->NEW_YEAR);
  169. $changed = true;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. if (!$changed) {
  177. continue;
  178. }
  179. try {
  180. $data['result'] = json_encode($result);
  181. // console::log($row['product_id']);
  182. $ok = StoreProductAttrResult::where('product_id', $row['product_id'])
  183. ->update($data);
  184. if (!$ok) {
  185. console::log('sql execute error:', StoreProductAttrResult::getErrorInfo());
  186. }
  187. } catch (\Exception $e) {
  188. console::log('exception:', $e->getMessage());
  189. }
  190. }
  191. }
  192. private function _udpate_StoreProductRule()
  193. {
  194. $rows = StoreProductRule::where('rule_name', 'like', '%' . $this->LUCKY . '%')->select()->toArray();
  195. $changed = false;
  196. foreach ($rows as $row) {
  197. $changed = false;
  198. $values = json_decode($row['rule_value'], true);
  199. if (is_array($values)) {
  200. foreach ($values as &$value) {
  201. if (!isset($value['value']) && $value['value'] != $this->LUCKY) {
  202. continue;
  203. }
  204. if (
  205. isset($value['detail']) &&
  206. is_array($value['detail']) &&
  207. count($value['detail']) > 0
  208. ) {
  209. for ($i = 0; $i < count($value['detail']); $i++) {
  210. if (intval($value['detail'][$i]) == self::PREV_YEAR) {
  211. $value['detail'][$i] = strval($this->NEW_YEAR);
  212. $changed = true;
  213. }
  214. }
  215. }
  216. }
  217. }
  218. if (!$changed) {
  219. continue;
  220. }
  221. $data['rule_value'] = json_encode($values);
  222. try {
  223. console::log($values);
  224. $ok = StoreProductRule::where('id', $row['id'])->update($data);
  225. if (!$ok) {
  226. console::log('StoreProductRule failed:', StoreProductRule::getErrorInfo());
  227. }
  228. } catch (\Exception $e) {
  229. console::log('StoreProductRule exception:', $e->getMessage());
  230. }
  231. }
  232. }
  233. /**
  234. * 让一定时间之前的商品下架
  235. */
  236. protected function prod()
  237. {
  238. $now = time();
  239. $days = Config::get('app.off_days_before', 30);
  240. $days_ago = $now - $days * SECONDS_OF_ONEDAY;
  241. // 找到过期商品
  242. $products = StoreProduct::where([
  243. 'is_del' => 0,
  244. 'is_show' => 1,
  245. ])->where('add_time', '<', $days_ago)
  246. ->select()->toArray();
  247. // 下架
  248. $ids = [];
  249. foreach ($products as $p) {
  250. array_push($ids, $p['id']);
  251. }
  252. $ret = Storeproduct::where('id', 'in', $ids)->update([
  253. 'is_show' => 0,
  254. ]);
  255. // 记录日志
  256. warnlog("auto off-shelves executed: $ret" . json_encode($ids));
  257. }
  258. /**
  259. * 删除过期日志等数据
  260. */
  261. protected function trash()
  262. {
  263. echo getRate(5);
  264. }
  265. /**
  266. * 备份数据库
  267. *
  268. * 后台备份功能超出内存限制
  269. */
  270. protected function backup()
  271. {
  272. $db = config('database.connections.' . config('database.default'));
  273. $user = $db['username'];
  274. $password = $db['password'];
  275. $host = $db['hostname'];
  276. $database = $db['database'];
  277. $now = time();
  278. $dir = "backup-$now.sql";
  279. exec("mysqldump --user={$user} --password={$password} --host={$host} {$database} --result-file={$dir} 2>&1", $output);
  280. warnlog('backup database:' . json_encode($output));
  281. }
  282. /**
  283. * 重新索引商品
  284. *
  285. * 1.用于常见的商品分类展示。
  286. * 2.用于关键字搜索
  287. * 3.用于按价格/销量/好评度/上线时间排序
  288. *
  289. * - 新增/上架商品: 新增索引
  290. * - 删除/下架商品:删除索引
  291. */
  292. protected function reindex()
  293. {
  294. }
  295. protected function none()
  296. {
  297. }
  298. }