/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.base.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.base.api.entity.Log;
import com.tofly.base.api.feign.BaseEsLogService;
import com.tofly.base.service.LogService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.auth.innerCall;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志信息表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@RestController
@RequestMapping("/log")
@Api(tags="日志管理")
@Slf4j
public class LogController {
    private static final Logger logs = org.slf4j.LoggerFactory.getLogger(LogController.class);

    @Autowired
    private  LogService logService;

    @Autowired
    private BaseEsLogService baseEsLogService;

    @Value("${isUseEs:false}")
    private boolean isUseEs;

    /**
    * 分页查询
    * @param page 分页对象
    * @param log 日志信息表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getLogPage(Page page, @ModelAttribute Log log) {
      return ResultRespone.success(logService.page(page, Wrappers.query(log)));
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @return
     */
    @GetMapping("/pageNew" )
    @ApiOperation(value = "分页查询-新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型", required = false, dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "开始日期", required = false, dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", required = false, dataType = "String")
    })
    public ResultRespone getLogPageNew(Page page,String type,String startDate,String endDate ) {

        System.out.println(isUseEs);

        Map map=new HashMap();
        map.put("type",type);
        map.put("startDate", StringUtil.isEmpty(startDate)?null:startDate+" 00:00:00");
        map.put("endDate",StringUtil.isEmpty(endDate)?null:endDate+" 23:59:59");
        if(isUseEs){
            log.info("开启了es，日志查询从es");
            Log log=new Log();
            log.setType(type);
//            Map map1=new HashMap();
//            map1.put("page",page);
//            map1.put("logMap",map);
            map.put("size",page.getSize());
            map.put("page",page.getCurrent());
            List list=baseEsLogService.queryLog(map);
            page.setRecords(list);
            return ResultRespone.success(page);
        }
        return ResultRespone.success(logService.listLogPage(page,map));
//        return ResultRespone.success(logService.page(page, Wrappers.<Log>lambdaQuery()
//                .eq(!StringUtil.isEmpty(type),Log::getType, type)
//                //.ge(!StringUtil.isEmpty(startDate),Log::getCreateTime, Timestamp.valueOf(startDate+" 00:00:00"))
//                .ge(!StringUtil.isEmpty(startDate),Log::getCreateTime, !StringUtil.isEmpty(startDate)?DateUtil.getDateFormat(startDate):null)
//                .le(!StringUtil.isEmpty(endDate),Log::getCreateTime, !StringUtil.isEmpty(endDate)?Timestamp.valueOf(endDate+" 23:59:59"):null)
//        ));
    }


    /**
     * 通过id查询日志信息表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(logService.getById(id));
    }

    /**
     * 新增日志信息表
     * @param log 日志信息表
     * @return ResultRespone
     */
    //@ToFlyAppLog(title="新增日志信息表"
    //@ApiOperation(value = "新增日志信息表")
    @innerCall
    @PostMapping("/add")
    public ResultRespone save(@RequestBody Log log) {
        if(isUseEs){
            System.out.println("开启了es，日志存储到es");
            return ResultRespone.success(baseEsLogService.saveLog(log));
        }
        return ResultRespone.success( logService.save(log));
    }

    /**
     * 新增日志信息表
     * @param log 日志信息表
     * @return ResultRespone
     */
    //@ToFlyAppLog(title="新增日志信息表"
    //@ApiOperation(value = "新增日志信息表")
    @PostMapping
    public ResultRespone savethis(@RequestBody Log log) {

        logService.save(log);

        return ResultRespone.success(true);
    }

    /**
     * 修改日志信息表
     * @param log 日志信息表
     * @return ResultRespone
     */
    //@ToFlyAppLog(title="修改日志信息表" )
    //@ApiOperation(value = "修改日志信息表")
    @PutMapping
    public ResultRespone updateById(@RequestBody Log log) {
      return ResultRespone.success(logService.updateById(log));
    }

    /**
     * 通过id删除日志信息表
     * @param id id
     * @return ResultRespone
     */
    //@ToFlyAppLog(title="通过id删除日志信息表" )
    //@ApiOperation(value = "通过ID删除日志信息表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
      return ResultRespone.success(logService.removeById(id));
    }
}
