<?php

namespace App\Modules\CpUser\Services;

use App\Modules\CpUser\Models\CpUser;


class CpUserService
{
  
    /**
     * 得到用户
     *
     */
    public static function getCpUser($username) {
        return CpUser::getCpUser($username);
    }
    
    /**
     * 得到用户by id
     */
    public static function getCpUserById($id) {
    	return CpUser::getCpUserById($id);
    }
    
    /**
     * 修改Cp密码
     * @param  string $username   用户名
     * @param  string $password 加密后密码
     * @return boolen
     */
    static function modifyPassword($username, $password)
    {
    	return CpUser::modifyPassword($username, $password);
    }


    static function createCp($username){
        try{
            $password = $username.'123456';
            $pass = md5($password."^-^zhuishuyun^_^");
            return CpUser::create([
                'username'=>$username,
                'password'=>$pass,
                'is_enabled'=>1,
                'is_show_total_amount'=>0
            ]);
        }catch (\Exception $e){

        }
        return false;
    }

    static function getAllCpUser($param,$is_all=false){
        if($is_all){
            return CpUser::all();
        }
        $where = [];
        if(isset($param['cp']) && !empty(($param['cp']))){
            $where[] = ['username','like','%'.trim($param['cp']).'%'];
        }

        return CpUser::where($where)->orderBy('id','desc')->paginate(20);
    }
}
