// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\ComplaintModel; use App\Services\BaseService; use App\Services\RedisService; /** * 投诉建议管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class ComplaintService * @package App\Services\Common */ class ComplaintService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * ComplaintService constructor. */ public function __construct() { $this->model = new ComplaintModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; $userType = isset($params['user_type'])? $params['user_type'] : 0; if($status>0){ $where['a.status'] = $status; } if($type>0){ $where['a.type'] = $type; } if($userType>0){ $where['a.user_type'] = $userType; } $list = $this->model->from('complaint as a') ->leftJoin('member as b', 'b.id','=','a.user_id') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->where('a.content','like',"%{$keyword}%"); } }) ->select(['a.*']) ->orderBy('a.create_time','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ foreach($list['data'] as &$item){ $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : ''; } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 获取文章详情 * @param $id * @return array|mixed */ public function getInfo($id) { $cacheKey = "caches:aomplaint:info_{$id}"; $info = RedisService::get($cacheKey); if($info){ return $info; } $info = $this->model->where(['id'=> $id,'mark'=>1]) ->select(['id','title','albums','user_id','type','content']) ->first(); $info = $info? $info->toArray() : []; if($info){ $info['albums'] = $info['albums']? get_images_preview($info['albums']) : []; $info['content'] = htmlspecialchars_decode($info['content']); RedisService::set($cacheKey, $info, rand(5,10)); } return $info; } /** * 提交内容 * @param $userId * @param $params * @return mixed */ public function submit($userId, $params) { $albums = isset($params['albums'])? get_format_images($params['albums'],'') : ''; $data = [ 'user_id'=> $userId, 'title'=> isset($params['title'])? trim($params['title']) : '', 'user_type'=> isset($params['user_type']) && $params['user_type']? intval($params['user_type']) : 1, 'mobile'=> isset($params['mobile'])? trim($params['mobile']) : '', 'content'=> isset($params['content'])? trim($params['content']) : '', 'type'=> isset($params['type'])? intval($params['type']) : 1, 'albums'=> $albums, 'create_time'=> time(), 'update_time'=> time(), 'status'=> 1, 'mark'=> 1, ]; return $this->model->insertGetId($data); } }