package com.fdkankan.platform.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.user.entity.ReceiverInfo;
import com.fdkankan.platform.user.mapper.IReceiverInfoMapper;
import com.fdkankan.platform.user.request.RequestUser;
import com.fdkankan.platform.user.service.IReceiverInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收货人信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-12-24
 */
@Service
public class ReceiverInfoServiceImpl extends ServiceImpl<IReceiverInfoMapper, ReceiverInfo> implements IReceiverInfoService {

    @Override
    public ReceiverInfo getDefaultByUserId(Long userId){
        QueryWrapper<ReceiverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReceiverInfo::getTbStatus, TbStatus.VALID.code());
        queryWrapper.lambda().eq(ReceiverInfo::getUserId,userId);
        queryWrapper.lambda().eq(ReceiverInfo::getSetDefault,1);
        List<ReceiverInfo> list = this.list(queryWrapper);
        if(list == null || list.size()<=0){
            return null;
        }
        return list.get(0);
    }

    @Override
    public String saveByParam(RequestUser param, Long userId) {
        ReceiverInfo defaultEntity = getDefaultByUserId(userId);
        ReceiverInfo receiverInfoEntity = new ReceiverInfo();
        receiverInfoEntity.setSetDefault(0);
        if(defaultEntity == null){
            receiverInfoEntity.setSetDefault(1);
        }
        receiverInfoEntity.setUserId(userId);
        receiverInfoEntity.setShipAreaPath(param.getShipAreaPath());
        receiverInfoEntity.setShipAddress(param.getShipAddress());
        receiverInfoEntity.setShipName(param.getShipName());
        receiverInfoEntity.setShipMobile(param.getShipMobile());
        if(!this.save(receiverInfoEntity)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public String updateDefaultAddress(RequestUser param, Long userId) {
        if(param.getSetDefault() != null && param.getSetDefault() == 1){
            ReceiverInfo defaultEntity = this.getDefaultByUserId(userId);
            if(defaultEntity != null && defaultEntity.getId().intValue() != param.getId().intValue()){
                defaultEntity.setSetDefault(0);
                if(!this.updateById(defaultEntity)){
                    throw new BusinessException(ErrorCode.ERROR_MSG);
                }
            }
        }
        ReceiverInfo receiverInfoEntity = this.getById(param.getId());
        if(param.getSetDefault() != null){
            receiverInfoEntity.setSetDefault(param.getSetDefault());
        }
        receiverInfoEntity.setShipAreaPath(param.getShipAreaPath());
        receiverInfoEntity.setShipAddress(param.getShipAddress());
        receiverInfoEntity.setShipName(param.getShipName());
        receiverInfoEntity.setShipMobile(param.getShipMobile());
        if(!this.updateById(receiverInfoEntity)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }

    @Override
    public List<ReceiverInfo> getListByUserId(Long userId) {
        QueryWrapper<ReceiverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReceiverInfo::getTbStatus,TbStatus.VALID.code());
        queryWrapper.lambda().eq(ReceiverInfo::getUserId,userId);
        return this.list(queryWrapper);
    }

    @Override
    public ReceiverInfo findDefaultByUserId(Long userId) {
        QueryWrapper<ReceiverInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ReceiverInfo::getTbStatus,TbStatus.VALID.code());
        queryWrapper.lambda().eq(ReceiverInfo::getSetDefault,1);
        List<ReceiverInfo> list = this.list(queryWrapper);
        if(list == null || list.size()<=0){
            return null;
        }
        return list.get(0);
    }
}
