package com.cool.store.dao;

import com.cool.store.entity.CloseStoreInfoDO;
import com.cool.store.enums.close.CloseStoreStatusEnum;
import com.cool.store.mapper.CloseStoreInfoMapper;
import com.cool.store.request.close.store.CloseStoreQueryRequest;
import com.cool.store.vo.close.store.CloseStoreInfoListVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @Author suzhuhong
 * @Date 2025/9/22 16:52
 * @Version 1.0
 */
@Repository
public class CloseStoreInfoDAO {
    @Resource
    private CloseStoreInfoMapper closeStoreInfoMapper;

    public int insertSelective(CloseStoreInfoDO closeStoreInfoDO) {
        return closeStoreInfoMapper.insertSelective(closeStoreInfoDO);
    }
    public int updateSelective(CloseStoreInfoDO closeStoreInfoDO) {
        return closeStoreInfoMapper.updateByPrimaryKeySelective(closeStoreInfoDO);
    }

    /**
     * 根据id查询
     * @param id 主键id
     * @return 闭店信息
     */
    public CloseStoreInfoDO getById(Long id) {
        return closeStoreInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 是否存在进行中的闭店申请
     * @param storeId 门店id
     * @return 是否存在
     */
    public boolean existOngoingRecord(String storeId) {
        Example example = new Example(CloseStoreInfoDO.class);
        example.createCriteria().andEqualTo("storeId", storeId)
                .andIn("closeStatus", Arrays.asList(CloseStoreStatusEnum.UNDER_APPROVAL.getCloseStoreStatus(),
                        CloseStoreStatusEnum.MANAGER_UNDER_APPROVAL.getCloseStoreStatus(),
                        CloseStoreStatusEnum.CLOSING.getCloseStoreStatus()));
        return closeStoreInfoMapper.selectCountByExample(example) > 0;
    }

    /**
     * 查询闭店申请记录
     * @param request 闭店申请查询Request
     * @return 闭店申请记录列表VO
     */
    public PageInfo<CloseStoreInfoListVO> getCloseStoreInfoList(CloseStoreQueryRequest request, List<String> regionIds) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CloseStoreInfoListVO> list = closeStoreInfoMapper.getCloseStoreInfoList(request, regionIds);
        return new PageInfo<>(list);
    }

    /**
     * 根据申请人id查询闭店申请记录
     * @param applyUserId 申请人id
     * @return 闭店申请记录列表
     */
    public List<CloseStoreInfoDO> getListByApplyUserId(String applyUserId, Integer closeType, String storeId) {
        Example example = new Example(CloseStoreInfoDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("applyUserId", applyUserId);
        if (StringUtils.isNotBlank(storeId)) {
            criteria.andEqualTo("storeId", storeId);
        }
        if (Objects.nonNull(closeType)) {
            criteria.andEqualTo("closeType", closeType);
        }
        example.setOrderByClause("create_time DESC");
        return closeStoreInfoMapper.selectByExample(example);
    }
}
