package com.zjrs.ggfw.annotation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.util.SecurityUtils;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.ggfw.rlzysc.dto.JlxxWhDTO;
import com.zjrs.ggfw.rlzysc.entity.QyxxMapper;
import com.zjrs.ggfw.rlzysc.entity.VisitLogMapper;
import com.zjrs.ggfw.rlzysc.po.RsRlzyFwrzPO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyQyxxPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Aspect
public class VisitLogimpl {

    @Autowired
    private VisitLogMapper visitLogMapper;

    @Autowired
    private QyxxMapper qyxxMapper;

    @Pointcut("@annotation(com.zjrs.ggfw.annotation.VisitLog)")
    public void annotationPointcut() {

    }

    @Around("annotationPointcut()  && @annotation(visitLog)")
    public AjaxResponse beforePointcut(ProceedingJoinPoint joinPoint, VisitLog visitLog) throws Throwable {
        AjaxResponse ajaxResponse = new AjaxResponse();

        Object[] args = joinPoint.getArgs();

        String userid = getCurrentUserId();
        if(!StringUtils.isNotBlank(userid)) {
            return  failResponse(ajaxResponse, "1", "获取不到该用户信息");
        }

        String type = visitLog.type();

        RsRlzyFwrzPO fwrz = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tsot = dateFormat.format(new Date());

        if("01".equals(type)) {

            JlxxWhDTO dto = (JlxxWhDTO) args[0];
            if(!StringUtils.isNotBlank(dto.getRsrlzyjlxxlsh())) {
                return  failResponse(ajaxResponse, "1", "获取不到简历流水号");
            }

            QueryWrapper<RsRlzyQyxxPO> qyxxPOQueryWrapper = new QueryWrapper<>();
            qyxxPOQueryWrapper.eq("rs_yhlsh", userid);
            RsRlzyQyxxPO qyxx = qyxxMapper.selectOne(qyxxPOQueryWrapper);
            if(qyxx == null) {
                return  failResponse(ajaxResponse, "1", "获取不到企业信息");
            }

            fwrz = new RsRlzyFwrzPO();
            fwrz.setRsrlzysourcelsh(qyxx.getRsrlzyqyxxlsh());
            fwrz.setRsrlzytargetlsh(dto.getRsrlzyjlxxlsh());
            fwrz.setType(type);
            fwrz.setTsop(userid);
            fwrz.setTsot(tsot);
        }
        visitLogMapper.insert(fwrz);

        return (AjaxResponse) joinPoint.proceed(args);
    }

    private String getCurrentUserId() {
        CurrentUser currentUser = SecurityUtils.getCurrentUser();
        return currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
    }

    private AjaxResponse successResponse(AjaxResponse ajaxResponse, String code, String message) {
        return ajaxResponse(ajaxResponse, code, message);
    }

    private AjaxResponse failResponse(AjaxResponse ajaxResponse, String code, String message) {
        return ajaxResponse(ajaxResponse, code, message);
    }

    private AjaxResponse ajaxResponse(AjaxResponse ajaxResponse, String code, String message) {
        ajaxResponse.setAppcode(code);
        ajaxResponse.setErrorMsg(message);
        return ajaxResponse;
    }


}
