<?php

namespace app\admin\controller;

use app\admin\common\User as UserCommon;
use think\Exception;
use think\facade\Db;
use think\facade\Validate;

//【公司账号管理】
class UserCompanyBasic extends Base
{
    //列表
    public function getList()
    {

        $param = $this->request->only(['nickname' => '', 'username' => '', 'status' => '', 'page' => 1, 'size' => 10, 'level' => '2,3', 'companyNo' => ''], 'post', 'trim');

        if ($param['level'] == 1) return json_show(1004, '这里不能查看超管账号');//排除超管账号

        $userCommon = UserCommon::getIns();

        $rs = $userCommon->handle('userCompanyBasicList', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //添加
    public function add()
    {
        $param = $this->request->only(['nickname', 'mobile', 'email' => '', 'level' => 2, 'companyArr' => []], 'post', 'trim');

        $val = Validate::rule([
            'nickname|真实姓名' => 'require|min:2|max:200',
            'mobile|手机号' => 'require|number|length:11|mobile',
            'email|邮箱' => 'email',
            'level|账号等级' => 'number|in:1,2',
            'companyArr|关联业务公司' => 'array|max:100',
        ]);

        if (!$val->check($param)) return json_show(1004, $val->getError());

        $val_company = Validate::rule([
            'companyCode|公司编码' => 'require|length:18',
            'companyName|公司名称' => 'require',
            'company_type|公司类型' => 'require|number',
            'is_main|是否默认公司' => 'require|in:0,1',
            'roleid|角色id' => 'require|number|gt:0',
        ]);

        foreach ($param['companyArr'] as $company) {
            if (!$val_company->check($company)) {
                return json_show(1004, $val_company->getError());
                break;
            }
        }

        $userCommon = UserCommon::getIns();
        $rs = $userCommon->handle('userCompanyBasicAdd', $param);

        if (!isset($rs['code']) || $rs['code'] != 0) return json_show($rs['code'], $rs['message'], $rs['data']);
        else {

            Db::startTrans();
            try {
                $date = date('Y-m-d H:i:s');
                $insert_data = [];
                foreach ($param['companyArr'] as $company) {
                    $insert_data[] = [
                        'uid' => $rs['data']['uid'],
                        'nickname' => $param['nickname'],
                        'roleid' => $company['roleid'],
                        'companyNo' => $company['companyCode'],
                        'company_type' => $company['company_type'],
                        'status' => 1,
                        'is_main' => $company['is_main'],
                        'is_del' => 0,
                        'addtime' => $date,
                        'updatetime' => $date,
                    ];
                }

                Db::name('user_role')->insertAll($insert_data);

                Db::commit();
                return json_show($rs['code'], $rs['message'], $rs['data']);
            } catch (Exception $exception) {
                Db::rollback();
                return json_show(1004, '账户注册失败,' . $exception->getMessage());
            }
        }
    }

    //删除
    public function delete()
    {
        $param = $this->request->only(['ids'], 'post', 'trim');

        $val = Validate::rule(['ids|账号ID' => 'require|array|max:100']);
        if (!$val->check($param)) return json_show(1004, $val->getError());

        $userCommon = UserCommon::getIns();
        $rs = $userCommon->handle('userCompanyBasicDelete', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //编辑
    public function update()
    {
        $param = $this->request->only(['id', 'itemid'], 'post');

        $userCommon = UserCommon::getIns();
        $rs = $userCommon->handle('userCompanyBasicUpdate', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //启禁用
    public function status()
    {
        $param = $this->request->only(['id', 'status'], 'post', 'trim');

        $userCommon = UserCommon::getIns();
        $rs = $userCommon->handle('userCompanyBasicStatus', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //修改密码
    public function password()
    {
        $param = $this->request->only(['id', 'password'], 'post', 'trim');

        $userCommon = UserCommon::getIns();
        $rs = $userCommon->handle('setpasswd', $param);

        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //详情
    public function info()
    {

        $param = $this->request->only(['id'], 'post', 'trim');

        $userCommon = UserCommon::getIns();

        $rs = $userCommon->handle('userCompanyBasicInfo', $param);

        $user_platform = [];

        if (isset($rs['code']) && $rs['code'] == 0) {
            $user_platform_id = Db::name('user_platform')
                ->where(['uid' => $param['id'], 'is_del' => 0])
                ->value('platform','');

            if ($user_platform_id != '') {
                $user_platform = Db::name('platform')
                    ->field('id,platform_code,platform_name')
                    ->where('is_del = 0 AND id IN (' . $user_platform_id . ')')
                    ->select()
                    ->toArray();
            }
        }

        $rs['data']['platform'] = $user_platform;

        return json_show($rs['code'], $rs['message'], $rs['data']);
    }

    //设置账号关联的平台
    public function setUserPlatfrom()
    {
        $param = $this->request->only(['uid', 'platform_id'], 'post', 'trim');

        $val = Validate::rule([
            'uid|用户ID' => 'require|number|gt:0',
            'platform_id|平台id集合' => 'require|array|max:9999'
        ]);

        if ($val->check($param) == false) return json_show(1004, $val->getError());

        $platform_id = Db::name('platform')
            ->where(['is_del' => 0, 'id' => $param['platform_id']])
            ->column('id');

        if (empty($platform_id)) return json_show(1004, '平台id不正确');

        $tmp = Db::name('user_platform')
            ->field('id')
            ->where(['is_del' => 0, 'uid' => $param['uid']])
            ->findOrEmpty();

        $date = date('Y-m-d H:i:s');

        if (empty($tmp)) {
            $res = Db::name('user_platform')
                ->insert([
                    'uid' => $param['uid'],
                    'platform' => implode(',', $platform_id),
                    'is_del' => 0,
                    'addtime' => $date,
                    'updatetime' => $date,
                ]);
        } else {
            $res = Db::name('user_platform')
                ->where(['is_del' => 0, 'id' => $tmp['id']])
                ->update([
                    'platform' => implode(',', $platform_id),
                    'updatetime' => $date,
                ]);
        }

        return $res ? json_show(0, '操作成功') : json_show(1004, '操作失败');

    }


}