package com.hwrj.cloud.portal.service.impl;

import com.hwrj.cloud.portal.mapper.UmsMemberReceiveAddressMapper;
import com.hwrj.cloud.portal.model.UmsMember;
import com.hwrj.cloud.portal.model.UmsMemberReceiveAddress;
import com.hwrj.cloud.portal.model.UmsMemberReceiveAddressExample;
import com.hwrj.cloud.portal.service.UmsMemberReceiveAddressService;
import com.hwrj.cloud.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 用户地址管理Service实现类
 * Created by macro on 2018/8/28.
 */
@Service
public class UmsMemberReceiveAddressServiceImpl implements UmsMemberReceiveAddressService {
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private UmsMemberReceiveAddressMapper addressMapper;

    @Override
    @Transactional
    public int add(UmsMemberReceiveAddress address) {
        UmsMember currentMember = memberService.getCurrentMember();
        address.setMemberId(currentMember.getId());
        int i = addressMapper.insert(address);
        if (address.getDefaultStatus() == 1) {
            defaultStatus(address.getId());
        }
        return i;
    }

    @Override
    @Transactional
    public int delete(Long id) {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        return addressMapper.deleteByExample(example);
    }

    @Override
    @Transactional
    public int update(Long id, UmsMemberReceiveAddress address) {
        address.setId(null);
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        if (address.getDefaultStatus() == 1) {
            //先将原来的默认地址去除
            UmsMemberReceiveAddress record = new UmsMemberReceiveAddress();
            record.setDefaultStatus(0);
            UmsMemberReceiveAddressExample updateExample = new UmsMemberReceiveAddressExample();
            updateExample.createCriteria()
                    .andMemberIdEqualTo(currentMember.getId())
                    .andDefaultStatusEqualTo(1);
            addressMapper.updateByExampleSelective(record, updateExample);
        }
        return addressMapper.updateByExampleSelective(address, example);
    }

    @Override
    public List<UmsMemberReceiveAddress> list(Long memberId) {
        UmsMember currentMember = memberService.getCurrentMember();
        if (memberId != null) {
            currentMember = memberService.getById(memberId);
        }
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId());
        example.setOrderByClause("default_status desc");
        return addressMapper.selectByExample(example);
    }

    @Override
    public UmsMemberReceiveAddress getItem(Long id) {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        List<UmsMemberReceiveAddress> addressList = addressMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(addressList)) {
            return addressList.get(0);
        }
        return null;
    }

    @Override
    @Transactional
    public int defaultStatus(Long id) {
        //更新自身
        updateLocal(id);
        //设置其他为不是默认地址
        int i = updateOther(id);
        return i;
    }

    private int updateOther(Long id) {
        UmsMemberReceiveAddress record = new UmsMemberReceiveAddress();
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        UmsMemberReceiveAddressExample.Criteria criteria = example.createCriteria();
        criteria.andIdNotEqualTo(id);
        criteria.andMemberIdEqualTo(currentMember.getId());
        record.setDefaultStatus(0);
        return addressMapper.updateByExampleSelective(record, example);
    }

    private UmsMemberReceiveAddress updateLocal(Long id) {
        UmsMemberReceiveAddress record = new UmsMemberReceiveAddress();
        record.setDefaultStatus(1);
        record.setId(id);
        addressMapper.updateByPrimaryKeySelective(record);
        return record;
    }
}
