package thyyxxk.webserver.aop.aspect;

import cn.hutool.core.convert.Convert;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.clinicalPathway.ClinicalPathwayDao;
import thyyxxk.webserver.entity.dictionary.pathwayTemplates.dto.ClinicalPathwayTemplates;
import thyyxxk.webserver.utils.TokenUtil;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@RequiredArgsConstructor
public class CheckClinicalPathwayAop {
    private final ClinicalPathwayDao dao;

    @Pointcut("@annotation(thyyxxk.webserver.aop.interfaces.CheckClinicalPathway)")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void doBefore() {
        HttpServletRequest request = TokenUtil.getInstance().getRequest();
        if (request == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "请求错误，请重新登录");
        }
        String templateId = request.getHeader("templateId");
        if (templateId == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "模板id不存在请刷新页面");
        }
        ClinicalPathwayTemplates clinicalPathwayTemplates = dao.selectById(templateId);
        if (clinicalPathwayTemplates == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "模板已经被停用了。");
        }

        if (!TokenUtil.getInstance().getTokenUserId().equals(clinicalPathwayTemplates.getCreateCode())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "改模板不是您创建的无法修改。");
        }

//        if (clinicalPathwayTemplates.getPublish()) {
//            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "模板已经发布无法修改");
//        }
    }

}
