package com.yc.ship.module.otc.service.storescheduling;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.date.DateUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.otc.controller.admin.storescheduling.vo.StoreSchedulingPageReqVO;
import com.yc.ship.module.otc.controller.admin.storescheduling.vo.StoreSchedulingSaveReqVO;
import com.yc.ship.module.otc.dal.dataobject.storescheduling.StoreSchedulingDO;
import com.yc.ship.module.otc.dal.mysql.storescheduling.StoreSchedulingMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.otc.enums.ErrorCodeConstants.STORE_SCHEDULING_NOT_EXISTS;
import static com.yc.ship.module.otc.enums.ErrorCodeConstants.STORE_SCHEDULING_TIME_EXISTS;

/**
 * 班次管理 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StoreSchedulingServiceImpl implements StoreSchedulingService {

    @Resource
    private StoreSchedulingMapper storeSchedulingMapper;

    @Override
    public Long createStoreScheduling(StoreSchedulingSaveReqVO createReqVO) {
        // 插入
        StoreSchedulingDO storeScheduling = BeanUtils.toBean(createReqVO, StoreSchedulingDO.class);
        storeScheduling.setId(IdWorker.getId(storeScheduling));
        verify(storeScheduling);
        storeSchedulingMapper.insert(storeScheduling);
        // 返回
        return storeScheduling.getId();
    }

    @Override
    public void updateStoreScheduling(StoreSchedulingSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreSchedulingExists(updateReqVO.getId());
        // 更新
        StoreSchedulingDO updateObj = BeanUtils.toBean(updateReqVO, StoreSchedulingDO.class);
        verify(updateObj);
        storeSchedulingMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreScheduling(Long id) {
        // 校验存在
        validateStoreSchedulingExists(id);
        // 删除
        storeSchedulingMapper.deleteById(id);
    }

    private void verify(StoreSchedulingDO scheduling) {
        List<StoreSchedulingDO> list = storeSchedulingMapper.selectList();
        for (StoreSchedulingDO schedulingDO : list) {
            if (Objects.equals(schedulingDO.getId(), scheduling.getId())) {
                continue;
            }
            // saveReqVO的开始时间和结束时间均在schedulingDO结束时间之后
            boolean isVerify1 = DateUtils.isCompare(scheduling.getEndTime(), schedulingDO.getEndTime()) > 0
                    && DateUtils.isCompare(scheduling.getStartTime(), schedulingDO.getEndTime()) > 0;
            // saveReqVO的开始时间和结束时间均在schedulingDO开始时间之前
            boolean isVerify2 = DateUtils.isCompare(scheduling.getEndTime(), schedulingDO.getStartTime()) < 0
                    && DateUtils.isCompare(scheduling.getStartTime(), schedulingDO.getStartTime()) < 0;
            if (!isVerify1 && !isVerify2) {
                throw exception(STORE_SCHEDULING_TIME_EXISTS);
            }
        }
    }

    private void validateStoreSchedulingExists(Long id) {
        if (storeSchedulingMapper.selectById(id) == null) {
            throw exception(STORE_SCHEDULING_NOT_EXISTS);
        }
    }

    @Override
    public StoreSchedulingDO getStoreScheduling(Long id) {
        return storeSchedulingMapper.selectById(id);
    }

    @Override
    public PageResult<StoreSchedulingDO> getStoreSchedulingPage(StoreSchedulingPageReqVO pageReqVO) {
        return storeSchedulingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StoreSchedulingDO> getSchedulingListByStatus() {
        return storeSchedulingMapper.selectList();
    }

    @Override
    public List<StoreSchedulingDO> getSchedulingList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return storeSchedulingMapper.selectBatchIds(ids);
    }

    @Override
    public Map<Long, StoreSchedulingDO> getSchedulingMap(Collection<Long> ids) {
        List<StoreSchedulingDO> users = getSchedulingList(ids);
        return CollectionUtils.convertMap(users, StoreSchedulingDO::getId);
    }

}
