package com.crm.ib.task.aop;

import com.crm.ib.task.configuration.IbReportAnnotation;
import com.crm.rely.backend.model.entity.report.VolumeReportEntity;
import com.crm.rely.backend.util.DateUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by max on 2020/3/31.
 */
@Order(1)
@Aspect
@Component
public class IbReportAop {


    @Before(value = "@annotation(ibReportAnnotation)")
    public void before(JoinPoint joinPoint, IbReportAnnotation ibReportAnnotation) throws Exception {

        for (Object arg : joinPoint.getArgs()) {
            if (arg instanceof VolumeReportEntity) {
                VolumeReportEntity entity = (VolumeReportEntity) arg;
                if (entity.getStartDate() == null) {
                    entity.setStartDate(DateUtil.getMonthStart(entity.getStartDate()));
                }
                if (entity.getEndDate() == null) {
                    entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
                } else {
                    entity.setEndDate(DateUtil.addDate(entity.getEndDate()));
                }

                break;

            }
        }
    }
}