package com.ozs.web.controller.monitor;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.common.core.domain.vo.SysLoginInfoVo;
import com.ozs.common.core.domain.vo.SysOperlogResq;
import com.ozs.common.core.domain.vo.SysOperlogVo;
import com.ozs.common.utils.DateUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.system.domain.SysLoginInfo;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.ozs.common.annotation.Log;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.core.page.TableDataInfo;
import com.ozs.common.enums.BusinessType;
import com.ozs.common.utils.poi.ExcelUtil;
import com.ozs.system.domain.SysOperLog;
import com.ozs.system.service.ISysOperLogService;

/**
 * 操作日志记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController {
    @Autowired
    private ISysOperLogService operLogService;

//    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(SysOperLog operLog)
//    {
//        startPage();
//        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
//        return getDataTable(list);
//    }

    /**
     * 获取操作日志分页列表
     *
     * @param sysOperlogVo
     * @return
     */
    @PreAuthorize("@ss.hasPermi('monitor:operlog:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysOperlogVo sysOperlogVo) {
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<SysOperLog>();
        if (!ObjectUtils.isEmpty(sysOperlogVo.getUserId())) {
            wrapper.like(SysOperLog::getUserId, sysOperlogVo.getUserId());
        }
        if (!ObjectUtils.isEmpty(sysOperlogVo.getOperatorType())) {
            wrapper.like(SysOperLog::getOperatorType, sysOperlogVo.getOperatorType());
        }
        if (!ObjectUtils.isEmpty(sysOperlogVo.getTitle())) {
            wrapper.like(SysOperLog::getTitle, sysOperlogVo.getTitle());
        }
        if (!ObjectUtils.isEmpty(sysOperlogVo.getStartTime())) {
//            wrapper.apply(StringUtils.isNotBlank(sysOperlogVo.getStartTime()), "date_format(oper_time,'%y%m%d') >= date_format('" + sysOperlogVo.getStartTime() + "','%y%m%d')");
            wrapper.apply(StringUtils.isNotBlank(sysOperlogVo.getStartTime()), "TO_CHAR(oper_time, 'YYYYMMDD') >= TO_CHAR('" + sysOperlogVo.getStartTime() + "','YYYYMMDD')");
        }
        if (!ObjectUtils.isEmpty(sysOperlogVo.getEndTime())) {
//            wrapper.apply(StringUtils.isNotBlank(sysOperlogVo.getEndTime()), "date_format(oper_time,'%y%m%d') <= date_format('" + sysOperlogVo.getEndTime() + "','%y%m%d')");
            wrapper.apply(StringUtils.isNotBlank(sysOperlogVo.getEndTime()), "TO_CHAR(oper_time,'YYYYMMDD') <= TO_CHAR('" + sysOperlogVo.getEndTime() + "','YYYYMMDD')");
        }
        if (!ObjectUtils.isEmpty(sysOperlogVo.getBusinessType())) {
            wrapper.eq(SysOperLog::getBusinessType, sysOperlogVo.getBusinessType());
        }
        wrapper.orderByDesc(SysOperLog::getOperTime);
        IPage<SysOperLog> page = operLogService.page(new Page<>(sysOperlogVo.getPageNum(), sysOperlogVo.getPageSize()), wrapper);
        return AjaxResult.success(page);
    }

    @PostMapping("/detail")
    @PreAuthorize("@ss.hasPermi('monitor:operlog:detail')")
    public AjaxResult detail(Long operId) {
        SysOperLog sysOperLog = operLogService.selectOperLogById(operId);
        return AjaxResult.success(sysOperLog);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SysOperlogResq sysOperlogResq) {
        List<SysOperLog> list = operLogService.selectOperLogList(sysOperlogResq);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @RequestMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('monitor:operlog:remove')")
    @RequestMapping("/clean")
    public AjaxResult clean()
    {
        operLogService.cleanOperLog();
        return success();
    }
}
