<?php
/**
 *------Create thems Model------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-12 05:08:18------
 */
namespace App\Api\Model;

use \System\Model;
use Biz\Account\AccountManager;

class AccountNews extends Model {

    protected $table = 'account_news';

    /**
     * 用户消息
     *
     * @access public
     * @return String
     */
    public function userNews()
    {
        // 获取用户信息
        $accountManagerClass = new AccountManager;
        $userInfo = $accountManagerClass -> getCurrentUser();
        if (empty($userInfo['identity'])) {
            Render([], '2001', lang('Common','Api') -> get('user does login'));
        }
        $accountNewsWhere['account_identity'] = $userInfo['identity'];
        // 查询条件
        if (!empty($_POST['read_status'])) {
            $accountNewsWhere['read_status'] = $_POST['read_status'];
        }
        if (!empty($_POST['type'])) {
            $accountNewsWhere['type'] = $_POST['type'];
        }
        // 时间区间
        $startTime = isset($_POST['startTime'])? $_POST['startTime'] : '1900-01-01 00:00:00';
        $endTime = isset($_POST['endTime'])? $_POST['endTime'] : '3000-12-12 00:00:00';
        $accountNewsBetween = ['write_time' => [$startTime, $endTime]];
        // 当前页
        $currentPage = isset($_POST['currentPage']) ? $_POST['currentPage'] : "1";
        // 分页大小
        $pageSize = isset($_POST['pageSize']) ? $_POST['pageSize'] : "10";
        // 起始查询位置
        $begin = ($currentPage - 1) * $pageSize;
        $accountNewsMdl = lm('account_news', "commons");
        // 获取消息
        $accountNewsSlt = ['title', 'identity', 'content', 'details', 'write_time', 'read_status', 'type'];
        $getMoneyBuyStr = $accountNewsMdl -> accountNews($accountNewsSlt, $accountNewsWhere, $accountNewsBetween, $begin, $pageSize, 1);
        // 获取消息总数
        $result['total'] = $accountNewsMdl -> accountNewsTotal($accountNewsWhere, $accountNewsBetween);
        $result['list'] = $getMoneyBuyStr;
        // 总页数计算
        $result['countPage'] = ceil($result['total'] / $pageSize);
        $result['currentPage'] = $currentPage;

        return $result;
    }
}
