package com.hzs.member.address.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EDefault;
import com.hzs.common.domain.member.address.CuMemberReceiving;
import com.hzs.member.address.mapper.CuMemberReceivingMapper;
import com.hzs.member.address.service.ICuMemberReceivingService;
import com.hzs.member.base.param.MemberAddressParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 会员收货信息 服务实现类
 */
@Service
public class CuMemberReceivingServiceImpl extends ServiceImpl<CuMemberReceivingMapper, CuMemberReceiving> implements ICuMemberReceivingService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(MemberAddressParam param, Long userId, Integer pkCountry) {
        Date nowDate = new Date();

        if (EDefault.YES.getValue() == param.getIsDefault()) {
            // 设为默认，需要把之前库中的更新为非默认
            LambdaUpdateWrapper<CuMemberReceiving> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(CuMemberReceiving::getPkMember, userId);
            updateWrapper.set(CuMemberReceiving::getIsDefault, EDefault.NO.getValue());
            updateWrapper.set(CuMemberReceiving::getPkModified, userId);
            updateWrapper.set(CuMemberReceiving::getModifiedTime, nowDate);
            this.update(updateWrapper);
        }

        CuMemberReceiving cuMemberReceiving = BeanUtil.copyProperties(param, CuMemberReceiving.class);
        cuMemberReceiving.setPkMember(userId);
        if (null == param.getPkId()) {
            // 新増
            cuMemberReceiving.setPkCreator(userId);
            cuMemberReceiving.setCreationTime(nowDate);
            cuMemberReceiving.setPkCountry(pkCountry);
        } else {
            // 修改
            cuMemberReceiving.setPkModified(userId);
            cuMemberReceiving.setModifiedTime(nowDate);
        }
        return this.saveOrUpdate(cuMemberReceiving);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean setDefault(Long pkId, Long userId) {
        Date nowDate = new Date();

        // 设为默认，需要把之前库中的更新为非默认
        LambdaUpdateWrapper<CuMemberReceiving> updateOldWrapper = new LambdaUpdateWrapper<>();
        updateOldWrapper.eq(CuMemberReceiving::getPkMember, userId);
        updateOldWrapper.set(CuMemberReceiving::getIsDefault, EDefault.NO.getValue());
        updateOldWrapper.set(CuMemberReceiving::getPkModified, userId);
        updateOldWrapper.set(CuMemberReceiving::getModifiedTime, nowDate);
        this.update(updateOldWrapper);

        // 更新新的为默认地址
        LambdaUpdateWrapper<CuMemberReceiving> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CuMemberReceiving::getPkId, pkId);
        updateWrapper.eq(CuMemberReceiving::getPkMember, userId);
        updateWrapper.set(CuMemberReceiving::getIsDefault, EDefault.YES.getValue());
        updateWrapper.set(CuMemberReceiving::getPkModified, userId);
        updateWrapper.set(CuMemberReceiving::getModifiedTime, nowDate);
        return this.update(updateWrapper);
    }
}
