package com.ylx.massage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.utils.StringUtils;
import com.ylx.massage.mapper.TAddressMapper;
import com.ylx.massage.domain.TAddress;
import com.ylx.massage.service.TAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 轮播图(TAddress)表服务实现类
 *
 * @author makejava
 * @since 2024-04-11 17:18:53
 */
@Service("tAddressService")
public class TAddressServiceImpl extends ServiceImpl<TAddressMapper, TAddress> implements TAddressService {

    @Override
    public TAddress getByOpenId(String openId) {
        LambdaQueryWrapper<TAddress> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(TAddress::getOpenid, openId).eq(TAddress::getType, 1);
        return this.getOne(objectLambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean defaultAddress(TAddress tAddress) {
        if (StringUtils.isBlank(tAddress.getOpenid())) {
            throw new RuntimeException("openid不能为空");
        }
        // 查询默认地址
        TAddress oldAddress = this.getByOpenId(tAddress.getOpenid());
        if (oldAddress != null) {
            oldAddress.setType(0);
            this.updateById(oldAddress);
        }
        tAddress.setType(1);
        return this.updateById(tAddress);
    }

    @Override
    public Object insertAddress(TAddress tAddress) {
        if (StringUtils.isBlank(tAddress.getOpenid())) {
            throw new RuntimeException("openid不能为空");
        }
        //查询默认地址
        TAddress oldAddress = this.getByOpenId(tAddress.getOpenid());
        tAddress.setType(1);
        if (oldAddress != null) {
            tAddress.setType(0);
        }
        return this.save(tAddress);
    }

     @Override
    @Transactional(rollbackFor = Exception.class)
    public Object insertVirtualAddress(TAddress tAddress) {
        if (StringUtils.isBlank(tAddress.getOpenid())) {
            throw new RuntimeException("openid不能为空");
        }
        //检查用户类型是否为空
        if (Objects.isNull(tAddress.getUserType())) {
            throw new RuntimeException("用户类型不能为空");
        }
        //检查用户类型是否为1或2
        if (tAddress.getUserType() != 1 && tAddress.getUserType() != 2) {
            throw new RuntimeException("用户类型只能为1或2");
        }
        //检查用户类型是否正确
         if (tAddress.getUserType() != 2) {
             throw new RuntimeException("用户类型错误，用户类型只能为2");
         }

         //检查地址类型是否为空
         if (Objects.isNull(tAddress.getType())) {
             throw new RuntimeException("地址类型不能为空");
         }
         //检查地址类型是否为1或2
         if (tAddress.getType() != 2) {
             throw new RuntimeException("地址类型错误，地址类型只能为2");
         }
        return this.save(tAddress);
    }

    @Override
    public Object updateAddress(TAddress tAddress) {
        if (StringUtils.isBlank(tAddress.getOpenid())) {
            throw new RuntimeException("openid不能为空");
        }
        if (StringUtils.isBlank(tAddress.getId())) {
            throw new RuntimeException("地址id不能为空");
        }
        //查询默认地址
        TAddress oldAddress = this.getByOpenId(tAddress.getOpenid());
        tAddress.setType(1);
        if (oldAddress != null && !oldAddress.getId().equals(tAddress.getId())) {
            tAddress.setType(0);
        }
        return this.updateById(tAddress);
    }
}

