UpdateCustom.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace app\admin\command;
  3. use app\common\library\Rabbitmq;
  4. use app\common\library\Ssdb;
  5. use PhpAmqpLib\Message\AMQPMessage;
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\Output;
  9. use think\Log;
  10. use think\Request;
  11. class UpdateCustom extends Command
  12. {
  13. protected function configure()
  14. {
  15. $this->setName('UpdateCustom')
  16. ->setDescription('刷新Custom的MessageJson数据');
  17. }
  18. /**
  19. * 入口方法
  20. *
  21. * @param Input $input
  22. * @param Output $output
  23. * @return int|null|void
  24. */
  25. protected function execute(Input $input, Output $output)
  26. {
  27. //cli模式下无法获取到当前的项目模块,手动指定一下
  28. Request::instance()->module('admin');
  29. $total = 0;
  30. $map['id'] = ['>=',1];
  31. //一次最多转移100个
  32. while($list = model('Custom')->where($map)->limit(10)->column('id,message_json')){
  33. $keys = array_keys($list);
  34. $map['id'] = ['>',end($keys)];
  35. if($list){
  36. foreach($list as $key => $val){
  37. $json = is_array($val) == true ? $val : json_decode($val);
  38. if($json){
  39. if(!is_array($json)){
  40. $json = json_decode($json);
  41. }
  42. $data = array_shift($json);
  43. $data = json_encode(array($data),JSON_UNESCAPED_UNICODE);
  44. if(false !== model('Custom')->where('id',$key)->update(['message_json'=>'"'.addslashes($data).'"'])){
  45. $total++;
  46. $output->info('数据更新成功,ID:'.$key.' Source:'.json_encode($val).' Update:'.$data);
  47. }else{
  48. $output->info('数据更新失败,ID:'.$key.' 写入失败');
  49. }
  50. }else{
  51. $output->error('数据更新失败,MAP:'.var_export($map,true).' 空数据');
  52. }
  53. }
  54. }
  55. usleep(1000);
  56. }
  57. $output->info('数据更新结束,共计更新数据:'.$total.'条');
  58. }
  59. }