<?php


namespace App\Http\Services;

use App\Model\BrandModel;
use App\Model\DepartmentModel;
use App\Model\SupplierChannelModel;
use App\Model\SupplierContactModel;
use App\Model\UserDepartmentModel;
use App\Model\UserInfoModel;
use Illuminate\Support\Facades\DB;

//部门数据服务
class DepartmentService
{

    public function getSubordinateUserIds($adminId)
    {
        $departmentId = DB::table('user_info')->where('userId', $adminId)->value('department_id');
        $users = $this->getUserByDepartmentId($departmentId);
        return array_column($users, 'userId');
    }

    public function getSubordinateUserNames($adminId)
    {
        $departmentId = DB::table('user_info')->where('userId', $adminId)->value('department_id');
        $users = $this->getUserByDepartmentId($departmentId);
        return array_column($users, 'name');
    }

    // 获取部门人员
    public function getUserByDepartmentId($departmentId, $status = '', $filter = '')
    {
        $departmentIds = [];

        $this->getSubDepartmentId($departmentId, $departmentIds);

        return DB::table('user_info')->whereIn('department_id', $departmentIds)
            ->where(function ($query) use ($status) {
                if ($status !== '') {
                    $query->where('status', '=', $status);
                }
            })
            ->where(function ($query) use ($filter) {
                if ($filter) {
                    $query->whereRaw($filter);
                }
            })
            ->select('userId', 'name', 'status')
            ->get();
    }

    // 获取下级部门ID
    public function getSubDepartmentId($departmentId, &$departmentIds)
    {
        // 获取下级部门
        $sub_department = DB::table('user_department')->where('parent_id', $departmentId)->select('department_id',
            'department_name')->get();
        if ($sub_department) {
            foreach ($sub_department as $v) {
                $this->getSubDepartmentId($v['department_id'], $departmentIds);
            }
        }

        $departmentIds[] = $departmentId;

        return $departmentIds;
    }

    //根据用户id获取上一层部门信息(专门为共用申请设置的方法)
    /*
    针对共用申请:
    不同部门的情况还不同,因为线上的数据错乱了,不同部门的分级有问题
    比如运营老大被分在了顶级部门(运营部),联营采购老大和自营采购老大被分在了二级部门
    但是联营采购老大的部下,在三级部门,而且自营采购老大的部下,在二级部门,和自己同一个部门
    但是目前用这个供应商系统的,只会有这三个部门,所以要找出一个合理的处理方法
    所以这边的方法如下 :
    运营和联营采购无脑找上一级.
    因为运营老大在顶级,找不到上一级,那就取顶级即可
    联营采购本来向上找一级就是对的(除了被查找的人是联营采购的老大....那就不要往上找了)
    自营采购比较特殊,因为领导和部下都在这一个层级,所以不能往上找,往上找就是顶级的采购部了,顶级采购部是找不到人的(没有用户的部门id是顶级采购部id)
    以上所说的情况,都是建立在部门架构不变以及部门老大维持层级不变的前提下进行的
    */
    public function getUpperDepartmentByUserIdForShareApply($userId)
    {
        $userModel = new UserInfoModel();
        $departmentId = $userModel->where('userId', $userId)->value('department_id');
        if (empty($departmentId)) {
            return [];
        }
        $departmentModel = new DepartmentModel();
        //先判断是不是自营采购部门id
        if ($departmentId == config('field.ZiYingDepartmentId')) {
            return $departmentModel->where('department_id', $departmentId)->first()->toArray();
        } else {
            $department = $departmentModel->where('department_id', $departmentId)->first();
            //如果当前的部门id已经是联营采购部门Id(二级,目前有联营一部和联营二P部)
            if (in_array($departmentId, config('field.LiangYingDepartmentIds'))) {
                return $department->toArray();
            } else {
                if (!empty($department)) {
                    $department = $department->toArray();
                    //找出自己的parent_id,如果是0代表自己就是顶级部门信息了
                    if ($department['parent_id'] == 0) {
                        return $department;
                    }
                    $department = $departmentModel->where('department_id',
                        $department['parent_id'])->first()->toArray();
                    return $department;
                }
            }
        }
        return [];
    }

    //根据用户名获取部门名称
    public function getDepartmentNameByUserName($userName)
    {
        $departmentId = UserInfoModel::where('name', $userName)->value('department_id');
        $departmentName = DepartmentModel::where('department_id', $departmentId)->value('department_name');
        return $departmentName;
    }

    //根据用户名获取父级部门名称
    public function getParentDepartmentNameByUserName($userName)
    {
        $departmentId = UserInfoModel::where('name', $userName)->value('department_id');
        $parentDepartmentId = DepartmentModel::where('department_id', $departmentId)->value('parent_id');
        $parentDepartmentName = DepartmentModel::where('department_id',$parentDepartmentId)->value('department_name');
        if (empty($parentDepartmentName)) {
            $parentDepartmentName = DepartmentModel::where('department_id', $departmentId)->value('department_name');
        }
        return $parentDepartmentName;
    }

    public function getDepartmentNameMapByUserIds($userIds)
    {
        $userIds = array_unique($userIds);
        $userIddepartmentIdMap = UserInfoModel::whereIn('userId', $userIds)->pluck('department_id', 'userId')->toArray();
        $departmentIds = array_values($userIddepartmentIdMap);
        $departmentNameDepartmentIdMap = DepartmentModel::whereIn('department_id', $departmentIds)->pluck('department_name', 'department_id')->toArray();
        //找到对应的关系
        $result = [];
        foreach ($userIddepartmentIdMap as $userId => $departmentId) {

            $result[$userId] = array_get($departmentNameDepartmentIdMap,$departmentId,'');
        }
        return $result;
    }

    public function getCreateUserDepartmentListForXmSelect()
    {
        $departmentList = [];
        $topDepartmentList = DB::table('user_department')->where('parent_id', 8)->select('department_id',
            'department_name')->get();
        foreach ($topDepartmentList as $topDepartment) {
             $data = [
                'name' => $topDepartment['department_name'],
                'value' => $topDepartment['department_id'],
            ];
            $childrenDepartmentList = UserDepartmentModel::where('parent_id', $topDepartment['department_id'])
                ->selectRaw('department_id as value , department_name as name')->get()->toArray();
            $data['children'] = $childrenDepartmentList;
            $departmentList[] = $data;
        }

        return $departmentList;
    }


}