<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/24
 * Time: 15:30
 */

namespace app\products\controller;
use app\web\controller\BaseController;

use think\Db;
use think\Request;

class Role extends BaseController
{
    //获取用户列表
    public function getUsers(Request $request){
        $data = $request->param();
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
        $offset=($page-1)*$rows;
        $data['offset']=$offset;
        $data['rows']=$rows;
        $where=[];
        if(!empty($data['id']) && isset($data['id'])){
            $where['id']=$data['id'];
        }

        if (isset($data['start']) && !empty($data['start']) &&  isset($data['end']) && !empty($data['end'])) {
            $start_time = strtotime($data['start'].' 00:00:01');
            $end_time = strtotime($data['end'].' 23:59:59');
            $where['entryTime'] = ['between', [$start_time, $end_time]];
        } else if (isset($data['start']) && !empty($data['start']) && isset($data['end']) == false) {
            $start_time = strtotime($data['start'].' 00:00:01');
            $where['entryTime'] = ['egt', $start_time];
        } else if (isset($data['start']) == false && isset($data['end']) && !empty($data['end'])) {
            $end_time = strtotime($data['end'].' 23:59:59');
            $where['entryTime'] = ['elt', $end_time];
        }
        if(!empty($data['xiala'])){
            $where['mechanism']=$data['xiala'];
        }
        if(!empty($data['names'])){
            $where['name']=$data['names'];
        }
        if(!empty($data['tel'])){
            $where['phone']=$data['tel'];
        }

        //$org_id = session('organization.org_id');
        $list= Db::table('sos_user_role')->where($where)->limit($offset,$rows)->select()->toArray();
        $total= Db::table('sos_user_role')->where($where)->count();

        $type=['','试用期','正式员工','已离职'];
        foreach($list  as $key=>$val ){
            $list[$key]['sex']=$val['sex'] == 1? '男' : '女';
            //所属机构
            if($val['mechanism'] != 0){
                $mechanism= Db::table('sos_user_mechanism')->field('mechanismName')->where('id',$val['mechanism'])->find();
            }
            $list[$key]['mechanism']=$val['mechanism'] == 0? '男' : $mechanism['mechanismName'];
            //所属部门
            if($val['department'] != 0){
                $department= Db::table('sos_user_department')->field('departmentName')->where('id',$val['department'])->find();
            }
            $list[$key]['department']=$val['department'] == 0? '男' : $department['departmentName'];
            $list[$key]['status']=$type[$val['status']];

            $list[$key]['entryTime']=date('Y-m-d',$val['entryTime']);
            $list[$key]['quitTime']=$val['quitTime'] == ''? '' : date('Y-m-d',$val['entryTime']);

        }
        $result["total"] =$total;
        $result['rows']=$list;
        echo json_encode($result);
    }


    /**
     * 获取机构列表
     */

    public function  getMechanism(){
        $list= Db::table('sos_user_mechanism')->select()->toArray();
        echo json_encode($list);
    }

    /**
     * 获取机构下部门列表
     */

    public function  getdepartment(Request $request){
        $data = $request->get();
        $list= Db::table('sos_user_department')->where('mechanismId',$data['id'])->select()->toArray();
        echo json_encode($list);
    }

    /**
     * @param Request $request
     * @return array|mixed|null
     * 添加用户
     */

    public function user_add(Request $request)
    {
        $data = $request->param();

        if(isset($data['sex']) && $data['sex']=='on'){
            $data['sex']=1;
        }else{
            $data['sex']=2;
        }
        if(isset($data['entryTime']) && !empty($data['entryTime'])){
            $data['entryTime']=strtotime($data['entryTime']);
        }
        if(isset($data['quitTime']) && !empty($data['quitTime'])){
            $data['quitTime']=strtotime($data['quitTime']);
        }

        if(isset($data['user_id']) && !empty($data['user_id'])){
            $data['updateTime']=time();
            $where['id']=$data['user_id'];
            unset($data['user_id']);
            $res = Db::table('sos_user_role')->where($where)->update($data);
        }else{
            unset($data['user_id']);
            $data['createTime']=time();

            $res = Db::table('sos_user_role')->insert($data);
        }
        $json=json_decode($res,true);
        if(is_numeric($json)){
            $data=[
                'code'=>200,
                'msg'=>'成功'
            ];
        }else{
            $data=[
                'code'=>100,
                'msg'=>'失败'
            ];
        }
        return $data;
    }

    /**
     * @param Request $request
     * @return \think\response\Json
     * 自动生成员工编号
     */

    public function generateNumber(Request $request){
        $data = $request->get();
        $rule_name= Db::table('sos_user_mechanism')->where('id',$data['id'])->find();
        if(isset($data['rule_id']) && !empty($data['rule_id'])){
            $rule_num= Db::table('sos_user_role')->where('id',$data['rule_id'])->find();
            $rule_number['num']=$rule_name['rule'].(substr($rule_num['personalNumber'], 2, 7));
        }else{
            $rule_num= Db::table('sos_user_role')->order('id desc')->find();
            $rule_number['num']=$rule_name['rule'].(substr($rule_num['personalNumber'], 2, 7)+1);
        }




        echo json_encode($rule_number);
    }



}