<?php
/**
 * Created by PhpStorm.
 * User: fuju
 * Date: 2018/1/20
 * Time: 17:52
 */

namespace app\model;
use think\Db;

class AAgents extends BaseModel
{

public function saveList(){

}

    //分页列表
    public function getListJoin($p = 1, $pageSize = 15, $order_ = 'id desc', $field = '',$join='', $where = '')
    {
        $data = $this->field($field)
            ->alias('a')
            ->join($join)
            ->where($where)
            ->order($order_)
            ->limit($pageSize)
            ->page($p)
            ->select();
        return $data;
    }

    /**
     *检查重复
     *
     */
    public function repetition($name,$key){
        $r=$this->field($name)
            ->where($name,'=',$key)
            ->select();
        //$this->getLastSql();
        if($r){
            return true;
        }else{
            return false;
        }
    }
    //更新数据
    public function saveStatus($name,$key,$ids){
      $r = $this->where("id",'in',$ids)->update([$name=>$key]);
       return $r;
    }
//删除数据
    public function  delUid($name,$ids){
        $this->where($name,'in',$ids)->delete();
    }
    //通过ids批量添加数据
    public  function addAllAccess($ids,$data){
        $idarr=explode($ids);
        if(is_array($idarr)){
            $arr=array();
            foreach ($idarr as $v){
                $data['uid']=$v;
                 $arr[]=$data;
            }
           $r= $this->saveAll($arr);
          }else{
            $data['uid']=$ids;
           $r= $this->save($data);
        }
        return $r;
    }

    /**
     * 记录总数
     *
     * @param $params
     * @return int|string
     */
    public function getTotal2($params)
    {
        return  $this->alias('a')->where($params)->count();
    }

    /**
     * 操作盘方记录
     *
     * @param int $pageNo
     * @param int $pageSize
     * @param string $order_
     * @param string $field
     * @param string $params
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRecords($pageNo = 1, $pageSize = 15, $order_ = 'id desc', $field = '', $params = '')
    {
        $data = Db::table('g_operating_records')
            ->field($field)
            ->where($params)
            ->limit($pageSize)
            ->order($order_)
            ->page($pageNo)
            ->select();
        return $data;
    }

    public function verifyUser($field,$join,$params)
    {
       
       $r= $this->field($field)
             ->alias('a')
             ->join($join) 
            ->where($params)
            ->find();
       // echo $this->getLastSql();
       return $r;
    }

    /**
     * 总监列表
     *
     * @param int $pageNo
     * @param int $pageSize
     * @param string $order_
     * @param string $field
     * @param string $params
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListDistrict($pageNo = 1, $pageSize = 15, $order_ = 'id desc', $field = '', $params = '') {
        $data = $this->field($field)
            ->where($params)
            ->order($order_)
            ->limit($pageSize)
            ->page($pageNo)
            ->select();
        $district_where['status'] = 0;
        $store_where['status']    = 0;
        foreach ($data as $k=>$v){
            $result[$k] = $v;
            if (isset($v->district_id)) {
                $district_where['id'] = $v->district_id;
                $result[$k]['district_name'] = Db::table('a_district')->where($district_where)->value('district_name');
                if ($result[$k]['district_name']) {
                    $store_where['district_id']  = $v->district_id;
                    $result[$k]['store_name'] = Db::table('a_store')->where($store_where)->column('store_name');
                } else {
                    $result[$k]['store_name'] = '';
                }
            }
        }
        return $result;
    }

}