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

import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
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.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/mobile/sysLogApi")
@Api(description = "日志记录接口")
public class SysLogApiController {


    @Autowired
    private SysLogService sysLogService;
    /**
     * 记录 微信小程序错误日志 ， 有些错误抛出了，也处理不了 ，记录到数据库中，看情况
     * @param message
     * @param token
     * @param subject
     * @return
     */
    @PostMapping("submit")
    @ApiOperation(value = "记录日志")
    public MessageResult<Boolean> submit(String  message, String token, @RequestAttribute String subject) {

        MessageResult<Boolean> messageResult = new MessageResult<>();

        try {

            SysLog sysLog = new SysLog();
          //  sysLog.setPointcut(deviceNo);
            sysLog.setData(message);
            sysLog.setUserId(subject);
            sysLog.setRemark("/mobile/sysLogApi->接收error");
            sysLog.setCreateTime(new Date());
            sysLogService.insert(sysLog);

            messageResult.setData(true);
            messageResult.setCode(200);
            messageResult.setMessage("成功");
            messageResult.setResult(true);
        } catch (Exception ex) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            log.error(ex.getMessage(), ex);
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);
        }

        return messageResult;
    }


}
