ResourceAutoRecovery.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use app\model\Customer;
  7. use app\model\Setting;
  8. use app\model\CrmImportLog;
  9. use think\facade\Log;
  10. class ResourceAutoRecovery extends Command
  11. {
  12. protected function configure()
  13. {
  14. $this->setName('resource_auto_recovery')
  15. ->setDescription('resource_auto_recovery to resource pool');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. Log::close(NULL);
  20. $recovery_list = Setting::where([['name','=','auto_recovery'],['state','=',1],['content','<>',0]])->select()->toArray();
  21. if (empty($recovery_list)) return $output->writeln("没有要执行的任务");
  22. foreach($recovery_list as $key=>$val){
  23. $orgids = orgSubIds($val['root_id']);
  24. $endtime = date('Y-m-d H:i:s',(time() - (int)$val['content'] * 86400));
  25. $where[] = ['org_id','in',$orgids];
  26. $where[] = ['crm_res_id','>',0];
  27. $where[] = ['employee_id','>',0];
  28. $where[] = ['employee_time','<=',$endtime];
  29. $where[] = ['state','in',Customer::changeState('待确认','chaos')];
  30. $cuslist = Customer::where($where)->field('id,name,crm_res_id,state')->select()->toArray();
  31. if(!empty($cuslist)) $this->recovery_cus($cuslist);
  32. }
  33. $output->writeln("回收至资源库完成");
  34. }
  35. /**
  36. * 处理员工需要回收的资源
  37. */
  38. public function recovery_cus($cuslist)
  39. {
  40. foreach($cuslist as $key=>$val){
  41. $crm_log = CrmImportLog::where('id', $val['crm_res_id'])->find();
  42. $left_num = $crm_log->left_num + 1;
  43. $crm_log->left_num = $left_num ? $left_num : 0;
  44. $crm_log->save();
  45. $is_distribution = $crm_log->pid ? 1 : 0;
  46. Customer::where([['id', '=', $val['id']]])->update(['employee_id' => null, 'is_resource' => 1, 'fresh' => 1, 'employee_time'=> null ,'is_distribution' => $is_distribution,'state'=> 0]);
  47. }
  48. }
  49. }