package com.wms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.constants.enums.ResponseCode;
import com.wms.entity.app.ResponseEntity;
import com.wms.entity.app.dto.PageDto;
import com.wms.entity.app.request.WmsLogQuery;
import com.wms.entity.app.vo.WmsLogVo;
import com.wms.entity.table.WmsLog;
import com.wms.service.LogService;
import com.wms.utils.HttpUtils;
import com.wms.utils.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import static com.wms.utils.StringUtils.convertJsonString;

@Controller
@CrossOrigin
@RequestMapping(value = "/wms/log")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WmsLogController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 请求头部信息
     */
    private final HttpServletRequest servletRequest;
    /**
     * 日志服务
     */
    private final LogService logService;

    /**
     * 查询日志
     *
     * @param logQuery 请求参数
     * @return 结果
     */
    @PostMapping("/queryWmsLog")
    @ResponseBody
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public String getGoodsInfoByPage(@RequestBody WmsLogQuery logQuery){
        logger.info("接收到查询日志请求：{}，请求ip：{}", convertJsonString(logQuery), HttpUtils.getIpAddr(servletRequest));
        ResponseEntity response = new ResponseEntity();
        try {
            Page<WmsLog> page = logQuery.toMpPage();
            Page<WmsLog> logPage = logService.page(page, new LambdaQueryWrapper<WmsLog>()
                    .like(StringUtils.isNotEmpty(logQuery.getQueryParam()), WmsLog::getLogTitle, logQuery.getQueryParam())
                    .or().like(StringUtils.isNotEmpty(logQuery.getQueryParam()), WmsLog::getLogMethod, logQuery.getQueryParam())
                    .or().like(StringUtils.isNotEmpty(logQuery.getQueryParam()), WmsLog::getLogRequest, logQuery.getQueryParam())
                    .or().like(StringUtils.isNotEmpty(logQuery.getQueryParam()), WmsLog::getLogResponse, logQuery.getQueryParam())
                    .or().like(StringUtils.isNotEmpty(logQuery.getQueryParam()), WmsLog::getLogIp, logQuery.getQueryParam())
                    .or().like(StringUtils.isNotEmpty(logQuery.getQueryParam()), WmsLog::getLogUser, logQuery.getQueryParam())
                    .orderByDesc(WmsLog::getLogTime));

            PageDto<WmsLogVo> pageDto = PageDto.of(logPage, log -> BeanUtil.copyProperties(log, WmsLogVo.class));
            response.setCode(ResponseCode.OK.getCode());
            response.setMessage("查询成功");
            response.setReturnData(pageDto);
            return convertJsonString(response);
        } catch (Exception e) {
            logger.error("查询日志信息发生异常：{}", convertJsonString(e));
            response.setCode(ResponseCode.ERROR.getCode());
            response.setMessage("查询日志信息发生异常");
            return convertJsonString(response);
        }
    }
}
