package com.jpsoft.shinestar.modules.sys.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.sys.entity.SysLog;
import com.jpsoft.shinestar.modules.sys.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/sys/log")
@Api(description = "系统日志")
public class LogController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysLogService sysLogService;

    @GetMapping("add")
    @Transactional(rollbackFor = Exception.class)
    public MessageResult<SysLog> add() throws Exception{
        MessageResult<SysLog> messageResult = new MessageResult<>();

        SysLog sysLog = new SysLog();
        sysLog.setUserId("test");
        messageResult.setData(sysLog);

        int affectCount = sysLogService.insert(sysLog);

        logger.warn("affectCount=" + affectCount);

        if (affectCount>0){
            throw new Exception("回滚测试");
        }

        return messageResult;
    }

    @ApiOperation(value="列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户编号", paramType = "form"),
            @ApiImplicitParam(name = "url",value = "url",  paramType = "form"),
            @ApiImplicitParam(name = "remoteIP",value = "远程IP", paramType="form"),
            @ApiImplicitParam(name = "startTime",value = "开始时间", paramType="form"),
            @ApiImplicitParam(name = "endTime",value = "结束时间", paramType="form"),
            @ApiImplicitParam(name = "elapseMin",value = "耗时大于（毫秒）", paramType="form"),
            @ApiImplicitParam(name = "elapseMax",value = "耗时小于（毫秒）", paramType="form"),
            @ApiImplicitParam(name = "pointcut",value = "切入口", paramType="form"),
            @ApiImplicitParam(name = "remoteIP",value = "远程IP", paramType="form"),
            @ApiImplicitParam(name = "remark",value = "备注", paramType="form")
    })
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public MessageResult<Map> pageList(
            String userId,
            String url,
            String remoteIP,
            Long elapseMin,
            Long elapseMax,
            String pointcut,
            String remark,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") Date endTime,
            @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
            @RequestParam(value="pageSize",defaultValue="20") int pageSize){
        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParams = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time","desc"));

        if (StringUtils.isNotEmpty(userId)) {
            searchParams.put("userId",userId);
        }

        if (StringUtils.isNotEmpty(url)) {
            searchParams.put("url",url + "%");
        }

        if (StringUtils.isNotEmpty(remoteIP)) {
            searchParams.put("remoteIP",remoteIP + "%");
        }

        if (StringUtils.isNotEmpty(pointcut)) {
            searchParams.put("pointcut",pointcut + "%");
        }

        if (startTime!=null) {
            searchParams.put("startTime",startTime);
        }

        if (endTime!=null) {
            searchParams.put("endTime",endTime);
        }

        if (elapseMin!=null){
            searchParams.put("elapseMin",elapseMin);
        }

        if (elapseMax!=null){
            searchParams.put("elapseMax",elapseMax);
        }

        if (StringUtils.isNotEmpty(remark)) {
            searchParams.put("remark","%" + remark + "%");
        }

        Page<SysLog> page = sysLogService.pageSearch(searchParams,pageIndex,pageSize,sortList);

        msgResult.setResult(true);
        msgResult.setData(PojoUtils.pageWrapper(page));

        return msgResult;
    }
}
