<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\facade\Db;

class UpdateProcessWaitFinallyActionData extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('UpdateProcessWaitFinallyActionData')
            ->setDescription('更新待办数据表中的字段:最终操作人');
    }

    protected function execute(Input $input, Output $output)
    {
        //判断是否在执行中


        Db::startTrans();
        try {

            //设置标识,执行中

            Db::name('process_wait')
                ->where('action_uid_finally','<>',0)
                ->update(['action_uid_finally'=>0,'action_name_finally'=>'']);

            $data = Db::name('process_wait')
                ->field('order_code,order_type,order_id')
                ->group('order_code,order_type,order_id')
                ->whereIn('status', [1, 2])
                ->cursor();

            foreach ($data as $value) {

                if($value['order_type'] == 'LZJJ') $order_code = $value['order_id'];//离职交接单,取id
                else $order_code = $value['order_code'];

                $action = Db::name('action_log')
                    ->field('id,action_id,action_name')
                    ->where([
                        'order_code'=>$order_code,
                        'order_type'=>$value['order_type']
                    ])
                    ->order('id','desc')
                    ->findOrEmpty();

                if ($action) Db::name('process_wait')
                    ->where('order_code', $value['order_code'])
                    ->where('order_type', $value['order_type'])
                    ->update(['action_uid_finally' => $action['action_id'],'action_name_finally' => $action['action_name']]);
            }

            Db::commit();

            $output->writeln('update success');

            //解除标识,执行完毕

        } catch (Exception $exception) {
            Db::rollback();
            $output->writeln('update fail,' . $exception->getMessage());
            //解除标识,执行完毕

        }


    }


}