<?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 UpdateProcessWaitFinallyData extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('UpdateProcessWaitFinallyData')
            ->setDescription('更新待办数据表中的字段:最终状态值');
    }

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



        Db::startTrans();
        try {

            //设置标识,执行中


            $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) {

                $order_process_finally = 0;

                switch ($value['order_type']) {
                    //1.采购单流程
                    case 'CGD':
                        $order_process_finally = Db::name('purchease_order')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //2.采购单入库流程
                    case 'RKD':
                        $order_process_finally = Db::name('purchease_in')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //3.备货申请单流程
                    case 'BHD':
                        $order_process_finally = Db::name('purchease')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //4.销售订单流程
                    case 'XSQRD':
                        $order_process_finally = Db::name('sale')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //5.发货申请单流程
                    case 'CKD':
                        $order_process_finally = Db::name('order_out')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //6.售后申请单流程
                    case 'SHD':
                        $order_process_finally = Db::name('order_return')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //7.售后退货单流程
                    case 'CKTHD':
                        $order_process_finally = Db::name('order_back')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //8.销售退货单流程
                    case 'XSTHD':
                        $order_process_finally = Db::name('sale_return')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //9.采购工差单流程
                    case 'CGGCD':
                        $order_process_finally = Db::name('purchease_diff')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //10.销售工差单流程
                    case 'XSGCD':
                        $order_process_finally = Db::name('sale_diff')
                            ->where(['id' => $value['order_id']])
                            ->value('status', 0);
                        break;
                    //11.调拨单流程
                    case 'DBD':
                        $order_process_finally = Db::name('allot_stock')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //12.盘点单流程
                    case 'PDD':
                        $order_process_finally = Db::name('good_check')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //13.议价单流程
                    case 'YJD':
                        $order_process_finally = Db::name('bargain_order')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //14.项目单流程
                    case 'PRO':
                        $order_process_finally = Db::name('project')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //15.商品成本流程
                    case 'SPCB':
                        $order_process_finally = Db::name('good_basic')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //16.商品上线流程
                    case 'SPSX':
                        $order_process_finally = Db::name('good_platform')
                            ->where(['id' => $value['order_id']])
                            ->value('exam_status', 0);
                        break;
                    //17.活动流程
                    case 'HD':
                        $order_process_finally = Db::name('good_activity')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //19.有赞商品上架流程
                    case 'YZSX':
                        $order_process_finally = Db::name('platform_youzan')
                            ->where(['id' => $value['order_id']])
                            ->value('exam_status', 0);
                        break;
                    //20.有赞订单审核流程
//                case 'YZQRD':
//                    $order_process_finally = Db::name('')->where(['id' => $value['order_id'], 'is_del' => 0])->value('', 0);
//                    break;
                    //22.离职交接流程
                    case 'LZJJ':
                        $order_process_finally = Db::name('resign_info')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //23.采购单退货流程
                    case 'CGTHD':
                        $order_process_finally = Db::name('purchease_return')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //24.竞价单流程
                    case 'ZXD':
                        $order_process_finally = Db::name('consult_order')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
                        break;
                    //25.商品下线流程
//                    case 'GOL':
//                        $order_process_finally = Db::name('good_offline_log')->where(['id' => $value['order_id'], 'is_del' => 0])->value('status', 0);
//                        break;
                }

                if ($order_process_finally) Db::name('process_wait')
                    ->where('order_code', $value['order_code'])
                    ->where('order_type', $value['order_type'])
                    ->update(['order_process_finally' => $order_process_finally]);
            }

            Db::commit();

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

            //解除标识,执行完毕

        } catch (Exception $exception) {
            Db::rollback();
            $output->writeln('update fail,' . $exception->getMessage());

            //解除标识,执行完毕


        }


    }


}