package com.cool.store.dao.fees;

import com.cool.store.entity.fees.ShopAllocationInfoDO;
import com.cool.store.enums.fees.AllocationPayStatusEnum;
import com.cool.store.mapper.fees.ShopAllocationInfoMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 门店平安钱包分账信息DAO
 * </p>
 *
 * @author wangff
 * @since 2026/1/29
 */
@Repository
@RequiredArgsConstructor
public class ShopAllocationInfoDAO {
    private final ShopAllocationInfoMapper shopAllocationInfoMapper;

    public List<ShopAllocationInfoDO> getByShopId(Long shopId) {
        ShopAllocationInfoDO info = new ShopAllocationInfoDO();
        info.setShopId(shopId);
        return shopAllocationInfoMapper.select(info);
    }

    /**
     * 批量新增
     */
    public void insertBatch(List<ShopAllocationInfoDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        shopAllocationInfoMapper.insertBatch(list);
    }

    /**
     * 批量更新
     */
    public void updateBatch(List<ShopAllocationInfoDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        shopAllocationInfoMapper.updateBatch(list);
    }

    public void insertOrUpdateBatch(List<ShopAllocationInfoDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (ShopAllocationInfoDO shopAllocationInfoDO : list) {
            if (Objects.isNull(shopAllocationInfoDO.getStatus())) {
                shopAllocationInfoDO.setStatus(AllocationPayStatusEnum.UNPAID.getStatus());
            }
        }
        shopAllocationInfoMapper.insertOrUpdateBatch(list);
    }

    /**
     * 更新分账状态
     * @param shopId 门店id
     * @param expenseTypes 费用类型列表
     * @param status 状态
     */
    public void updateStatusByShopId(Long shopId, List<String> expenseTypes, Integer status) {
        if (CollectionUtils.isEmpty(expenseTypes)) {
            return;
        }
        Example example = new Example(ShopAllocationInfoDO.class);
        example.createCriteria()
                .andEqualTo("shopId", shopId)
                .andIn("expenseType", expenseTypes);
        ShopAllocationInfoDO allocationInfoDO = new ShopAllocationInfoDO();
        allocationInfoDO.setStatus(status);
        shopAllocationInfoMapper.updateByExampleSelective(allocationInfoDO, example);
    }
}
