package com.uas.platform.b2b.manage.controller;

import com.uas.platform.b2b.manage.model.ErrorLog;
import com.uas.platform.b2b.manage.model.UseType;
import com.uas.platform.b2b.manage.service.ErrorLogService;
import com.uas.platform.b2b.manage.service.UseLogService;
import com.uas.platform.b2b.manage.web.BaseController;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;
import com.uas.platform.core.util.AgentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志查询接口
 *
 * Created by hejq on 2018-06-21.
 */
@RequestMapping("/log")
@RestController
public class LogsController extends BaseController {

    @Autowired
    private ErrorLogService logService;

    /**
     * 分页查询日志信息
     *
     * @param params 分页参数
     * @param keyword 关键字
     * @param request 请求
     * @param fromDate 开始时间
     * @param endDate 截止时间
     * @return
     */
    @RequestMapping(value = "/errPage", method = RequestMethod.GET)
    public Page<ErrorLog> findByPageInfo(PageParams params, String keyword, HttpServletRequest request, Long fromDate, Long endDate) {
        PageInfo info = new PageInfo(params);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        info.setSort(sort);
        if (!StringUtils.isEmpty(keyword)) {
            keyword = keyword.trim();
        }
        useLogService.appendLog(UseType.LOGS_SEARCH.code(), keyword, AgentUtils.getIp(request));
        return logService.findErrLogsByPageInfo(info, keyword, fromDate, endDate);
    }

    /**
     * 通过id查询参数详情
     *
     * @param id 主表id
     * @return
     */
    @RequestMapping(value = "/para", method = RequestMethod.GET)
    public ModelMap findParameters(Long id) {
        String result = logService.findParameters(id);
        return new ModelMap("content", result);
    }

    /**
     * 通过sign查询不存在的单据信息
     *
     * @param sign 标识
     * @return
     */
    @RequestMapping(value = "/notExistData", method = RequestMethod.GET)
    public ModelMap findNotExistDataBySign(String sign, String msg) {
        return logService.findNotExistDataBySign(sign, msg);
    }
}
