// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ActionLogModel; use App\Models\InstitutionalModel; use App\Services\BaseService; use App\Services\RedisService; /** * 制度参数管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class InstitutionalService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new InstitutionalModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取列表 * @param $params 参数 * @param int $pageSize 分页大小:默认 15 * @return array */ public function getDataList($params, $pageSize = 10, $field = []) { $query = $this->getQuery($params); $list = $query->select($field ? $field : ['a.*']) ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $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 $params * @return \Illuminate\Database\Eloquent\Builder */ public function getQuery($params) { $where = ['a.mark' => 1]; return $this->model->from('institutional as a') ->where($where) ->where(function ($query) use ($params) { $kw = isset($params['keyword']) ? trim($params['keyword']) : ''; if ($kw) { $query->where('a.name', 'like', "%{$params['keyword']}%"); } }) ->where(function ($query) use($params){ $type = isset($params['type'])? $params['type'] : 0; if ($type) { $query->where('a.type', $type); } $status = isset($params['status'])? $params['status'] : 0; if (is_array($status)) { $query->whereIn('a.status', $status); } else if($status){ $query->where('a.status', $status); } }); } /** * 质押轮次奖励参数 * @param $round 轮次 * @return array|mixed */ public function getAwardByRound($round) { $cacheKey = "caches:config:institutional_round_{$round}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model->where('condition_num','<=', $round) ->where(['type'=>2,'status'=>1,'mark'=>1]) ->select(['id','name','type','condition_num as round','value','status']) ->orderBy('condition_num','desc') ->first(); $data = $data? $data->toArray() : []; if($data){ RedisService::set($cacheKey, $data, 300, 600); } return $data; } /** * 推荐奖励制度参数 * @return array|mixed */ public function getAwardByFloor() { $cacheKey = "caches:config:institutional_floor"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model->where('floor','>', 0) ->where(['type'=>1,'status'=>1,'mark'=>1]) ->select(['id','name','type','floor','value','status']) ->orderBy('floor','asc') ->get() ->keyBy('floor'); $data = $data? $data->toArray() : []; if($data){ RedisService::set($cacheKey, $data, 300, 600); } return $data; } /** * 修改 * @return array */ public function edit() { ActionLogModel::setTitle("修改设置制度参数"); ActionLogModel::record(); return parent::edit(); // TODO: Change the autogenerated stub } }