Team.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * 使用定时器将符合条件的团队成员移出团队
  4. *
  5. * @author guogaobo
  6. * @since 2021-05-20
  7. */
  8. namespace app\common\command;
  9. use think\Config;
  10. use think\console\Command;
  11. use think\console\Input;
  12. use think\console\input\Argument;
  13. use think\console\input\Option;
  14. use think\console\Output;
  15. use think\Db;
  16. use think\response\Json;
  17. use Workerman\Lib\Timer;
  18. use Workerman\Worker;
  19. class Team extends Command
  20. {
  21. protected $timer;
  22. protected $interval = 10;
  23. protected function configure()
  24. {
  25. $this->setName('team')
  26. ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
  27. ->addOption('d', null, Option::VALUE_NONE, 'daemon(守护进程)方式启动')
  28. ->setDescription('团队成员移出定时器');
  29. // 读取数据库配置文件
  30. $filename = ROOT_PATH . 'config' . DS . 'database.php';
  31. // 重新加载数据库配置文件
  32. Config::load($filename, 'database');
  33. }
  34. /**
  35. * 初始化
  36. *
  37. * @param Input $input
  38. * @param Output $output
  39. */
  40. protected function init(Input $input, Output $output)
  41. {
  42. global $argv;
  43. $argv[1] = $input->getArgument('status') ?: 'start';
  44. if ($input->hasOption('d')) {
  45. $argv[2] = '-d';
  46. } else {
  47. unset($argv[2]);
  48. }
  49. }
  50. /**
  51. * 停止定时器
  52. */
  53. public function stop()
  54. {
  55. Timer::del($this->timer);
  56. }
  57. /**
  58. * 启动定时器
  59. */
  60. public function start()
  61. {
  62. $this->timer = Timer::add(1, function () {
  63. # 只在凌晨12点至6点间执行
  64. if ((int)date('H') >= 0 && (int)date('H') < 6) {
  65. # 团队成员过滤规则
  66. db('crm_team')->where('target_time',0)->delete();
  67. $ruleList = db('crm_team')
  68. ->where('target_time', '<', time())->select();
  69. if (!empty($ruleList)) {
  70. Db::startTrans();
  71. try {
  72. foreach ($ruleList as $v) {
  73. switch ($v['types']) {
  74. case 1 :
  75. $data_name = 'customer_id';
  76. $types = 'crm_customer';
  77. $typesName = '客户';
  78. break;
  79. case 2 :
  80. $data_name = 'contacts_id';
  81. $types = 'crm_contacts';
  82. $typesName = '联系人';
  83. break;
  84. case 3 :
  85. $data_name = 'business_id';
  86. $types = 'crm_business';
  87. $typesName = '商机';
  88. break;
  89. case 4 :
  90. $data_name = 'contract_id';
  91. $types = 'crm_contract';
  92. $typesName = '合同';
  93. break;
  94. case 5 :
  95. $data_name = 'receivables_id';
  96. $types = 'crm_receivables';
  97. $typesName = '回款';
  98. break;
  99. }
  100. $resData = db($types)->where([$data_name => $v['target_id']])->field('name,rw_user_id,ro_user_id')->find();
  101. if ($v['types'] == 5) {
  102. $resData = db($types)->where([$data_name => $v['target_id']])->field('number as name,rw_user_id,ro_user_id')->find();
  103. }
  104. $team_user_id = array_column($ruleList, 'team_user_id');
  105. $old_rw_user_id = !empty($resData['rw_user_id']) ? explode(',', trim($resData['rw_user_id'], ',')) : []; //去重
  106. //只读
  107. $old_ro_user_id = !empty($resData['ro_user_id']) ? explode(',', trim($resData['ro_user_id'], ',')) : []; //去重
  108. if ($v['auth'] == 1) {
  109. $all_rw_user_id = $team_user_id ? array_diff($old_ro_user_id, $team_user_id) : ''; // 差集
  110. $data['ro_user_id'] = $all_rw_user_id ? ',' . implode(',', $all_rw_user_id) . ',' : ''; //去空
  111. } else {
  112. $all_ro_user_id = $team_user_id ? array_diff($old_rw_user_id, $team_user_id) : ''; // 差集
  113. $data['rw_user_id'] = $all_ro_user_id ? ',' . implode(',', $all_ro_user_id) . ',' : ''; //去空 ;
  114. }
  115. $upData = db($types)->where([$data_name => $v['target_id']])->update($data);
  116. db('crm_team')->where(['target_id' => $v['target_id'], 'types' => $v['types'], 'team_user_id' => ['in', arrayToString($team_user_id)]])->delete();
  117. }
  118. Db::commit();
  119. } catch (\Exception $e) {
  120. Db::rollback();
  121. }
  122. }
  123. }
  124. });
  125. }
  126. protected function execute(Input $input, Output $output)
  127. {
  128. # 动态修改运行时参数
  129. set_time_limit(0);
  130. ini_set('memory_limit', '512M');
  131. $this->init($input, $output);
  132. # 创建定时器任务
  133. $worker = new Worker();
  134. $worker->name = 'team';
  135. $worker->count = 1;
  136. $worker->onWorkerStart = [$this, 'start'];
  137. $worker->runAll();
  138. }
  139. }