package com.cool.store.dao.closeup;

import com.alibaba.excel.util.CollectionUtils;
import com.cool.store.entity.closeup.CloseUpPlatformDO;
import com.cool.store.enums.closeup.CloseUpPlatformEnum;
import com.cool.store.mapper.closeup.CloseUpPlatformMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 歇业开业平台账号DAO
 * </p>
 *
 * @author wangff
 * @since 2026/4/8
 */
@Repository
@RequiredArgsConstructor
public class CloseUpPlatformDAO {
    private final CloseUpPlatformMapper closeUpPlatformMapper;

    public List<CloseUpPlatformDO> init(Long applyId, Integer applyType, Date taskStartDate) {
        List<CloseUpPlatformDO> list = Arrays.stream(CloseUpPlatformEnum.values())
                .map(v -> CloseUpPlatformDO.builder()
                        .applyId(applyId)
                        .applyType(applyType)
                        .code(v.getCode())
                        .name(v.getName())
                        .status(0)
                        .type(v.getType())
                        .icon(v.getIcon())
                        .taskStartDate(taskStartDate)
                        .build())
                .collect(Collectors.toList());
        closeUpPlatformMapper.insertBatch(list);
        return list;
    }

    public List<CloseUpPlatformDO> getList(Long applyId, Integer status, Integer type) {
        Example example = new Example(CloseUpPlatformDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("applyId", applyId);
        if (Objects.nonNull(status)) {
            criteria.andEqualTo("status", status);
        }
        if (Objects.nonNull(type)) {
            criteria.andEqualTo("type", type);
        }
        return closeUpPlatformMapper.selectByExample(example);
    }

    /**
     * 更新状态
     */
    public void updateStatusByIds(List<Long> ids, Integer status) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        Example example = new Example(CloseUpPlatformDO.class);
        example.createCriteria()
                .andIn("id", ids);
        CloseUpPlatformDO update = CloseUpPlatformDO.builder().status(status).build();
        closeUpPlatformMapper.updateByExampleSelective(update, example);
    }

    /**
     * 根据申请单id删除
     */
    public void deleteByApplyId(Long applyId) {
        closeUpPlatformMapper.delete(CloseUpPlatformDO.builder().applyId(applyId).build());
    }

    /**
     * 查询待处理的申请单id
     */
    public List<Long> selectTodoApplyIds(Integer applyType, String storeKeyword) {
        return closeUpPlatformMapper.selectTodoApplyIds(applyType, storeKeyword);
    }
}
