package com.cool.store.dao;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.cool.store.entity.LineCalendarsEventDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.LineCalendarsEventMapper;
import com.cool.store.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: LineCalendarsEventDAO
 * @Description:
 * @date 2024-03-15 16:00
 */
@Slf4j
@Repository
public class LineCalendarsEventDAO {

    @Resource
    private LineCalendarsEventMapper lineCalendarsEventMapper;

    public List<LineCalendarsEventDO> getInterviewerLineCalendarsEvent(String interviewerUserId, LocalDate localDate) {
        if (StringUtils.isBlank(interviewerUserId)  || Objects.isNull(localDate)) {
            return null;
        }
        return lineCalendarsEventMapper.getInterviewerLineCalendarsEvent(interviewerUserId, localDate.toString());
    }

    public List<LineCalendarsEventDO> getRegionLineCalendarsEvent(Long regionId, LocalDate localDate) {
        if (Objects.isNull(regionId) || Objects.isNull(localDate)) {
            return null;
        }
        return lineCalendarsEventMapper.getRegionLineCalendarsEvent(regionId, localDate.toString());
    }

    /**
     * 判断时间是否被占用
     * @param interviewType
     * @param regionId
     * @param interviewer
     * @param startTime
     * @param endTime
     * @return
     */
    public Boolean isOccupied(Integer interviewType, Long regionId, String interviewerUserId, String startTime, String endTime, Long excludeInterviewId){
        return lineCalendarsEventMapper.getOccupiedCount(interviewType, regionId, interviewerUserId, startTime, endTime, excludeInterviewId) > 0;
    }

    /**
     * 新增日历事件
     * @param param
     * @return
     */
    public Long addCalendarsEvent(LineCalendarsEventDO param){
        if(Objects.isNull(param.getRegionId()) || Objects.isNull(param.getLineId()) || Objects.isNull(param.getPartnerId()) || Objects.isNull(param.getStartTime()) || Objects.isNull(param.getEndTime())){
            log.error("新增日历事件失败：{}",JSONObject.toJSONString(param));
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        lineCalendarsEventMapper.insertSelective(param);
        return param.getId();
    }

    public Integer updateCalendarsEvent(LineCalendarsEventDO param){
        if(Objects.isNull(param.getId())){
            return null;
        }
        return lineCalendarsEventMapper.updateByPrimaryKeySelective(param);
    }

}
