package com.sqx.modules.shop.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.exception.SqxException;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.datacentre.entity.SysUserShop;
import com.sqx.modules.goods.entity.GoodsShop;
import com.sqx.modules.goods.service.GoodsShopService;
import com.sqx.modules.shop.dao.ShopBankCardInfoDao;
import com.sqx.modules.shop.entity.ShopBankCardInfo;
import com.sqx.modules.shop.service.ShopBankCardInfoService;
import com.sqx.modules.sys.service.SysUserShopService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ShopBankCardInfoServiceImpl extends ServiceImpl<ShopBankCardInfoDao, ShopBankCardInfo> implements ShopBankCardInfoService {

    private final UserService userService;
    private final SysUserShopService userShopService;
    private final GoodsShopService goodsShopService;


    @Override
    public void saveCardInfo(ShopBankCardInfo shopBankCardInfo) {
        // 校验店铺下是否存在店铺信息
        checkUnique(shopBankCardInfo);

        this.save(shopBankCardInfo);
    }

    @Override
    public void updateCardInfo(ShopBankCardInfo shopBankCardInfo) {
        // 校验店铺下是否存在店铺信息
        checkUnique(shopBankCardInfo);

        this.updateById(shopBankCardInfo);
    }

    @Override
    public ShopBankCardInfo getByShopId(Long shopId) {
        LambdaQueryWrapper<ShopBankCardInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShopBankCardInfo::getShopId, shopId);
        ShopBankCardInfo cardInfo = this.getOne(queryWrapper);
        if (ObjectUtil.isNull(cardInfo)) {
            throw new SqxException("当前店铺未录入银行卡信息");
        }
        GoodsShop goodsShop=goodsShopService.getById(shopId);
        cardInfo.setMchId(goodsShop==null?null:goodsShop.getMchId());
        return cardInfo;
    }

    @Override
    public ShopBankCardInfo getByUserId(Long userId) {
        SysUserShop userShop = userShopService.getByUserId(userId);
        if (ObjectUtil.isNull(userShop)) {
            throw new SqxException("当前用户未绑定商户");
        }

        return getByShopId(userShop.getShopId());
    }

    @Override
    public void deleteByShopId(Long shopId) {
        LambdaQueryWrapper<ShopBankCardInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShopBankCardInfo::getShopId, shopId);

        this.remove(queryWrapper);
    }

    private void checkUnique(ShopBankCardInfo shopBankCardInfo) {
        LambdaQueryWrapper<ShopBankCardInfo> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ShopBankCardInfo::getShopId, shopBankCardInfo.getShopId());
        queryWrapper.ne(ObjectUtil.isNotNull(shopBankCardInfo.getId()), ShopBankCardInfo::getId, shopBankCardInfo.getId());

        if (count(queryWrapper) > 0) {
            throw new SqxException("当前店铺已存在银行卡信息");
        }
    }
}
