package com.org.hktshop.controller.system;


import com.org.hktshop.Interface.system.HktSystemLogService;
import com.org.hktshop.Interface.system.HktSystemService;
import com.org.hktshop.Interface.system.HktUserLoginLogService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.log.HktSystemLog;
import com.org.hktshop.entity.system.HktSystem;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.entity.user.HktUserLogin;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.IpUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 系统
 * @author hotking03
 */
@RestController
public class HktSystemLogController extends BaseController {

    @Autowired
    private HktSystemLogService hktSystemLogService;

    @Autowired
    private HktUserLoginLogService hktUserLoginLogService;

    @PostMapping("/async/log/selectByPage")
    @ApiOperation(value = "日志查询 系统")
    public Object selectByPage(@RequestBody HktSystemLog hktSystemLog) {
        try {
            return BaseResult.successWithData(hktSystemLogService.selectByPage(hktSystemLog));
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/log/queryByPage")
    @ApiOperation(value = "用户登录系统")
    public Object queryByPage(@RequestBody HktUserLogin hktUserLogin) {
        try {
            return BaseResult.successWithData(hktUserLoginLogService.queryByPage(hktUserLogin));
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    @RequestMapping("/log/addUserLog")
    @ApiOperation(value = "记录用户操作页面")
    public Object addUserLog(@RequestBody HktUserLogin hktUserLogin, HttpServletRequest request) {
        try {
            HktUser hktUser = (HktUser) getSessionUserId(request);
            if(hktUser != null){
                hktUserLogin.setUserId(hktUser.getId());
            }else {
                hktUserLogin.setUserId("10010");
            }
            hktUserLogin.setType("3");
            hktUserLogin.setCreateTime(DateUtil.getDateTime());
            hktUserLogin.setLoginIp(IpUtils.getIPAddress(request));
            hktUserLoginLogService.saveHktUserLoginLog(hktUserLogin);
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


}

