package com.cool.store.dao;

import com.cool.store.entity.CloseStoreRefundInfoDO;
import com.cool.store.enums.close.RefundPayStatusEnum;
import com.cool.store.mapper.CloseStoreRefundInfoMapper;
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;

/**
 * <p>
 * 闭店退款信息DAO
 * </p>
 *
 * @author wangff
 * @since 2025/10/13
 */
@Repository
@RequiredArgsConstructor
public class CloseStoreRefundInfoDAO {
    private final CloseStoreRefundInfoMapper closeStoreRefundInfoMapper;

    /**
     * 根据id查询
     */
    public CloseStoreRefundInfoDO getById(Long id) {
        return closeStoreRefundInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新退款状态
     * @param id id
     * @param refundStatus 退款状态
     */
    public void updateRefundStatus(Long id, String refundStatus, Date refundTime) {
        closeStoreRefundInfoMapper.updateByPrimaryKeySelective(CloseStoreRefundInfoDO.builder().id(id).refundStatus(refundStatus).refundTime(refundTime).build());
    }

    /**
     * 根据id批量更新退款状态
     * @param list 退款信息列表
     */
    public void updateRefundStatusBatchById(List<CloseStoreRefundInfoDO> list) {
        closeStoreRefundInfoMapper.updateRefundStatusBatchById(list);
    }

    /**
     * 编辑
     */
    public boolean updateSelective(CloseStoreRefundInfoDO closeStoreRefundInfoDO) {
        return closeStoreRefundInfoMapper.updateByPrimaryKeySelective(closeStoreRefundInfoDO) > 0;
    }
    
    /**
     * 新增
     */
    public boolean insertSelective(CloseStoreRefundInfoDO closeStoreRefundInfoDO) {
        return closeStoreRefundInfoMapper.insertSelective(closeStoreRefundInfoDO) > 0;
    }

    /**
     * 根据闭店申请id查询
     * @param closeStoreId 闭店申请id
     * @param type 退款类型(闭店子阶段code)
     * @return 退款信息
     */
    public CloseStoreRefundInfoDO getByCloseStoreId(Long closeStoreId, Integer type) {
        return closeStoreRefundInfoMapper.selectOne(CloseStoreRefundInfoDO.builder().closeStoreId(closeStoreId).type(type).build());
    }

    /**
     * 根据闭店申请id查询列表
     * @param closeStoreId 闭店申请id
     * @return 退款信息列表
     */
    public List<CloseStoreRefundInfoDO> getListByCloseStoreId(Long closeStoreId, Integer type) {
        Example example = new Example(CloseStoreRefundInfoDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("closeStoreId", closeStoreId);
        if (Objects.nonNull(type)) {
            criteria.andEqualTo("type", type);
        }
        example.setOrderByClause("create_time DESC");
        return closeStoreRefundInfoMapper.selectByExample(example);
    }

    /**
     * 根据id删除
     */
    public void removeById(Long id) {
        closeStoreRefundInfoMapper.deleteByPrimaryKey(id);
    }

    /**
     * 是否存在非作废状态的记录
     * @param closeStoreId 闭店申请id
     * @param type 退款类型
     * @return 是否存在
     */
    public boolean existEffectiveRecord(Long closeStoreId, Integer type) {
        Example example = new Example(CloseStoreRefundInfoDO.class);
        example.createCriteria()
                .andEqualTo("closeStoreId", closeStoreId)
                .andNotEqualTo("refundStatus", RefundPayStatusEnum.ABANDON.getStatus())
                .andEqualTo("type", type);
        return closeStoreRefundInfoMapper.selectCountByExample(example) > 0;
    }

    /**
     * 获取未完成的退款记录
     * @return 退款信息
     */
    public List<CloseStoreRefundInfoDO> getUnfinishedRecords() {
        Example example = new Example(CloseStoreRefundInfoDO.class);
        example.createCriteria()
                .andNotIn("refundStatus", Arrays.asList(RefundPayStatusEnum.PAY_SUCCESS.getStatus(), RefundPayStatusEnum.ABANDON.getStatus()));
        return closeStoreRefundInfoMapper.selectByExample(example);
    }
}
