package com.app.admin.framework.log.controller;

import com.app.admin.framework.log.model.SysOperationLog;
import com.app.admin.framework.log.service.SysOperationLogService;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.controller.CrudBaseControllerSupport;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.grid.Paging;
import com.app.framework.core.utils.request.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 日志管理
 */
@Controller
@RequestMapping("/sys/log")
public class SysOperationLogController extends CrudBaseControllerSupport<SysOperationLog> {

    @Autowired
    SysOperationLogService sysOperationLogService;

    /**
     * 查询列表的具体实现
     * 请求：list
     *
     * @param request HttpServletRequest
     * @param view    查询条件
     * @param paging  分页条件
     * @return 查询结果
     */
    @Override
    public List<SysOperationLog> list(HttpServletRequest request, SysOperationLog view, Paging paging) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        return sysOperationLogService.getPageList(view, loginUser, paging);
    }

    /**
     * 保存记录的具体实现
     * 请求：saveEntity
     *
     * @param request HttpServletRequest
     * @param entity  实体
     * @return 影响结果数
     */
    @Override
    public AjaxResult saveEntity(HttpServletRequest request, SysOperationLog entity) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        entity.setOrganizationId(loginUser.getOrganizationId());
        boolean resultCount = sysOperationLogService.saveOperationLog(entity);
        return AjaxResult.wrap(resultCount);
    }

    /**
     * 删除记录,根据Id 具体实现
     * 请求：deleteById 参数oid
     * 请求：batchDelete 参数oid数组
     *
     * @param request HttpServletRequest
     * @param oids    实体
     * @return 影响结果数
     */
    @Override
    public AjaxResult batchDelete(HttpServletRequest request, Long oids[]) {
        boolean b = sysOperationLogService.removeByIds(Arrays.stream(oids).collect(Collectors.toList()));
        return AjaxResult.wrap(b);
    }

    /**
     * 跳转到列表页面
     */
    @RequestMapping(value = "/golist")
    public ModelAndView golist(HttpServletRequest request) {
        return new ModelAndView("admin/framework/system/log/list.html");
    }
}
