package com.org.hktshop.utils.log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.log.HktSystemLog;
import com.org.hktshop.utils.DateUtil;
import com.org.hktshop.utils.ExecSqlLib;
import com.org.hktshop.utils.IpUtils;
import com.org.hktshop.utils.exception.CaptureException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;


/**
 * @author owen
 * 系统操作日志
 */
@Aspect
@Component
public class RecordSystemOperationLogAspect extends BaseService<HktSystemLog> {

    private static final Logger logger = LoggerFactory.getLogger(RecordSystemOperationLogAspect.class);

    @Around(value = "@annotation(recordSystemOperation)")
    public Object log(ProceedingJoinPoint joinPoint, SystemOperation recordSystemOperation) throws Throwable {

        HktSystemLog hktSystemLog = new HktSystemLog();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String requestURI = request.getRequestURI();

        hktSystemLog.setUrl(requestURI);

        String method = request.getMethod();

        hktSystemLog.setMethod(method);

        HktAdmin opUser = (HktAdmin) request.getSession().getAttribute("OpUser");
        if(opUser == null){
            hktSystemLog.setUserId("1");
        }else {
            hktSystemLog.setUserId(opUser.getId());
        }

        String ip = IpUtils.getIPAddress(request);

        hktSystemLog.setIp(ip);

        Object object = joinPoint.proceed();

        if(object == null){
            throw new CaptureException("返回参数有误");
        }

        String jsonString = JSON.toJSONString(object);

        JSONObject jsonObject = JSON.parseObject(jsonString);

        String httpCode = jsonObject.getString("httpCode");

        hktSystemLog.setReturnCode(httpCode);

        String message = jsonObject.getString("message");

        hktSystemLog.setReturnMessage(message);

        Object[] args = joinPoint.getArgs();

        if(args.length == 0){
            throw new CaptureException("请求参数不能为空");
        }

        String s = JSON.toJSONString(args[0]);

        hktSystemLog.setParamData(s);

        SystemModule systemModule = joinPoint.getTarget().getClass().getAnnotation(SystemModule.class);

        if (systemModule != null) {

            hktSystemLog.setMoudle(systemModule.module());
        }

        hktSystemLog.setOperation(recordSystemOperation.operation());

        if("修改商品价格".equals(hktSystemLog.getOperation())){
            JSONObject jsonObject1 = JSON.parseObject(hktSystemLog.getParamData());
            hktSystemLog.setParamData(jsonObject1.getString("price"));
            hktSystemLog.setGoodsId(jsonObject1.getString("id"));
        }

        hktSystemLog.setCreateTime(DateUtil.getDateTime());

        String sql = ExecSqlLib.GetInsertSql(hktSystemLog);

        boolean execute = this.baseJdbcDao.execute(sql);

        if (!execute) {
            logger.info("新增日志信息失败！ data={}", sql);
        }

        return object;
    }
}