<?php
namespace app\admin\controller;

use app\common\model\UserWill;
use think\facade\Session;
use app\admin\BaseController;
use app\common\model\User as UserModel;
use app\common\model\UserAuths as UserAuthsModel;
use app\common\model\UserFollow as UserFollowModel;
use app\common\model\UserGroups as UserGroupsModel;
use app\common\model\UserPart as UserPartModel;
use app\common\model\UserIntegral as UserIntegralModel;
use app\common\model\UserRank as UserRankModel;
use app\common\model\UserParam as UserParamModel;

use app\common\model\Agent as AgentModel;
use app\common\model\Broker as BrokerModel;

use app\common\validate\User as UserValidate;
use think\exception\ValidateException;

use think\facade\Db;

use PHPExcel_IOFactory;
use PHPExcel;

class User extends BaseController
{
    
    
    // 用户跟进记录
    public function follow()
    {
        $userid = input('userid/d');
        $user = UserModel::findOrEmpty($userid);
        $followlist = UserFollowModel::where('userid', $userid)->order('id', 'desc')->limit(100)->select();
        return view('user/follow', [
            'user'			=> $user,
            'followlist'	=> $followlist
        ]);
    }
    
    public function editFollow()
    {
        $userid = input('userid/d', 0);
        $user = UserModel::findOrEmpty($userid);
        if ($user->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "用户信息不存在。"
            )));
        }
        UserFollowModel::create([
            'userid'		=> $userid,
            'ftype'			=> input('ftype/s', ""),
            'remark'		=> input('remark/s', ""),
            'createtime'	=> time()
        ]);
        $followstatus = input('followstatus/d', 1);
        $user->save([
            'followstatus' => $followstatus
        ]);
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
        
    // 邀请排行榜
    public function rankList()
    {
        return view('user/ranklist');
    }
    
    public function rankForm()
    {
        $id = input('id/d');
        $rank = UserRankModel::findOrEmpty($id);
        return view('user/rankform', [
            'rank'	=> $rank
        ]);
    }
    
    public function editRank()
    {
        $id = input('id/d');
        $rank = UserRankModel::findOrEmpty($id);
        $rank->save([
            'realname'			=> input('realname/s'),
            'mobile'			=> input('mobile/s'),
            'partnumber'		=> input('partnumber/d', 0),
        ]);
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function fieldRank()
    {
        $id = input('id/d', 0);
        $rank = UserRankModel::findOrEmpty($id);
        if ($rank->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "信息不存在"
            )));
        } else {
            $rank->save([
                input('field/s')  => input('value')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function listRank()
    {
        $limit = input('limit');
        $page = input('page');
        $map = array();
        $list = UserRankModel::where($map)->order('partnumber', 'desc')->limit($limit)->page($page)->select();
        $count = UserRankModel::where($map)->count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function delRank()
    {
        $idarr = input('idarr/a');
        UserRankModel::whereIn('id', $idarr)->delete();
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }
    
    // 邀请记录
    public function partList()
    {
        return view('user/partlist');
    }
    
    public function listPart()
    {
        $limit = input('limit');
        $page = input('page');
        $map = array();
        $keywords = input('keywords/s', "");
        $status = input('status/d', 0);
        if (!empty($status)) {
            $map[] = ['UserPart.status', '=', $status];
        }
        $list = UserPartModel::hasWhere('puser', [['realname|mobile', 'like', '%'.$keywords.'%', 'or']])->with(['puser','user'])->where($map)->order('id', 'desc')->limit($limit)->page($page)->append(['status_text'])->select();
        $count = UserPartModel::hasWhere('puser', [['realname|mobile', 'like', '%'.$keywords.'%', 'or']])->where($map)->count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function exportPart()
    {
        $map = array();
        $keywords = input('keywords/s', "");
        $status = input('status/d', 0);
        if (!empty($status)) {
            $map[] = ['UserPart.status', '=', $status];
        }
        $xlsData = UserPartModel::hasWhere('puser', [['realname|mobile', 'like', '%'.$keywords.'%', 'or']])->with(['puser','user'])->where($map)->order('id', 'desc')->select()->toArray();
        $xlsCell = array(
            array('id','表ID'),
            array('puser.nickname','邀请人昵称'),
            array('puser.realname','邀请人姓名'),
            array('puser.mobile','邀请人手机号'),
            array('puser.bankcard.openbank','邀请人开户行'),
            array('puser.bankcard.account','邀请人帐户名'),
            array('puser.bankcard.number','邀请人账户号'),
            array('user.nickname','被邀请人昵称'),
            array('user.realname','被邀请人姓名'),
            array('user.mobile','被邀请人手机号'),
            array('status','状态',array(1=>'未入职',2=>'已入职',3=>'已发放')),
            array('redmoney','奖金金额'),
            array('createtime','推荐时间'),
        );
        export_excel("系统用户", $xlsCell, $xlsData);
    }
    
    public function fieldPart()
    {
        $id = input('id/d', 0);
        $part = UserPartModel::findOrEmpty($id);
        if ($part->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "信息不存在"
            )));
        } else {
            $part->save([
                input('field/s')  => input('value')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function partForm()
    {
        $id = input('id/d');
        $part = UserPartModel::with(['puser','user'])->findOrEmpty($id);
        return view('user/partform', [
            'part'	=> $part
        ]);
    }
    
    public function editPart()
    {
        $id = input('id/d');
        $part = UserPartModel::findOrEmpty($id);
        $part->save([
            'status'	=> input('status/d', 1),
            'redmoney'	=> input('redmoney/d', 0),
        ]);
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function delPart()
    {
        $idarr = input('idarr/a');
        UserPartModel::whereIn('id', $idarr)->delete();
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }
    
    // 用户聘豆
    public function integralList()
    {
        return view('user/integrallist');
    }
    
    public function listIntegral()
    {
        $limit = input('limit');
        $page = input('page');
        $map = array();
        $keywords = input('keywords/s', "");
        $itype = input('itype/d', 0);
        if (!empty($itype)) {
            $map[] = ['itype', '=', $itype];
        }
		$status = input('status/d', 0);
		if (!empty($status)) {
		    $map[] = ['status', '=', $status];
		}
        $list = UserIntegralModel::hasWhere('user', [['realname|mobile', 'like', '%'.$keywords.'%', 'or']])->with(['user'])->where($map)->order('id', 'desc')->limit($limit)->page($page)->select()->append(['itype_text','status_text']);
        $count = UserIntegralModel::hasWhere('user', [['realname|mobile', 'like', '%'.$keywords.'%', 'or']])->where($map)->count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function exportIntegral()
    {
        $map = array();
        $keywords = input('keywords/s', "");
        $itype = input('itype/d', 0);
        if (!empty($itype)) {
            $map[] = ['itype', '=', $itype];
        }
        $xlsData = UserIntegralModel::hasWhere('user', [['realname|mobile', 'like', '%'.$keywords.'%', 'or']])->with(['user'])->where($map)->order('id', 'desc')->select()->append(['itype_text','status_text'])->toArray();
        $xlsCell = array(
            array('id','表ID'),
            array('user.nickname','昵称'),
            array('user.realname','姓名'),
            array('user.mobile','手机号'),
            array('user.bankcard.openbank','开户行'),
            array('user.bankcard.account','帐户名'),
            array('user.bankcard.number','账户号'),
            array('title','聘豆标题'),
            array('itype_text','类型'),
			array('status_text','状态'),
            array('intvalue','聘豆变更值'),
            array('intmoney','聘豆金额'),
            array('remark','备注'),
            array('createtime','注册时间'),
        );
        export_excel("用户聘豆", $xlsCell, $xlsData);
    }
    
    
    public function fieldIntegral()
    {
        $id = input('id/d', 0);
        $integral = UserIntegralModel::findOrEmpty($id);
        if ($integral->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "信息不存在"
            )));
        } else {
            $integral->save([
                input('field/s')  => input('value')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function integralUpdate()
    {
        $id = input('id/d', 0);
        $integral = UserIntegralModel::with('user')->findOrEmpty($id);
        return view('user/integralupdate', [
            'integral'	=> $integral
        ]);
    }
    
    public function updateIntegral()
    {
        $id = input('id/d', 0);
        $integral = UserIntegralModel::findOrEmpty($id);
        if ($integral->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "用户聘豆记录不存在"
            )));
        }
        $createtime = input('createtime/s', date("Y-m-d H:i:s"));
        $integral->save([
            'title'			=> input('title/s', ""),
            'intvalue'		=> input('intvalue/d', 0),
            'intmoney'		=> input('intmoney/f', 0.00),
            'remark'		=> input('remark/s', ""),
            'itype'			=> input('itype/d', 1),
			'status'		=> input('status/d', 1),
            'createtime'	=> $createtime,
            'yeartime'		=> date("Y", strtotime($createtime)),
            'monthtime'		=> date("Ym", strtotime($createtime))
        ]);
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function statusIntegral()
    {
        $id = input('id/d');
        $integral = UserIntegralModel::where(['itype'=>3])->find($id);
        if ($integral==null) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "该记录非兑现类型"
            )));
        } elseif ($integral->status==1) {
            $integral->save([
                'status' => 2
            ]);
        } elseif ($integral->status==2) {
            $integral->save([
                'status' => 1
            ]);
        } else {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "该记录非兑现类型。"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
      
    public function statusIntegralAll()
    {
        $idarr = input('idarr/a');
        UserIntegralModel::update(['status' => 2], ['status'=>1,'itype'=>3,'id'=>$idarr]);
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }
    
    public function delIntegral()
    {
        $idarr = input('idarr/a');
        UserIntegralModel::whereIn('id', $idarr)->delete();
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }
    
    public function integralForm()
    {
        $userid = input('userid/d');
        $user = UserModel::findOrEmpty($userid);
        return view('user/integralform', [
            'user'	=> $user
        ]);
    }
    
    public function editIntegral()
    {
        $userid = input('userid/d');
        $user = UserModel::findOrEmpty($userid);
        $intvalue = input('intvalue/d', 0);
        if ($intvalue==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "聘豆变更值不能为0"
            )));
        }
        $data = array(
            'userid'		=> $userid,
            'title'			=> input('title/s'),
            'intvalue'		=> $intvalue,
            'intmoney'		=> input('intmoney/s', 0.00),
			'onlycontent'	=> "",
            'remark'		=> input('remark/s'),
            'itype'			=> input('itype/d'),
			'status'		=> 2,
            'createtime'	=> input('createtime/s'),
            'yeartime'		=> date("Y"),
            'monthtime'		=> date("Ym")
        );
        UserIntegralModel::create($data);
        $udata = array();
        $udata['integral'] = intval($user->integral) + $intvalue;
        $isinttotal = input('isinttotal/d', 0);
        if ($isinttotal==1) {
            $udata['inttotal'] = intval($user->inttotal) + $intvalue;
        }
        $user->save($udata);
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    // 用户授权
    public function authsList()
    {
        $userid = input('userid/d');
        return view('user/authslist', [
            'userid'	=> $userid
        ]);
    }
    
    public function listAuths()
    {
        $userid = input('userid/d');
        $list = UserAuthsModel::where('userid', $userid)->order('id', 'asc')->append(['identitytype_text'])->select();
        $count = UserAuthsModel::where('userid', $userid)->count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function delAuths()
    {
        $id = input('id/d');
        $auths = UserAuthsModel::find($id);
        if ($auths->identitytype == 'mobile') {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "手机号授权方式不允许删除"
            )));
        }
        $result = $auths->delete();
        if ($result) {
            exit(json_encode(array(
                'code' 	=> 0
            )));
        }
        exit(json_encode(array(
            'code' 	=> 1,
            'msg'	=> "删除失败，请稍后重试"
        )));
    }
    
    // 用户
	public function userPublic()
	{
	    $groupslist = UserGroupsModel::order(['isdefault'=>'desc','id'=>'asc'])->select();
	    $agentlist = AgentModel::with('broker')->order(['id'=>'desc'])->select();
	    return view('user/userpublic', [
	        'groupslist'	=> $groupslist,
	        'agentlist'		=> $agentlist
	    ]);
	}
	
    public function userList()
    {
        $groupslist = UserGroupsModel::order(['isdefault'=>'desc','id'=>'asc'])->select();
        $agentlist = AgentModel::with('broker')->order(['id'=>'desc'])->select();
        return view('user/userlist', [
            'groupslist'	=> $groupslist,
            'agentlist'		=> $agentlist
        ]);
    }
    
    public function userForm()
    {
        $id = input('id/d', 0);
        $user = UserModel::with(['broker'])->findOrEmpty($id);
        $agentlist = AgentModel::with('broker')->order(['id'=>'asc'])->select();
        $groupslist = UserGroupsModel::order(['isdefault'=>'desc','id'=>'asc'])->select();
        $willlist = UserWill::select();
        return view('user/userform', [
            'groupslist'	=> $groupslist,
            'willlist'      => $willlist,
            'agentlist'		=> $agentlist,
            'user'			=> $user
        ]);
    }
    
    public function fieldUser()
    {
        $id = input('id/d', 0);
        $user = UserModel::findOrEmpty($id);
        if ($user->isEmpty()) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "信息不存在"
            )));
        } else {
            $user->save([
                input('field/s')  => input('value')
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function listUser()
    {
        $limit = input('limit');
        $page = input('page');
        $map = array();
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] =['nickname|realname|mobile', 'like', '%'.$keywords.'%', 'or'];
        }
        $groupsid = input('groupsid/d');
        if (!empty($groupsid)) {
            $map[] = ['groupsid', '=', $groupsid];
        }
		$ispublic = input('ispublic/d', 0);
		if ($ispublic==0) {
			$agentbrokerarr = explode(",", input('agentbroker/s'));
			$agentid = isset($agentbrokerarr[0]) ? $agentbrokerarr[0] : 0;
			if (!empty($agentid)) {
			    $brokeridarr = BrokerModel::where('agentid', '=', $agentid)->column('id');
			    $map[] = ['brokerid', 'in', $brokeridarr];
			    $map[] = ['brokerid', '<>', 0];
			}
			$brokerid = isset($agentbrokerarr[1]) ? $agentbrokerarr[1] : 0;
			if (!empty($brokerid)) {
			    $map[] = ['brokerid', '=', $brokerid];
			}
		}else{
			$map[] = ['brokerid', '=', 0];
		}
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $authstatus = input('authstatus/d');
        if (!empty($authstatus)) {
            $map[] = ['authstatus', '=', $authstatus];
        }
		$followstatus = input('followstatus/d');
		if (!empty($followstatus)) {
		    $map[] = ['followstatus', '=', $followstatus];
		}
        $list = UserModel::with(['userGroups','broker'=>['agent','worker']])->withCount('userPart')->where($map)->order('id', 'desc')->limit($limit)->page($page)->append(['status_text','isvip_text','authstatus_text','followstatus_text','education_text','worker_text','jobintention_text'])->select();
        $count = UserModel::where($map)->count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function exportUser()
    {
        $map = array();
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] =['nickname|realname|mobile', 'like', '%'.$keywords.'%', 'or'];
        }
        $groupsid = input('groupsid/d');
        if (!empty($groupsid)) {
            $map[] = ['groupsid', '=', $groupsid];
        }
        $agentbrokerarr = explode(",", input('agentbroker/s'));
        $agentid = isset($agentbrokerarr[0]) ? $agentbrokerarr[0] : 0;
        if (!empty($agentid)) {
            $brokeridarr = BrokerModel::where('agentid', '=', $agentid)->column('id');
            $map[] = ['brokerid', 'in', $brokeridarr];
            $map[] = ['brokerid', '<>', 0];
        }
        $brokerid = isset($agentbrokerarr[1]) ? $agentbrokerarr[1] : 0;
        if (!empty($brokerid)) {
            $map[] = ['brokerid', '=', $brokerid];
        }
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $authstatus = input('authstatus/d');
        if (!empty($authstatus)) {
            $map[] = ['authstatus', '=', $authstatus]; 
        }
		$followstatus = input('followstatus/d');
		if (!empty($followstatus)) {
		    $map[] = ['followstatus', '=', $followstatus];
		}
        $xlsData = UserModel::with(['userGroups','broker'=>['agent','worker']])->withCount('userPart')->where($map)->order('id', 'desc')->append(['status_text','isvip_text','authstatus_text','followstatus_text'])->select()->toArray();
        $xlsCell = array(
            array('id','表ID'),
            array('nickname','昵称'),
            array('realname','姓名'),
            array('mobile','手机号'),
            array('integral','可用聘豆'),
            array('inttotal','累计聘豆'),
            array('status_text','状态'),
            array('isvip_text','是否VIP'),
            array('authstatus_text','是否实名认证'),
            array('idcard','身份证号'),
            array('gender','性别',array(1=>'男',2=>'女')),
            array('birthday','出生日期'),
            array('address','现居住地'),
            array('education','学历'),
            array('bankcard.openbank','开户行'),
            array('bankcard.account','账户名'),
            array('bankcard.number','账户号'),
            array('followstatus_text','跟进状态'),
            array('userGroups.title','用户组'), 
            array('broker.worker.title','劳务公司'),
            array('broker.agent.title','代理门店'),
            array('broker.title','职业顾问'),
            array('user_part_count','推广人数'),
            array('createtime','注册时间'),
        );
        export_excel("系统用户", $xlsCell, $xlsData);
    }
    
	public function setBroker()
	{
	    $idarr = input('idarr/a');
		$setagentbroker = explode(",", input('setagentbroker/s'));
		$brokerid = isset($setagentbroker[1]) ? $setagentbroker[1] : 0;
		if (empty($brokerid)) {
		    exit(json_encode(array(
		        'code' 	=> 1,
		        'msg'	=> "请选择职业顾问。"
		    )));
		}
	    UserModel::whereIn('id', $idarr)->update(['brokerid' => $brokerid]);
	    exit(json_encode(array(
	        'code' 	=> 0,
	        'msg'	=> ""
	    )));
	}
	
    public function delUser()
    {
        $idarr = input('idarr/a');
        UserAuthsModel::whereIn('userid', $idarr)->delete();
		UserModel::whereIn('id', $idarr)->delete();
        // $result = Db::name('user')->whereIn('id',$idarr)->update(['deletetime'=>time()]);
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }
    
    public function editUser()
    {
        $id = input('id/d');
        $mobile = input('mobile/s');
        $vdata = array(
            'id'		=> $id,
            'mobile'    => $mobile
        );
        try {
            validate(UserValidate::class)->check($vdata);
        } catch (ValidateException $e) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> $e->getError()
            )));
        }
        $address = input('address/s', "");
        $jobintention = input('jobintention/s', "");
        if(!$address){
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "请填写居住地址。"
            )));
        }
        if(!$jobintention){
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "请填写意向岗位。"
            )));
        }
        $agentbrokerarr = explode(",", input('agentbroker/s'));
        $brokerid = isset($agentbrokerarr[1]) ? $agentbrokerarr[1] : 0;
        $data = array(
            'groupsid'			=> input('groupsid/d', 0),
            'brokerid'			=> $brokerid,
            'nickname'			=> input('nickname/s', ""),
            'avatar'			=> input('avatar/s', ""),
            'realname'			=> input('realname/s', ""),
            'mobile'			=> $mobile,
            'inttotal'			=> input('inttotal/d', 0),
            'status'			=> input('status/d', 1),
            'isvip'				=> input('isvip/d', 1),
            'authstatus'		=> input('authstatus/d', 1),
            'authremark'		=> input('authremark/s', ""),
            'idcardzpic'		=> input('idcardzpic/s', ""),
            'idcardfpic'		=> input('idcardfpic/s', ""),
            'idcard'			=> input('idcard/s', ""),
            'gender'			=> input('gender/d', 1),
            'birthday'			=> input('birthday/s', ""),
            'address'			=> $address,
            'education'			=> input('education/s', ""),
            'createtime'		=> input('createtime/s', ""),
            'jobintention'		=> $jobintention,
            'workexperience'	=> input('workexperience/s', ""),
            'eduexperience'		=> input('eduexperience/s', ""),
            'followstatus'		=> input('followstatus/d', 1),
            'bankcard'			=> input('bankcard/a', array()),
        );
        $password = input('password/s');
        if (empty($id)) {
            $data['integral'] = 0;
            $user = UserModel::create($data);
            $auths = UserAuthsModel::create([
                'userid'		=> $user->id,
                'identitytype'	=> "mobile",
                'identifier'	=> $mobile,
                'password'		=> empty($password) ? md5("123456789") : md5($password),
                'logintime'		=> time(),
                'loginip'		=> $_SERVER['SERVER_ADDR'],
                'wxampcode'		=> ""
            ]);
        } else {
            $data['id'] = $id;
            $user = UserModel::update($data);
            $adata = ['identifier'=>$mobile];
            if (!empty($password)) {
                $adata['password'] =  md5($password);
            }
            UserAuthsModel::update($adata, ['userid'=>$id,'identitytype'=>'mobile']);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    

    // 用户组
    public function groupsList()
    {
        return view('user/groupslist');
    }
    
    public function groupsForm()
    {
        $id = input('id/d, 0');
        $groups = UserGroupsModel::findOrEmpty($id);
        return view('user/groupsform', [
            'groups' => $groups
        ]);
    }
    
    public function listGroups()
    {
        $limit = input('limit');
        $page = input('page');
        $list = UserGroupsModel::order(['isdefault'=>'desc','id'=>'asc'])->limit($limit)->page($page)->append(['isdefault_text'])->select();
        $count = UserGroupsModel::count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }
    
    public function editGroups()
    {
        $id = input('id/d');
        if (empty($id)) {
            $groups = UserGroupsModel::create([
                'title' 	=> input('title/s'),
                'isdefault' => input('isdefault/d')==2 ? 2 : 1
            ]);
        } else {
            $administer = UserGroupsModel::find($id);
            $administer->save([
                'title' 	=> input('title/s'),
                'isdefault' => input('isdefault/d')==2 ? 2 : 1
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }
    
    public function delGroups()
    {
        $access_admin = session('access_admin');
        $password = input('password');
        if ($access_admin['password'] !== md5($password)) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "操作密码验证失败"
            )));
        }
        $idarr = input('idarr/a');
        UserGroupsModel::whereIn('id', $idarr)->delete();
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }

    // 参数设置
    public function param()
    {
        $param = UserParamModel::where(1)->findOrEmpty();
        return view('user/param', [
            'param' => $param
        ]);
    }
    
    public function editParam()
    {
        $param = UserParamModel::where(1)->findOrEmpty();
        $data = array(
            'redmoney'		=> input('redmoney/d', 0),
            'usernumber'	=> input('usernumber/d', 0),
            'shareintegral'	=> input('shareintegral/d', 0),
            'postintegral'	=> input('postintegral/d', 0),
            'inttomoney'	=> input('inttomoney/d', 0),
            'minintegral'	=> input('minintegral/d', 0),
            'intrecharge'	=> input('intrecharge/d', 0),
			'picregworker'	=> input('picregworker/s', ""),
            'intregworker'	=> input('intregworker/d', 0)
        );
        if ($param->isEmpty()) {
            UserParamModel::create($data);
        } else {
            $data['id'] = $param->id;
            UserParamModel::update($data);
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'   => ""
        )));
    }



    // 用户组
    public function willList()
    {
        return view('user/willlist');
    }

    public function willForm()
    {
        $id = input('id/d, 0');
        $will = UserWill::findOrEmpty($id);
        return view('user/willform', [
            'will' => $will
        ]);
    }

    public function listwill()
    {
        $limit = input('limit');
        $page = input('page');
        $list = UserWill::order(['isdefault'=>'desc','id'=>'asc'])->limit($limit)->page($page)->append(['isdefault_text'])->select();
        $count = UserWill::count();
        if ($count==0) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "未查询到数据"
            )));
        }
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> "",
            'count' => $count,
            'data'  => $list
        )));
    }

    public function editwill()
    {
        $id = input('id/d');
        if (empty($id)) {
            UserWill::create([
                'title' 	=> input('title/s'),
                'isdefault' => input('isdefault/d')==2 ? 2 : 1
            ]);
        } else {
            $administer = UserWill::find($id);
            $administer->save([
                'title' 	=> input('title/s'),
                'isdefault' => input('isdefault/d')==2 ? 2 : 1
            ]);
        }
        exit(json_encode(array(
            'code' 	=> 0
        )));
    }

    public function delwill()
    {
        $access_admin = session('access_admin');
        $password = input('password');
        if ($access_admin['password'] !== md5($password)) {
            exit(json_encode(array(
                'code' 	=> 1,
                'msg'	=> "操作密码验证失败"
            )));
        }
        $idarr = input('idarr/a');
        UserWill::whereIn('id', $idarr)->delete();
        exit(json_encode(array(
            'code' 	=> 0,
            'msg'	=> ""
        )));
    }

}
