package com.xmomen.module.member.service.impl;

import com.xmomen.framework.mybatis.dao.MybatisDao;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.framework.utils.StringUtilsExt;
import com.xmomen.module.member.entity.MemberAddress;
import com.xmomen.module.member.entity.MemberAddressExample;
import com.xmomen.module.member.mapper.MemberAddressMapperExt;
import com.xmomen.module.member.model.MemberAddressModel;
import com.xmomen.module.member.model.MemberAddressQuery;
import com.xmomen.module.member.service.MemberAddressService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class MemberAddressServiceImpl implements MemberAddressService {

    //注入MybatisDao
    @Autowired
    MybatisDao mybatisDao;

    //创建会员地址
    @Override
    @Transactional
    public MemberAddressModel createMemberAddress(MemberAddressModel memberAddressModel) {
        MemberAddress memberAddress = createMemberAddress(memberAddressModel.getEntity());
        if (memberAddress != null) {
            return getOneMemberAddressModel(memberAddress.getId());
        }
        return null;
    }

    //创建会员地址
    @Override
    @Transactional
    public MemberAddress createMemberAddress(MemberAddress memberAddress) {
        //如果地址id为空，则生成一个新的id
        if (StringUtils.trimToNull(memberAddress.getId()) == null) {
            memberAddress.setId(StringUtilsExt.getUUID(32));
        }
        return mybatisDao.insertByModel(memberAddress);
    }

    //批量创建会员地址
    @Override
    @Transactional
    public List<MemberAddressModel> createMemberAddresss(List<MemberAddressModel> memberAddressModels) {
        List<MemberAddressModel> memberAddressModelList = null;
        for (MemberAddressModel memberAddressModel : memberAddressModels) {
            memberAddressModel = createMemberAddress(memberAddressModel);
            if (memberAddressModel != null) {
                if (memberAddressModelList == null) {
                    memberAddressModelList = new ArrayList<>();
                }
                memberAddressModelList.add(memberAddressModel);
            }
        }
        return memberAddressModelList;
    }

    //更新会员地址
    @Override
    @Transactional
    public void updateMemberAddress(MemberAddressModel memberAddressModel) {
        mybatisDao.update(memberAddressModel.getEntity());
    }

    //更新会员地址
    @Override
    @Transactional
    public void updateMemberAddress(MemberAddress memberAddress) {
        mybatisDao.update(memberAddress);
    }

    //删除会员地址
    @Override
    @Transactional
    public void deleteMemberAddress(String[] ids) {
        MemberAddressExample memberAddressExample = new MemberAddressExample();
        memberAddressExample.createCriteria().andIdIn(Arrays.<String>asList((String[]) ids));
        mybatisDao.deleteByExample(memberAddressExample);
    }

    //删除会员地址
    @Override
    @Transactional
    public void deleteMemberAddress(String id) {
        mybatisDao.deleteByPrimaryKey(MemberAddress.class, id);
    }

    //分页查询会员地址
    @Override
    public Page<MemberAddressModel> getMemberAddressModelPage(int limit, int offset, MemberAddressQuery memberAddressQuery) {
        return (Page<MemberAddressModel>) mybatisDao.selectPage(MemberAddressMapperExt.MemberAddressMapperNameSpace + "getMemberAddressModel", memberAddressQuery, limit, offset);
    }

    //分页查询会员地址
    @Override
    public Page<MemberAddressModel> getMemberAddressModelPage(int limit, int offset) {
        return (Page<MemberAddressModel>) mybatisDao.selectPage(MemberAddressMapperExt.MemberAddressMapperNameSpace + "getMemberAddressModel", null, limit, offset);
    }

    //查询会员地址列表
    @Override
    public List<MemberAddressModel> getMemberAddressModelList(MemberAddressQuery memberAddressQuery) {
        return mybatisDao.getSqlSessionTemplate().selectList(MemberAddressMapperExt.MemberAddressMapperNameSpace + "getMemberAddressModel", memberAddressQuery);
    }

    //查询会员地址列表
    @Override
    public List<MemberAddressModel> getMemberAddressModelList() {
        return mybatisDao.getSqlSessionTemplate().selectList(MemberAddressMapperExt.MemberAddressMapperNameSpace + "getMemberAddressModel");
    }

    //根据id查询会员地址
    @Override
    public MemberAddress getOneMemberAddress(String id) {
        return mybatisDao.selectByPrimaryKey(MemberAddress.class, id);
    }

    //根据id查询会员地址
    @Override
    public MemberAddressModel getOneMemberAddressModel(String id) {
        MemberAddressQuery memberAddressQuery = new MemberAddressQuery();
        memberAddressQuery.setId(id);
        return mybatisDao.getSqlSessionTemplate().selectOne(MemberAddressMapperExt.MemberAddressMapperNameSpace + "getMemberAddressModel", memberAddressQuery);
    }

    //根据查询条件查询会员地址
    @Override
    public MemberAddressModel getOneMemberAddressModel(MemberAddressQuery memberAddressQuery) throws TooManyResultsException {
        return mybatisDao.getSqlSessionTemplate().selectOne(MemberAddressMapperExt.MemberAddressMapperNameSpace + "getMemberAddressModel", memberAddressQuery);
    }

    //设置默认地址
    public void defaultAddress(String addressId) {
        MemberAddress memberAddress = this.getOneMemberAddress(addressId);
        //将收货地址全部更新为非默认
        mybatisDao.getSqlSessionTemplate().update(MemberAddressMapperExt.MemberAddressMapperNameSpace + "updateAddressByMemberId", memberAddress.getCdMemberId());
        memberAddress.setIsDefault(true);
        this.mybatisDao.updateByModel(memberAddress);
    }

    //根据查询条件查询会员地址列表
    @Override
    public List<MemberAddressModel> getMemberAddressModels(MemberAddressQuery memberAddressQuery) {
        return mybatisDao.getSqlSessionTemplate().selectList(MemberAddressMapperExt.MemberAddressMapperNameSpace + "getMemberAddressModel", memberAddressQuery);
    }
}
