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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.SystemFieldConstants;
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;

/**
 * 会员收货信息 服务实现类
 *
 * @author hzs
 * @since 2022-08-22
 */
@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()) {
            // 设为默认，需要把之前库中的更新为非默认
            UpdateWrapper<CuMemberReceiving> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("PK_MEMBER", userId);
            updateWrapper.set("IS_DEFAULT", EDefault.NO.getValue());
            updateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
            updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, 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();

        // 设为默认，需要把之前库中的更新为非默认
        UpdateWrapper<CuMemberReceiving> updateOldWrapper = new UpdateWrapper<>();
        updateOldWrapper.eq("PK_MEMBER", userId);
        updateOldWrapper.set("IS_DEFAULT", EDefault.NO.getValue());
        updateOldWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        updateOldWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        this.update(updateOldWrapper);

        // 更新新的为默认地址
        UpdateWrapper<CuMemberReceiving> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        updateWrapper.eq("PK_MEMBER", userId);
        updateWrapper.set("IS_DEFAULT", EDefault.YES.getValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, userId);
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, nowDate);
        return this.update(updateWrapper);
    }
}
