package com.ho.business.service.impl;

import com.ho.business.entity.PlanningCurve;
import com.ho.business.entity.PlanningCurveTactics;
import com.ho.business.mapper.PlanningCurveTacticsMapper;
import com.ho.business.service.PlanningCurveTacticsService;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author catkins
 * @date 2023/10/12 0012 17:34
 */
@Service
public class PlanningCurveTacticsServiceImpl implements PlanningCurveTacticsService {

    @Autowired
    private PlanningCurveTacticsMapper planningCurveTacticsMapper;

    @Override
    public List<PlanningCurveTactics> getTacticsList(PlanningCurveTactics tactics) {
        return planningCurveTacticsMapper.getTacticsList(tactics);
    }

    @Override
    public PlanningCurveTactics getTacticsDetails(PlanningCurveTactics tactics) {
        PlanningCurveTactics tacticsDetail = planningCurveTacticsMapper.getTacticsDetails(tactics);
        if ( tacticsDetail != null && tacticsDetail.getTemId() != null ){
            List<PlanningCurve> planningCurves = planningCurveTacticsMapper.getPlanningCurves(tacticsDetail.getTemId());
            tacticsDetail.setPlanningCurves(planningCurves);
        }
        return tacticsDetail;
    }

    @Override
    public int add(PlanningCurveTactics tactics) {
        boolean flag = false;
        List<PlanningCurveTactics> tacticsList = planningCurveTacticsMapper.getTacticsList(tactics);
        if ( tacticsList != null && tacticsList.size() > 0 ) {
            flag = hasOverlap(tacticsList, tactics);
        }
        if( flag ){
            throw new BusinessException(BaseResponseCode.HAS_OVER_LAP);
        }
        return planningCurveTacticsMapper.add(tactics);
    }

    @Override
    public int modify(PlanningCurveTactics tactics) {
        boolean flag = false;
        List<PlanningCurveTactics> tacticsList = planningCurveTacticsMapper.getTacticsList(tactics);
        if ( tacticsList != null && tacticsList.size() > 0 ) {
            flag = hasOverlap(tacticsList, tactics);
        }
        if( flag ){
            throw new BusinessException(BaseResponseCode.HAS_OVER_LAP);
        }
        return planningCurveTacticsMapper.modify(tactics);
    }


    private boolean hasOverlap(List<PlanningCurveTactics> tacticsList,PlanningCurveTactics tactics){
        for (int i = 0; i < tacticsList.size(); i++) {
            if (isOverlap(tacticsList.get(i), tactics)) {
                return true;
            }
        }
        return false;
    }

    private boolean isOverlap(PlanningCurveTactics interval1, PlanningCurveTactics interval2) {
        return interval1.getStartDate().before(interval2.getEndDate()) && interval2.getStartDate().before(interval1.getEndDate());
    }

    @Override
    public int delete(PlanningCurveTactics tactics) {
        return planningCurveTacticsMapper.delete(tactics);
    }

    @Override
    public List<PlanningCurveTactics> getPlanningCurveTemplates(PlanningCurveTactics tactics) {
        return planningCurveTacticsMapper.getPlanningCurveTemplates(tactics);
    }
}
