package com.gxwebsoft.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.shop.mapper.MerchantOfflinePayMapper;
import com.gxwebsoft.shop.service.MerchantOfflinePayService;
import com.gxwebsoft.shop.entity.MerchantOfflinePay;
import com.gxwebsoft.shop.param.MerchantOfflinePayParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.service.MerchantService;
import org.springframework.stereotype.Service;

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

/**
 * 商铺线下支付Service实现
 *
 * @author 科技小王子
 * @since 2025-05-28 18:23:45
 */
@Service
public class MerchantOfflinePayServiceImpl extends ServiceImpl<MerchantOfflinePayMapper, MerchantOfflinePay> implements MerchantOfflinePayService {

    @Resource
    private MerchantService merchantService;
    @Override
    public PageResult<MerchantOfflinePay> pageRel(MerchantOfflinePayParam param) {
        PageParam<MerchantOfflinePay, MerchantOfflinePayParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<MerchantOfflinePay> list = baseMapper.selectPageRel(page, param);
        for (MerchantOfflinePay merchantOfflinePay : list) {
            merchantOfflinePay.setMerchant(merchantService.getById(merchantOfflinePay.getMerchantId()));
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<MerchantOfflinePay> listRel(MerchantOfflinePayParam param) {
        List<MerchantOfflinePay> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<MerchantOfflinePay, MerchantOfflinePayParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        for (MerchantOfflinePay merchantOfflinePay : list) {
            merchantOfflinePay.setMerchant(merchantService.getById(merchantOfflinePay.getMerchantId()));
        }
        return page.sortRecords(list);
    }

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

    @Override
    public MerchantOfflinePay getByOrderNo(String orderNo) {
        return getOne(
                new LambdaQueryWrapper<MerchantOfflinePay>()
                        .eq(MerchantOfflinePay::getOrderNo, orderNo)
                        .last("limit 1")
        );
    }

    @Override
    public List<MerchantOfflinePay> listByMerchant(Integer merchantId) {
        return list(
                new LambdaQueryWrapper<MerchantOfflinePay>()
                        .eq(MerchantOfflinePay::getMerchantId, merchantId)
        );
    }

    @Override
    public Integer countByMerchant(Integer merchantId) {
        return count(
                new LambdaQueryWrapper<MerchantOfflinePay>()
                        .eq(MerchantOfflinePay::getMerchantId, merchantId)
        );
    }

    @Override
    public List<MerchantOfflinePay> countByMerchantIds(List<Integer> merchantIds) {
        return list(
                new QueryWrapper<MerchantOfflinePay>()
                        .select("COUNT(*) AS `total_num`", "merchant_id")
                        .groupBy("merchant_id")
                        .in("merchant_id", merchantIds)
        );
    }

}
