<?php

/**
 * 控制器:后台系统-技师管理
 * date:2019-08-31
 */

namespace app\modules\manage\v1\controllers;

use app\modules\common\Helper;
use app\modules\logic\services\TechService;
use app\modules\logic\services\CommonService;
use app\modules\logic\model\UsersModel;
use app\modules\logic\model\UsersTechInfoModel;
use Yii;
use yii\web\Controller;

class TechController extends MiddleController {
	/**
	 * 返回
	 * @param $code
	 * @param string $info
	 * @param string $controller
	 * @param string $total
	 */
	protected function response($code, $info = '', $total = '', $controller = 'shop') {
		return parent::response($code, $info, $total, $controller);
	}
	/**
	 * [actionAddtech 添加技师]
	 * @apiDoc
	 * @api     v1/tech/addtech
	 * @group   tech
	 * @name    添加/修改技师
	 * @desc    后台管理-技师管理-新建技师
	 * @method  POST
	 * @param  mobile string 手机号 req
	 * @param  prov_id int 省ID req
	 * @param  city_id int 市ID req
	 * @param  area_id int 区ID noreq
	 * @param  password string 初始密码 req
	 * @param  realname string 姓名 noreq
	 * @param  id_card string 身份证号 noreq
	 * @param  avatar int 头像 noreq
	 * @param  positive_card int 身份证正面 noreq
	 * @param  counter_card int 身份证反面 noreq
	 * @param  work_type string 从业工种 noreq
	 * @param  work_age string 工作年龄 noreq
	 * @param  ut_contact string 紧急联系人 noreq
	 * @param  ut_tel string 紧急联系人号码 noreq
	 * @param  tra_tool string 交通工具 noreq
	 * @param  marry int 婚姻状况 noreq
	 * @param  education int 学历 noreq
	 * @param  crash_save_valid date 意外险到期日期 req
	 * @param  approve_json json 认证【[{"yw_id":1,"level_id":2},{"yw_id":1,"level_id":3}]】业务类型+技术等级 noreq
	 * @param  user_id id 用户ID【修改必填】 noreq
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
	 	{
		"code": "000000",
		"msg": "操作成功",
		"info": true
		}
	 */
	public function actionAddtech(){
		if (!isset($this->para['mobile']) || empty($this->para['mobile'])) {
            return $this->response('para_miss', '手机号不能为空');
        }
        if (empty($this->para['prov_id']) || !is_numeric($this->para['prov_id'])) {
            return $this->response('para_miss', '请输入省ID');
        }
        if (empty($this->para['city_id']) || !is_numeric($this->para['city_id'])) {
            return $this->response('para_miss', '请输入市ID');
        }
        if (!isset($this->para['password']) || empty($this->para['password'])) {
            return $this->response('para_miss', '初始密码不能为空');
        }
        $user_id = isset($this->para['user_id'])?(int)$this->para['user_id']:0;
		$flag = CommonService::verifyUsername($this->para['mobile'],$user_id);
		if($flag){
			return $this->response('para_miss', '手机号已存在');
		}
		$res = TechService::createTech($this->para,$user_id);
		if($res){
			return $this->response('deal_succ', $res);
		}else{
			return $this->response('deal_false', $res);
		}
	}
	/**
	 * [actionListtech 技师账号列表]
	 * @apiDoc
	 * @api     v1/tech/listtech
	 * @group   tech
	 * @name    技师列表
	 * @desc    后台管理-技师管理-技师列表
	 * @method  POST
	 * @param  user_code string 技师编码 noreq
	 * @param  mobile string 联系电话 noreq
	 * @param  realname string 姓名 noreq
	 * @param  verify_status int 审核状态【0待审核,1通过,2未通过,-1未提交】 noreq
	 * @param currentPage string 当前页 noreq 1
	 * @param pageSize string 每页条数 noreq 15
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		{
			"code": "000000",
			"msg": "操作成功",
			"total": "77",
			"info": [
			    {
			        "user_id": "1",//用户ID
			        "user_code": "Tech20190905",//师傅工号
			        "mobile": "18980647859",//手机号
			        "status": "正常",//运行状态
			        "reg_time": "2019-09-05 10:34:26",//注册时间
			        "realname": "张三",//师傅姓名
			        "id_card": "513701199007225224",//身份证号码
			        "work_age": "10年",//工龄（年）
			        "work_type": "测试工种",//工种
			        "work_nature": "全职",//师傅属性
			        "created": "2019-09-05 10:34:26",//审核时间
			        "verify_status_show": "已审核",//审核状态
			        "address": "-",//地址
			        "sex": "-",//性别
			        "age": "-",//年龄
			        "prov_id_show": "四川省",//省
			        "city_id_show": "成都市",//市
			        "area_id_show": "武侯区",//区
			        "approve_level": "一级美容师"//授权级别
			    },
			    {
			        "user_id": "13",
			        "user_code": null,
			        "mobile": "13988886666",
			        "status": "正常",
			        "reg_time": "2019-09-27 16:04:09",
			        "realname": null,
			        "id_card": null,
			        "work_age": null,
			        "work_type": null,
			        "work_nature": "兼职",
			        "created": "1970-01-01 08:00:00",
			        "verify_status_show": "--",
			        "address": "-",
			        "sex": "-",
			        "age": "-",
			        "prov_id_show": "--",
			        "city_id_show": "--",
			        "area_id_show": "--",
			        "approve_level": ""
			    }
			]
			}
	 */
	public function actionListtech(){
		$_where = "";
		$_where .= " and p1.type= 'tech' ";
        if (!empty($this->para['user_code'])) {
            $_where .= " and p1.user_code= '" . $this->para['user_code'] . "' ";
        }
        if (!empty($this->para['mobile'])) {
            $_where .= " and p1.mobile= '" . $this->para['mobile'] . "' ";
        }
        if (!empty($this->para['realname'])) {
            $_where .= " and p2.realname= '" . $this->para['realname'] . "' ";
        }
        if (isset($this->para['verify_status']) && $this->para['verify_status']>=0) {
            $_where .= " and p2.verify_status= '" . $this->para['verify_status'] . "' ";
        }
        if(isset($this->para['verify_status']) && $this->para['verify_status']==-1){
        	$_where .= " and p2.verify_status is null ";
        }
        $currentPage = 1;
        $pageSize = 15;
        if (isset($this->para['currentPage']) && !empty($this->para['currentPage']) && is_numeric($this->para['currentPage'])) {
            $currentPage = $this->para['currentPage'];
        }
        if (isset($this->para['pageSize']) && !empty($this->para['pageSize']) && is_numeric($this->para['pageSize'])) {
            $pageSize = $this->para['pageSize'];
        }

        $limit_start = ($currentPage - 1) * $pageSize;
        $_limit = " limit " . $limit_start . " , " . $pageSize;
        $_order = " order by p1.user_id desc";
        /*数据库链接*/
        $conn = Yii::$app->db;
        //记录总数
        $sql_total = "select count(1) as total_num from {{%users}} as p1 left join {{%users_tech_info}} as p2  on p1.user_id=p2.user_id where 1  " . $_where;
        $cmd_total = $conn->createCommand($sql_total);
        $res_total = $cmd_total->queryOne();
        $total_records = $res_total['total_num'];

        //当前记录
        $sql = "select p1.user_id,p1.user_code,p1.mobile,p1.prov_id,p1.city_id,p1.area_id,p1.status,p1.reg_time,p2.realname,p2.id_card,p2.work_age,p2.work_type,p2.work_nature,p2.verify_status,p2.created,p2.operator,p2.op_time
		from {{%users}} as p1 left join {{%users_tech_info}} as p2  on p1.user_id=p2.user_id where 1 " . $_where . $_order . $_limit;
        $cmd = $conn->createCommand($sql);
        $data = $cmd->queryAll();
        foreach ($data as &$value) {
        	$value['verify_status_show'] = Helper::getVerifyStatusName($value['verify_status']);
        	$value['address'] = '-';
        	$idCardInfo =!empty($value['id_card'])?Helper::getInfoByIdCard($value['id_card']):[];
        	$value['sex'] = isset($idCardInfo['sex'])?$idCardInfo['sex']:'-';
        	$value['age'] = isset($idCardInfo['age'])?$idCardInfo['age']:'-';
        	$value['status'] = $value['status']==0?'正常':'禁用';
        	$value['work_nature'] = $value['work_nature']==1?'全职':'兼职';
        	$value['prov_id_show'] = Helper::getRegion($value['prov_id']);
        	$value['city_id_show'] = Helper::getRegion($value['city_id']);
        	$value['area_id_show'] = Helper::getRegion($value['area_id']);
        	$approve = TechService::getTechApprove($value['user_id']);
        	$value['approve_level'] = isset($approve['approve_level'])?$approve['approve_level']:'';
        	$value['created'] = !empty($value['created'])?date("Y-m-d H:i:s",$value['created']):'-';
        	$value['reg_time'] = !empty($value['reg_time'])?date("Y-m-d H:i:s",$value['reg_time']):'-';
        	$value['op_time'] = !empty($value['op_time'])?date("Y-m-d H:i:s",$value['op_time']):'-';
        	unset($value['prov_id']);
        	unset($value['city_id']);
        	unset($value['area_id']);
        	unset($value['verify_status']);
        }
 		$conn->close();
        return $this->response('search_succ', $data, $total_records);
	}
	/**
	 * [actionDetailtech 技师详情]
	 * @apiDoc
	 * @api     v1/tech/detailtech
	 * @group   tech
	 * @name    技师详情
	 * @desc    后台管理-技师管理-技师详情
	 * @method  POST
	 * @param  user_id int 技师ID req
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
		 {
		"code": "000000",
		"msg": "操作成功",
		"info": {
		    "info": {
		        "user_id": "1",
		        "realname": "test",
		        "id_card": "513701199007225224",
		        "avatar": "1",
		        "positive_card": "2",
		        "counter_card": "2",
		        "yw_id": "0",
		        "work_type": "美容",
		        "work_age": "12",
		        "ut_contact": "bbx",
		        "ut_tel": "15067462246",
		        "tra_tool": "电动车",
		        "marry": "1",
		        "education": "2",
		        "work_nature": "1",
		        "verify_status": "0",
		        "created": "1567650866",
		        "avatar_show": "http://img.repucar.test/uploads/app/images/201909/tech_20190904_5d6f7e5f52a45.jpg",
		        "positive_card_show": "http://img.repucar.test/uploads/app/images/201909/tech_20190904_5d6f8094c212c.jpg",
		        "counter_card_show": "http://img.repucar.test/uploads/app/images/201909/tech_20190904_5d6f8094c212c.jpg",
		        "verify_status_show": "待审核",
		        "work_nature_show": "全职"
		    },
		    "approve": [
		        {
		            "id": "1",
		            "yw_id": "1",
		            "level_id": "1"
		        },
		        {
		            "id": "2",
		            "yw_id": "1",
		            "level_id": "1"
		        }
		    ]
		}
		}
	 */
	public function actionDetailtech(){
		if (empty($this->para['user_id']) || !is_numeric($this->para['user_id'])) {
            return $this->response('para_miss', '技师ID不能为空');
        }
        $data = TechService::getTechDetail($this->para['user_id']);
        return $this->response('deal_succ', $data);
	}
	/**
	 * [actionVerifytech 技师审核]
	 * @apiDoc
	 * @api     v1/tech/verifytech
	 * @group   tech
	 * @name    技师审核
	 * @desc    后台管理-技师管理-技师审核
	 * @method  POST
	 * @param   user_id int 用户ID req
	 * @param  verify_status int 审核状态【1，通过2，未通过】 req
	 * @author JOHN.W
	 * @version [1.0]
	 * @return  json
	 {
		"code": "000000",
		"msg": "操作成功",
		"info": true
	 }
	 */
	public function actionVerifytech(){
		if (empty($this->para['user_id']) || !is_numeric($this->para['user_id'])) {
            return $this->response('para_miss', '用户ID不能为空');
        }
        if (empty($this->para['verify_status']) || !in_array($this->para['verify_status'], ['1','2'])) {
            return $this->response('para_miss', '审核状态不正确');
        }
        $UsersTechInfo = new UsersTechInfoModel();
        //查找用户是否未审核
        $flag = $UsersTechInfo->getWidgetRow(['cols'=>['verify_status'],'user_id'=>$this->para['user_id']]);
        if(isset($flag['verify_status']) && !in_array($flag['verify_status'],['0','2'])){
        	return $this->response('para_miss', '已审核通过');
        }
        $res = $UsersTechInfo->saveUs(['verify_status'=>$this->para['verify_status'],'operator'=>$this->rec_role['operator_name'],'created'=>time(),'op_time'=>time()],$this->para['user_id']);
		if($res){
			return $this->response('deal_succ', $res);
		}else{
			return $this->response('deal_false', $res);
		}
	}
	

}
