Maintain.php 11 KB

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