12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace app\command;
- use app\event\Msg;
- use app\model\Customer;
- use app\model\CustomerNotVisit;
- use app\model\CustomerVisitLog;
- use app\model\Employee;
- use app\model\Org;
- use think\console\Command;
- use think\console\input\Argument;
- use think\console\Input;
- use think\console\input\Option;
- use think\console\Output;
- //勋章管理
- class SetNotVisitCustomers extends Command
- {
- protected function configure()
- {
- $this->setName('set_not_visit_customers')
- ->addOption('date', 'd', Option::VALUE_OPTIONAL, "要统计的日期")
- ->setDescription('未回访客户提醒');
- }
- protected function execute(Input $input, Output $output)
- {
- if ($input->hasOption('date')) {
- $date = date('Y-m-d', strtotime($input->getOption('date')));
- } else {
- $date = date('Y-m-d');
- }
- $exit = CustomerNotVisit::where(['appoint_date' => $date])->find();
- if ($exit) return $output->writeln($date . '已记录完毕!无需重复计入。');
- // 查询预约到当前时间要回访的客户
- $appointVisit = CustomerVisitLog::where(['next_contact_date' => $date])
- ->group('customer_id')
- ->column('count(id)', 'customer_id');
- if (empty($appointVisit)) return $output->writeln($date . '已记录完毕!0条数据被录入');
- // 查询实际回访次数
- $actualVisit = CustomerVisitLog::where([['addtime', 'like', $date . '%']])
- ->group('customer_id')
- ->column('count(id)', 'customer_id');
- // 预约为回访客户提取
- $notVisitCustomerIds = [];
- foreach ($appointVisit as $customerId => $num) {
- if (!isset($actualVisit[$customerId]) || $actualVisit[$customerId] < $num) $notVisitCustomerIds[] = $customerId;
- }
- if (empty($notVisitCustomerIds)) return $output->writeln($date . '已记录完毕!0条数据被录入');
- // 获取客户数据
- $customers = Customer::where([['id', 'in', $notVisitCustomerIds]])
- ->field('id as customer_id,employee_id,designer_id,org_id,"' . $date . '" as appoint_date', 'state')
- ->select();
- // 数据整理,用于消息发送
- $org = [];
- foreach ($customers as $customer) {
- isset($org[$customer['org_id']]) ? $org[$customer['org_id']]++ : $org[$customer['org_id']] = 1;
- }
- // 获取部门信息 将数量向上统计
- $org1 = [];
- $orgList = Org::where([['id', 'in', array_keys($org)]])->column('path', 'id');
- foreach ($orgList as $orgid => $path) {
- $lids = explode('-', trim($path, '-'));
- foreach ($lids as $i) {
- isset($org1[$i]) ? $org1[$i] += $org[$orgid] : $org1[$i] = $org[$orgid];
- }
- }
- // 保存数据
- (new CustomerNotVisit())->saveAll($customers->toArray());
- // 发送消息
- foreach ($org1 as $id => $num) {
- $leaders = Employee::where([['org_id', '=', $id], ['is_manager', '=', 1], ['uid', '<>', 0], ['state', '=', '在职']])->column('id');
- foreach ($leaders as $lid) {
- event(new Msg($lid, '今日有' . $num . '个客户未及时跟进,点击查看未跟进客户名单', 'notVisit'));
- }
- }
- $output->writeln($date . '已记录完毕!' . count($customers) . '条数据录入');
- }
- }
|