package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.ShopDealerBank;
import com.gxwebsoft.shop.mapper.ShopDealerBankMapper;
import com.gxwebsoft.shop.param.ShopDealerBankParam;
import com.gxwebsoft.shop.service.ShopDealerBankService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分销商提现银行卡Service实现
 *
 * @author 科技小王子
 * @since 2025-08-11 23:51:41
 */
@Service
public class ShopDealerBankServiceImpl extends ServiceImpl<ShopDealerBankMapper, ShopDealerBank> implements ShopDealerBankService {

    @Override
    public PageResult<ShopDealerBank> pageRel(ShopDealerBankParam param) {
        PageParam<ShopDealerBank, ShopDealerBankParam> page = new PageParam<>(param);
        page.setDefaultOrder("is_default desc, create_time desc");
        List<ShopDealerBank> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<ShopDealerBank> listRel(ShopDealerBankParam param) {
        List<ShopDealerBank> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<ShopDealerBank, ShopDealerBankParam> page = new PageParam<>();
        page.setDefaultOrder("is_default desc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public ShopDealerBank getByIdRel(Integer id) {
        ShopDealerBankParam param = new ShopDealerBankParam();
        param.setId(id);
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    public ShopDealerBank getDefaultBank(Integer userId) {
      LambdaQueryWrapper<ShopDealerBank> wrapper = new LambdaQueryWrapper<>();
      wrapper.eq(ShopDealerBank::getUserId, userId)
        .eq(ShopDealerBank::getIsDefault, true)
        .orderByDesc(ShopDealerBank::getCreateTime)
        .last("LIMIT 1");
      return getOne(wrapper);
    }

}
