package com.gci.boot.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.common.util.QueryWrapperUtil;
import com.gci.boot.common.vo.Result;
import com.gci.boot.exception.GciBootException;
import com.gci.boot.modules.system.entity.SysLog;
import com.gci.boot.modules.system.entity.SysRole;
import com.gci.boot.modules.system.service.ISysLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;

/**
 * <p>
 * 系统日志表 前端控制器
 * </p>
 *
 * @author chenzhiwei
 * @since 2023-09-08
 */
@RestController
@RequestMapping("/sys/log")
public class SysLogController {

    @Autowired
    private ISysLogService sysLogService;

    /**
     * @param syslog
     * @param pageNo
     * @param pageSize
     * @return
     * @功能：查询日志记录
     */
    @GetMapping("/list")
    public Result<IPage<SysLog>> queryPageList(SysLog syslog, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        String keyWord = req.getParameter("keyWord");
        if (StringUtils.isNotBlank(keyWord)) {
            syslog.setLogContent(keyWord);
        }
        QueryWrapper<SysLog> queryWrapper = QueryWrapperUtil.builder()
                .table(SysLog.class)
                .like(SysLog::getLogContent)
                .orderColums(req.getParameter("column"))
                .orders(req.getParameter("order"))
                .buildFromObject(syslog);
        String createTimeBegin = req.getParameter("createTime_begin");
        String createTimeEnd = req.getParameter("createTime_end");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            if (StringUtils.isNotBlank(createTimeBegin)) {
                queryWrapper.ge("create_time", dateFormat.parse(createTimeBegin + " 00:00:00"));
            }
            if (StringUtils.isNotBlank(createTimeEnd)) {
                queryWrapper.le("create_time", dateFormat.parse(createTimeEnd + " 23:59:59"));
            }
        } catch (ParseException e) {
            throw new GciBootException("日期出错！");
        }
        //创建时间/创建人的赋值
        IPage<SysLog> pageList = sysLogService.page(new Page<>(pageNo, pageSize), queryWrapper);

        return Result.ok(pageList);
    }

    /**
     * @param id
     * @return
     * @功能：删除单个日志记录
     */
    @DeleteMapping("/delete")
    public Result<SysLog> delete(String id) {
        sysLogService.removeById(id);
        return Result.ok("删除成功！");
    }

    /**
     * @param ids
     * @return
     * @功能：批量，全部清空日志记录
     */
    @DeleteMapping("/deleteBatch")
    public Result<SysRole> deleteBatch(String ids) {
        this.sysLogService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("删除成功！");
    }
}
