package com.cool.store.dao.closeup;

import com.cool.store.entity.closeup.CloseUpApplyFormDO;
import com.cool.store.mapper.closeup.CloseUpApplyFormMapper;
import com.cool.store.request.closeup.CloseUpQueryRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.cool.store.enums.closeup.CloseUpStatusEnum.APPROVAL;
import static com.cool.store.enums.closeup.CloseUpStatusEnum.REJECTED;

/**
 * <p>
 * 歇业开业申请单DAO
 * </p>
 *
 * @author wangff
 * @since 2026/4/8
 */
@Repository
@RequiredArgsConstructor
public class CloseUpApplyFormDAO {
    private final CloseUpApplyFormMapper closeUpApplyFormMapper;

    public void insertOrUpdate(CloseUpApplyFormDO record) {
        closeUpApplyFormMapper.insertOrUpdate(record);
    }

    /**
     * 是否存在进行中的申请
     */
    public boolean existOngoingRecord(String storeId, Integer type) {
        Example example = new Example(CloseUpApplyFormDO.class);
        example.createCriteria()
                .andEqualTo("storeId", storeId)
                .andEqualTo("type", type)
                .andEqualTo("status", APPROVAL.getStatus());
        return closeUpApplyFormMapper.selectCountByExample(example) > 0;
    }

    public CloseUpApplyFormDO getById(Long id) {
        return closeUpApplyFormMapper.selectByPrimaryKey(id);
    }

    public List<CloseUpApplyFormDO> getByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        Example example = new Example(CloseUpApplyFormDO.class);
        example.createCriteria().andIn("id", ids);
        return closeUpApplyFormMapper.selectByExample(example);
    }

    public void updateByPrimaryKeySelective(CloseUpApplyFormDO record) {
        closeUpApplyFormMapper.updateByPrimaryKeySelective(record);
    }

    public List<CloseUpApplyFormDO> getList(Long lineId, Integer type, String storeId) {
        Example example = new Example(CloseUpApplyFormDO.class);
        example.createCriteria()
                .andEqualTo("lineId", lineId)
                .andEqualTo("type", type)
                .andEqualTo("storeId", storeId);
        example.setOrderByClause("create_time DESC");
        return closeUpApplyFormMapper.selectByExample(example);
    }

    public List<CloseUpApplyFormDO> getList(CloseUpQueryRequest request) {
        return closeUpApplyFormMapper.getList(request);
    }

    /**
     * 歇业申请单存在开业申请
     */
    public boolean existOpenApply(Long sourceApplyId) {
        Example example = new Example(CloseUpApplyFormDO.class);
        example.createCriteria()
                .andEqualTo("sourceApplyId", sourceApplyId)
                .andNotEqualTo("status", REJECTED.getStatus());
        return closeUpApplyFormMapper.selectCountByExample(example) > 0;
    }

    public CloseUpApplyFormDO getBySourceApplyId(Long sourceApplyId) {
        return closeUpApplyFormMapper.selectOne(CloseUpApplyFormDO.builder().sourceApplyId(sourceApplyId).build());
    }

    /**
     * 判断是否存在时间交集的歇业申请单（非审批拒绝状态）
     */
    public boolean existsTimeOverlap(String storeId, Date planCloseDate, Date planOpenDate) {
        return closeUpApplyFormMapper.existsTimeOverlap(storeId, planCloseDate, planOpenDate) > 0;
    }
}
