/**
  * 获取已申请的学生列表.
  * $where 参数项: selectYear(integer), startDate(string), endDate(string), dept(integer), firstName(string), 
  * familyName(string),  birth(string)
  * 返回包含 2 项: lists 表示列表数据, pageList 是分页字符串
  * 
  * @access private
  * @param array $where
  * @param boolean $isPage 是否分页, 默认 TRUE
  * @param integer $pageSize 每页显示的数据条数, 默认 20
  * @return array
  * @author Liuping <*****@*****.**>
  */
 private function fetchApplyStuList_bak($where, $isPage = TRUE, $pageSize = 20)
 {
     // invalid : 0 有效单, is_complete : 0表示未完成,1表示已完成
     $startDate = dateToTimestamp($where['startDate']) === FALSE ? 0 : dateToTimestamp($where['startDate']);
     $endDate = dateToEndTimestamp($where['endDate']) === FALSE ? 0 : dateToEndTimestamp($where['endDate']);
     $deptid = $where['dept'];
     $firstName = $where['firstName'];
     $familyName = $where['familyName'];
     $birth = dateToTimestamp($where['birth']) === FALSE ? 0 : dateToTimestamp($where['birth']);
     $selectYear = $where['selectYear'];
     $uid = session('uid');
     $binds = [];
     $where = "a.invalid=0 AND a.is_complete=1";
     // 有效的且已填写完成的申请单
     if (!empty($selectYear)) {
         $where .= " AND a.year=:year";
         $binds[':year'] = $selectYear;
     }
     if (!empty($startDate)) {
         $where .= " AND a.c_time >= :startDate";
         $binds[':startDate'] = $startDate;
     }
     if (!empty($endDate)) {
         $where .= " AND a.c_time <= :endDate";
         $binds[':endDate'] = $endDate;
     }
     if (is_numeric($deptid)) {
         //$where .= " AND (a.partid = :partid OR a.uid=:uid)";
         $where .= " AND (a.partid = :partid)";
         $binds[':partid'] = $deptid;
         //$binds[':uid'] = $uid;
     } else {
         // 有读取权限的部门列表
         $allowDepts = AgentAuth::getAllowParts(['r']);
         $partInfo = [];
         // 有权限操作的部门键值对 id => name
         foreach ($allowDepts as $row) {
             $partInfo[$row['id']] = $row['name'];
         }
         unset($allowDepts);
         $ids = array_keys($partInfo);
         if (!empty($ids)) {
             $ids = implode(",", $ids);
             $where .= " AND (a.uid=:uid OR a.partid IN ({$ids}))";
             $binds[':uid'] = $uid;
         } else {
             $where .= " AND a.uid=:uid";
             $binds[':uid'] = $uid;
         }
     }
     if (!empty($familyName)) {
         //$where .= " AND a.family_name = :familyName";
         $where .= " AND a.family_name LIKE '%{$familyName}%'";
         //$binds[':familyName'] = $familyName;
     }
     if (!empty($firstName)) {
         // $where .= " AND a.first_name = :firstName";
         $where .= " AND a.first_name LIKE '%{$firstName}%'";
         //             $binds[':firstName'] = $firstName;
     }
     if (!empty($birth)) {
         $where .= " AND a.birth = :birth";
         $binds[':birth'] = $birth;
     }
     // c.num 已申请数量
     $model = M('apply_status');
     $fields = ['a.id', 'a.uid', 'a.fuid', 'a.partid', 'a.s_no', 'a.a_no', 'a.family_name', 'a.first_name', 'a.birth', 'b.name AS part_name', 'a.c_time', 'c.num AS apply_num'];
     $result = ['lists' => [], 'pageList' => ''];
     $info = [];
     $pageList = '';
     // 是否有分页
     if ($isPage) {
         $totalRows = $model->alias('a')->field($fields)->join("LEFT JOIN __AGENT_PART__ AS b ON a.partid=b.id")->join("LEFT JOIN __APPLY_STUDENT__ AS c ON a.s_no=c.s_id")->where($where)->bind($binds)->count();
         // 请求的总页数大于分页的总页数, 设置为最后一页
         $totalPage = ceil($totalRows / $pageSize);
         $varPage = C('VAR_PAGE');
         if (isset($_GET[$varPage]) && intval($_GET[$varPage]) > $totalPage) {
             $_GET[$varPage] = $totalPage;
         }
         // 实例化分页类对象
         $pager = new \Think\Page($totalRows, $pageSize);
         $pager->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%');
         $info = $model->alias('a')->field($fields)->join("LEFT JOIN __AGENT_PART__ AS b ON a.partid=b.id")->join("LEFT JOIN __APPLY_STUDENT__ AS c ON a.s_no=c.s_id")->where($where)->bind($binds)->order('a.id DESC')->limit($pager->firstRow . ',' . $pager->listRows)->select();
         $pageList = $pager->show();
     } else {
         $info = $model->alias('a')->field($fields)->join("LEFT JOIN __AGENT_PART__ AS b ON a.partid=b.id")->join("LEFT JOIN __APPLY_STUDENT__ AS c ON a.s_no=c.s_id")->where($where)->bind($binds)->order('a.id DESC')->select();
     }
     if (FALSE === $info) {
         $info = [];
     }
     $result['lists'] = $info;
     $result['pageList'] = $pageList;
     return $result;
 }
 /**
  * ajax 按申请信息搜索学生.
  *
  * @access public
  * @return void
  * @author Liuping <*****@*****.**>
  */
 public function ajaxApplySearch()
 {
     $post = I('post.');
     // 默认条件值
     $_status_default = ['a_status' => [], 'o_status' => [], 'c_status' => [], 'invalid' => []];
     $_other_default = ['firstAppStartDate' => '', 'firstAppEndDate' => '', 'lastAppStartDate' => '', 'lastAppEndDate' => '', 'school' => '', 'refNo' => '', 'appYear' => ''];
     $_allowSortFields = ['appNum', 'offerNum', 'firstAppDate', 'lastAppDate'];
     // 允许排序的字段
     $_sort_default = ['field' => 'lastAppDate', 'sortby' => 'DESC'];
     // 默认用这个字段排序
     // status 条件
     $statusCond = isset($post['status']) ? $post['status'] : [];
     $statusCond = is_array($statusCond) ? $statusCond : [];
     $statusCond = array_merge($_status_default, $statusCond);
     // 合并 status 条件
     // other 条件
     $otherCond = isset($post['other']) ? $post['other'] : [];
     $otherCond = is_array($otherCond) ? $otherCond : [];
     $otherCond = array_merge($_other_default, $otherCond);
     // 判断几个日期是否有值并转换成时间戳
     $_firstStartDate = $otherCond['firstAppStartDate'];
     $_firstEndDate = $otherCond['firstAppEndDate'];
     $_lastStartDate = $otherCond['lastAppStartDate'];
     $_lastEndDate = $otherCond['lastAppEndDate'];
     if (!empty($_firstStartDate)) {
         $_firstStartDate = dateToTimestamp($_firstStartDate, '/');
         $_firstStartDate = $_firstStartDate === FALSE ? '' : $_firstStartDate;
     } else {
         $_firstStartDate = '';
     }
     if (!empty($_firstEndDate)) {
         $_firstEndDate = dateToEndTimestamp($_firstEndDate, '/');
         $_firstEndDate = $_firstEndDate === FALSE ? '' : $_firstEndDate;
     } else {
         $_firstEndDate = '';
     }
     if (!empty($_lastStartDate)) {
         $_lastStartDate = dateToTimestamp($_lastStartDate, '/');
         $_lastStartDate = $_lastStartDate === FALSE ? '' : $_lastStartDate;
     } else {
         $_lastStartDate = '';
     }
     if (!empty($_lastEndDate)) {
         $_lastEndDate = dateToEndTimestamp($_lastStartDate, '/');
         $_lastEndDate = $_lastEndDate === FALSE ? '' : $_lastEndDate;
     } else {
         $_lastEndDate = '';
     }
     $otherCond['firstAppStartDate'] = $_firstStartDate;
     $otherCond['firstAppEndDate'] = $_firstEndDate;
     $otherCond['lastAppStartDate'] = $_lastStartDate;
     $otherCond['lastAppEndDate'] = $_lastEndDate;
     // 申请年份
     $appYear = isset($otherCond['appYear']) ? $otherCond['appYear'] : date('Y', time());
     $appYear = is_numeric($appYear) ? intval($appYear) : intval(date('Y', time()));
     // 排序
     $sortCond = isset($post['sort']) ? $post['sort'] : [];
     $sortCond = is_array($sortCond) ? $sortCond : [];
     $sortCond = array_merge($_sort_default, $sortCond);
     // 最终条件
     $post = ['sort' => $sortCond, 'other' => $otherCond, 'status' => $statusCond];
     $service = new StudentApplySearchService();
     $result = $service->applySearch($post, TRUE, 20);
     $this->assign('arrSort', $sortCond);
     $this->assign('lists', $result['lists']);
     $this->assign('pageList', $result['pageList']);
     $this->assign('totalList', $result['totalList']);
     $this->display();
 }