<?php

namespace app\admin\model;

use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
use app\abutment\model\SupplierUser as SupplierUserModel;
use think\facade\Db;
use think\Model;

//待办已办
class ProcessWait extends Model
{

    protected $table = 'wsm_process_wait';
    protected $pk = 'id';
    protected $autoWriteTimestamp = false;

    public static $status_wait = 1;//状态,1待处理
    public static $status_finish = 2;//状态,2已处理
    public static $status_interrupt = 3;//状态,3中断

    //添加待办记录
    public static function add(array $data = [], int $wait_id = 0, string $wait_name = '', string $handle_user_list = '')
    {
//        $data数据格式实例
//        $data=[
//            "order_type"=>'',
//            "order_code"=>'',
//            "order_id"=>'',
//            "order_status"=>'',
//            "action_process"=>'',
//            "action_status"=>'',
//            "action_uid"=>'',
//            "action_name"=>'',
//            "addtime"=>'',
//        ];

        //修改各个节点的最终节点
        Db::name('process_wait')
            ->where(['order_type' => $data['order_type'], 'order_code' => $data['order_code'], 'order_id' => $data['order_id']])
            ->update(['order_process_finally' => $data['action_process'], 'action_uid_finally' => $data['action_uid'], 'action_name_finally' => $data['action_name']]);

        //把上一个节点改成已完成
        Db::name('process_wait')
            ->where(['order_type' => $data['order_type'], 'order_process' => $data['action_status'], 'order_code' => $data['order_code'], 'order_id' => $data['order_id'], 'status' => self::$status_wait])
            ->update(['status' => self::$status_finish, 'updatetime' => date('Y-m-d H:i:s')]);

        //查询流程下该节点值的id
        $info = Db::name('process')
            ->alias('a')
            ->field('p.id,p.action_type,p.operation_type,a.api_url')
            ->join('action_process p', 'p.process_id=a.id AND p.order_process=' . $data['action_process'])
            ->where(['a.process_type' => $data['order_type'], 'a.status' => Process::$status_normal, 'a.is_del' => Process::$is_del_normal])
            ->findOrEmpty();

        if ($info) {

            //如果下一个节点是中断节点,则将相关记录删除,不再新增(只删除待办数据,221010145800@戴确认)
            if ($info['action_type'] == ActionProcess::$action_type_interrupt) {
                self::where(['order_type' => $data['order_type'], 'order_code' => $data['order_code'], 'order_id' => $data['order_id'], 'status' => self::$status_wait])
                    ->update(['status' => self::$status_interrupt, 'updatetime' => date('Y-m-d H:i:s')]);
                return true;
            }

            //如果是系统节点,不写入待办已办数据
            if ($info['operation_type'] == ActionProcess::$operation_type_system) return true;

            //如果是以下4个流程,还需要判断对应供应商是否开通账号,如果开通的话就不推送待办已办数据
            if (in_array($data['order_type'], ['CKD', 'RKD', 'CGD'])) {

                switch ($data['order_type']) {
                    //发货申请单流程
                    case 'CKD':
                        $supplierNo = Db::name('order_out')
                            ->alias('a')
                            ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
                            ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
                            ->where(['a.is_del' => 0, 'a.outCode' => $data['order_code']])
                            ->value('c.supplierNo');
                        break;

                    //采购单入库流程
                    case 'RKD':
                        $supplierNo = Db::name('purchease_in')
                            ->alias('a')
                            ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo')
                            ->where(['a.is_del' => 0, 'a.wsm_in_code' => $data['order_code']])
                            ->value('b.supplierNo');
                        break;
                    //采购单流程
                    case 'CGD':
                        $supplierNo = Db::name('purchease_order')
                            ->where(['cgdNo' => $data['order_code'], 'is_del' => 0])
                            ->value('supplierNo');
                        break;

//                    //商品成本流程--要根据节点的值来判断是否推送待办数据
//                    case 'SPCB':
//                        $supplierNo = Db::name('good_basic')
//                            ->where(['is_del' => 0, 'spuCode' => $data['order_code'],])
//                            ->value('supplierNo');
//                        break;

                    default:
                        $supplierNo = '';
                        break;

                }

                if ($supplierNo) {
                    //查询该供应商是否开通账号
                    $res = SupplierRelationUserModel::field('id')
                        ->where([
                            'is_del' => SupplierUserModel::$is_del_normal,
                            'supplierNo' => $supplierNo,
                            'status' => SupplierUserModel::$status_normal
                        ])
                        ->findOrEmpty()
                        ->isEmpty();

                    if (!$res) return true;

                }

            }

            //如果是SPCB的话,以下节点值推送待办数据(0新建待审核,2基础修改待审核,3成本修改待审核)
            if ($data['order_type'] == 'SPCB' && !in_array($data['action_process'], [0, 2, 3])) return true;

            $insert_data = [
                'order_type' => $data['order_type'],
                'order_code' => $data['order_code'],
                'order_id' => $data['order_id'],
                'action_uid' => $data['action_uid'],
                'action_name' => $data['action_name'],
                'status' => self::$status_wait,
                'order_process' => $data['action_process'],
                'order_process_finally' => $data['action_process'],
                'action_uid_finally' => $data['action_uid'],
                'action_name_finally' => $data['action_name'],
                'addtime' => date('Y-m-d H:i:s'),
                'updatetime' => date('Y-m-d H:i:s'),
            ];

            if ($wait_id) {
                $insert_data['handle_user_list'] = $wait_id;//handle_user_list当前待处理人的uid集合
                $insert_data['wait_id'] = $wait_id;
                $insert_data['wait_name'] = $wait_name;
            } else {
                //查询该节点值对应的角色id
                $roleid = Db::name('role_process')
                    ->whereFindInSet('action_data', $info['id'])
                    ->where('is_del', 0)
                    ->column('role_id');
                $insert_data['roleid'] = implode(',', $roleid);

                //针对议价单流程,待boss审核节点,做特殊处理
                if ($data['order_type'] == 'YJD' && $data['action_process'] == 7) $insert_data['handle_user_list'] = implode(',', Db::name('user_role')->whereIn('roleid', $roleid)->where(['is_del' => 0, 'status' => 1])->where('roleid', '<>', 1)->column('uid'));
                else $insert_data['handle_user_list'] = $handle_user_list ? $handle_user_list : implode(',', get_handle_user_list($info['id'], $data['holder_id'] ?? 0, $info['api_url'], $data['person_id'] ?? 0));//handle_user_list当前待处理人的uid集合
            }

            //增加新的节点
            return self::create($insert_data)->save();
        }

        return true;

    }

}