', 0]; } else { $where[] = ['change_amount', '<', 0]; } } return $where; } /** * @description: 查询单条数据 * @param array $search * @return */ public static function findOne(array $search) { return static::model()::where(static::getWhere($search))->first(); } /** * @description: 查询所有数据 * @param array $search * @return \Illuminate\Database\Eloquent\Collection */ public static function findAll(array $search = []) { return static::model()::where(static::getWhere($search))->get(); } /** * @description: 分页查询 * @param array $search * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public static function paginate(array $search = []) { $limit = isset($search['limit']) ? $search['limit'] : 15; $paginator = static::model()::where(static::getWhere($search)) ->orderBy('updated_at', 'desc') ->paginate($limit); return ['total' => $paginator->total(), 'data' => $paginator->items()]; } /** * @description: * @param {*} $params * @return {*} */ public static function submit($params = []) { $result = false; $msg['code'] = self::NOT; $msg['msg'] = ''; // 2. 判断是否是更新 if (!empty($params['id'])) { // 更新 $info = self::findOne(['id' => $params['id']]); if (!$info) { $msg['msg'] = '数据不存在!'; } else { $result = $info->update($params); $id = $params['id']; } } else { // 创建 $result = $info = self::model()::create($params); $id = $result->id; } if ($result) { $msg['code'] = self::YES; $msg['msg'] = '设置成功'; } else { $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg']; } return $msg; } /** * 添加日志 * * @param int $userId * @param string|null $changeType 类型 * @param string|null $beforeBalance 变动前余额 * @param string|null $changeAmount 变动金额 * @param string|null $remark 备注 * @return array */ public static function addLog($userId, $changeType = '', $beforeBalance = '', $changeAmount = '', $remark = '', $withdrawsId = null) { $afterBalance = bcadd($beforeBalance, $changeAmount, 4); $params = [ 'user_id' => $userId, 'change_type' => $changeType, 'before_balance' => $beforeBalance, 'change_amount' => $changeAmount, 'after_balance' => $afterBalance, 'remark' => $remark, 'withdraws_id' => $withdrawsId ]; return self::submit($params); } }