| 1 |
- <?php
/**
* 信息服务
* @author wesmielr
*/
namespace app\index\service;
use think\Db;
class MessageService
{
/**
* 获取咨询留言列表
* @param $params 参数
* @param int $pageSize 记录大小
* @param string $field 返回字段
* @return $this
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public static function getList($params, $pageSize=10, $field=''){
$field = $field? $field : 'id,jm_id,jm_title,name,address,ip,content,reply';
$cacheKey = "cache:messages:list:" . md5(json_encode($params).$pageSize.$field);
$dataList = RedisService::get($cacheKey);
if ($dataList) {
return $dataList;
}
$dataList = Db::name('message')
->where(function($query) use ($params){
$type = isset($params['type'])? $params['type'] : 0;
if($type){
$query->where('type', $type);
}
})
->field($field)
->order('create_time desc')
->limit($pageSize)
->select()
->each(function($item, $k){
if(isset($item['name'])){
$item['name'] = $item['name']? formatName($item['name']) : '游客';
}
$item['address'] = isset($item['address'])? $item['address'] : '';
$item['address'] = $item['address']? $item['address'] : (isset($item['ip'])? IpService::getAddress($item['ip']) : '未知地区');
return $item;
});
$dataList = $dataList ? $dataList->toArray() : [];
if ($dataList) {
RedisService::set($cacheKey, $dataList, 1 * 3600);
}
return $dataList;
}
/**
* 获取今天提交次数
* @param $where
*/
public static function getTodyCount($where){
$todaytime = strtotime(date('Y-m-d'));
$cacheKey = "cache:messages:count_".date('Ymd');
$data = RedisService::get($cacheKey);
if ($data) {
return $data;
}
$data = Db::name('message')
->where($where)
->where('create_time','gt',$todaytime)
->count();
if($data){
RedisService::set($cacheKey, $data, 6 * 3600);
}
return $data;
}
/**
* 获取本周提交次数
* @param $where
*/
public static function getWeekCount($where){
// 当前日期
$sdefaultDate = date("Y-m-d");
// $first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$first=1;
// 获取当前周的第几天 周日是0 周一到周六是 1 - 6
$w = date('w', strtotime($sdefaultDate));
// 获取本周开始日期,如果$w是0,则表示周日,减去 6 天
$week_start=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
return Db::name('message')
->where($where)
->where('create_time','gt',$week_start)
->count();
}
}
|