<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
use think\Db;

/**
 * 用户接口
 */
class User extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * @ApiTitle    (用户接口-基本信息)
     * @ApiSummary  (基本信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/EssentialInformation)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": [
    {
    "con": [
    {
    "username": "用户名",
    "mobile": "手机号",
    "email": "邮箱"
    }
    ]
    },
    {
    "team": [
    {
    "company_name": "公司名",
    "credit": "公司信用代码",
    "company_address": "公司地址",
    "address_con": "公司详细地址",
    "invoice_address": "开票地址",
    "bank_name": "银行名称",
    "bank_num": "银行账号",
    "company_tel": "公司电话",
    "type": "类型",
    "industry": "行业"
    }
    ]
    }
    ]
    })
     */
    public function EssentialInformation()
    {
        $user_id = $this->is_token($this->request->header());
        $user_con = Db::name('user')->where(['id' => $user_id])->find();
        $is_team = Db::name('team')->where(['user_id' => $user_id])->find();
        $return['con'] = [
            'username' => $user_con['username'],
            'mobile' => $user_con['mobile'],
            'email' => $user_con['email']
        ];
        if (!empty($is_team)) {
            $team = \db('company')
                ->alias('a')
                ->join('type b', 'b.id=a.type_id')
                ->join('industry c', 'c.id=a.industry_id')
                ->field('a.company_name,a.credit,a.company_address,a.address_con,a.invoice_address,a.bank_name,a.bank_num,a.company_tel,b.type,c.industry')
                ->where(['a.id' => $is_team['company_id']])
                ->find();
            $return['team'] = [
                'company_name' => $team['company_name'],
                'credit' => $team['credit'],
                'company_address' => $team['company_address'],
                'address_con' => $team['address_con'],
                'invoice_address' => $team['invoice_address'],
                'bank_name' => $team['bank_name'],
                'bank_num' => $team['bank_num'],
                'company_tel' => $team['company_tel'],
                'type' => $team['type'],
                'industry' => $team['industry'],
            ];
        } else {
            $is_company = Db::name('company')->where(['company_holder' => $user_id])->find();
            $team = \db('company')
                ->alias('a')
                ->join('type b', 'b.id=a.type_id')
                ->join('industry c', 'c.id=a.industry_id')
                ->field('a.company_name,a.credit,a.company_address,a.address_con,a.invoice_address,a.bank_name,a.bank_num,a.company_tel,b.type,c.industry')
                ->where(['a.id' => $is_company['id']])
                ->find();
            $return['team'] = [
                'company_name' => $team['company_name'],
                'credit' => $team['credit'],
                'company_address' => $team['company_address'],
                'address_con' => $team['address_con'],
                'invoice_address' => $team['invoice_address'],
                'bank_name' => $team['bank_name'],
                'bank_num' => $team['bank_num'],
                'company_tel' => $team['company_tel'],
                'type' => $team['type'],
                'industry' => $team['industry'],
            ];
        }
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (用户接口-基本信息修改)
     * @ApiSummary  (基本信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UpdateEssentialInformation)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="mobile", type="integer", required=true, description="手机号")
     * @ApiParams   (name="username", type="integer", required=true, description="用户名")
     * @ApiParams   (name="email", type="string", required=true, description="邮箱")
     * @ApiParams   (name="code", type="string", required=true, description="验证码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function UpdateEssentialInformation()
    {
        $param = $this->request->param();
        $user_id = $this->is_token($this->request->header());
        $this->CheckCode($param['mobile'], $param['code']);
        $is_matching = Db::name('user')->where(['mobile' => $param['mobile']])->find();
        if (!($is_matching['id'] == $user_id)) {
            $this->error('修改失败,请联系后台管理员', 0);
        }
        $res = Db::name('user')->where(['id' => $user_id])->update(['username' => $param['username'], 'email' => $param['email']]);
        if ($res) {
            $this->success('修改成功', 1);
        } else {
            $this->error('修改成功', 0);
        }
    }

    /**
     * @ApiTitle    (用户接口-修改密码)
     * @ApiSummary  (修改密码)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/User/UpdatePassword)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="old_password", type="string", required=true, description="原密码")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function UpdatePassword()
    {
        $param = $this->request->param();
        $user_id = $this->is_token($this->request->header());
        $password = Db::name('user')->where(['id' => $user_id])->find();
        if (!($param['old_password'] == $password['password'])) {
            $this->error('原密码错误', 0);
        }
        $res = Db::name('user')->where(['id' => $user_id])->update(['password' => $param['password']]);
        if ($res) {
            $this->success('修改成功', 1);
        } else {
            $this->error('修改失败', 0);
        }
    }
}