<?php

/**
 * +----------------------------------------------------------------------
 * | Created By PhpStorm
 * +----------------------------------------------------------------------
 * | DATE: 2021/7/14
 * +----------------------------------------------------------------------
 * | TIME: 17:14
 * +----------------------------------------------------------------------
 * | Author: YangXing(925032253@qq.com)
 * +----------------------------------------------------------------------
 * | Class: AdminUser
 * +----------------------------------------------------------------------
 */

namespace model\User;


use model\authoritySystem;
use model\collocation;
use model\user;

/**
 * Class AdminUser
 * @package model\User
 */
class adminUser extends \model
{

    public $table = 'adm_user';

    public $key = 'au_id';

    /**
     * @var collocation
     */
    private $collocation;

    /**
     * userLinkRole constructor.
     * @param null $db
     */
    public function __construct($db = null)
    {
        parent::__construct($db);
        $this->collocation = new collocation();
    }

    /**
     * @param $rules
     * @param string $where
     * @param int $page
     * @param int $pagesize
     * @param string $sidx
     * @param string $sord
     * @param string $orderby
     * @return array
     */
    public function list($rules, $where='',$page=0,$pagesize=0,$sidx='',$sord='asc',$orderby=''){


        $where = $this->filter2medoo($where,function(&$field,&$op,&$data){
            switch($field){
                case 'au_area':
                case 'au_depa':
                    $op = 'eq';
                    $data = $this->collocation->like($data);
                    if(empty($data)) $data = [0];
                    break;
                case 'au_status':
                    if($data == '开'){
                        $op = 'eq';
                        $data = 'on';
                    }else if($data == '关'){
                        $op = 'eq';
                        $data = 'off';
                    }
                    break;
            }
        });

        $where = $this->listWhere($where,$page,$pagesize,$sidx,$sord,$orderby);
        $where['au_id[!]'] = 'gsp.admin';
        if($page>0 && $pagesize>0){
            $pages = $this->paging($this->table,$where,$page,$pagesize);
            $where["LIMIT"] = [$pages['limit'],$pagesize];
        }

        $fields = [
            'au_id',
            'au_name',
            'au_depa',
            'au_area',
            'au_jobno',
            'au_status',
            'au_remark',
            'temp',
            'au_outid',
            'au_create_time',
            'au_update_time',
        ];
        $rows = $this->db->select($this->table,$fields,$where);
        foreach ($rows as &$item)
        {
            $item['au_status'] = $item['au_status'] == 'on' ? '开' : '关' ;
        }
        $data = [
            'page'=> $page,
            'total'=> $pages['total'],
            'records' => $pages['records'],
            'rows' => $rows
        ];
        return $data;
    }

    /**
     * @param $pass
     * @return bool
     */
    public function getIsExistByOriginalPass($pass)
    {
        $auth = new authoritySystem();
        return $this->db->has($this->table,['au_id' => $this->getOpuser(),'au_pass' => (new user())->pass2(getOpuser(),$pass)]);
    }

    /**
     * @param $
     * @return array
     */
    public function getItem($key, $isCaption=false){
        $row = $this->getRowByKey($key);
        if($isCaption){
            $row = $this->formartRow($row);
        }
        return $row;
    }

    /**
     * @param int|array $row
     * @return array
     */
    public function formartRow($row){
        if(!is_array($row)){
            $row = $this->getRowByKey($row);
        }
        $collocation = new \model\collocation();
        $crow = $collocation->getRowByKey($row['au_depa']);
        $row['captions']['au_depa'] = $crow[LG_NAME];

        $crow = $collocation->getRowByKey($row['au_area']);
        $row['captions']['au_area'] = $crow[LG_NAME];
        return $row;
    }
}
