package com.ruoyi.mall.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.vo.MemberAddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMemberAddressMapper;
import com.ruoyi.mall.domain.MallMemberAddress;
import com.ruoyi.mall.service.IMallMemberAddressService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收货地址Service业务层处理
 * 
 * @author chang
 * @date 2021-11-25
 */
@Service
public class MallMemberAddressServiceImpl implements IMallMemberAddressService 
{
    @Autowired
    private MallMemberAddressMapper mallMemberAddressMapper;

    /**
     * 查询收货地址
     * 
     * @param id 收货地址主键
     * @return 收货地址
     */
    @Override
    public MallMemberAddress selectMallMemberAddressById(Long id)
    {
        return mallMemberAddressMapper.selectMallMemberAddressById(id);
    }

    @Override
    public MallMemberAddress getDefault(String userId) {
        return mallMemberAddressMapper.getDefault(userId);
    }

    /**
     * 查询收货地址列表
     * 
     * @param mallMemberAddress 收货地址
     * @return 收货地址
     */
    @Override
    public List<MallMemberAddress> selectMallMemberAddressList(MallMemberAddress mallMemberAddress)
    {
        return mallMemberAddressMapper.selectMallMemberAddressList(mallMemberAddress);
    }

    @Override
    public List<MallMemberAddress> queryByUserId(Map<String, Object> map) {
        return mallMemberAddressMapper.queryByUserId(map);
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
    public int add(Map<String, Object> map) {
        String userId = map.get("userId").toString();
        String province = map.get("province").toString();
        String city = map.get("city").toString();
        String country = map.get("area").toString();
        String address = map.get("address").toString();
        String name = map.get("name").toString();
        String phone = map.get("phone").toString();
        String defaul = map.get("default").toString();
        MallMemberAddress addressDO = new MallMemberAddress();
        addressDO.setAddress(address);
        addressDO.setCity(city);
        addressDO.setName(name);
        addressDO.setProvince(province);
        addressDO.setArea(country);
        addressDO.setPhone(phone);
        addressDO.setIsDefault(defaul);
        addressDO.setCreateAt(DateUtils.getTime());
        addressDO.setUserId(Long.parseLong(userId));
        if ("1".equals(defaul)){
            mallMemberAddressMapper.removeDefault(userId);
        }
        return mallMemberAddressMapper.insertMallMemberAddress(addressDO);
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
    public int change(Map<String, Object> map) {
        String addressId = map.get("id").toString();
        String userId = map.get("userId").toString();
        String city = map.get("city").toString();
        String area = map.get("area").toString();
        String address = map.get("address").toString();
        String name = map.get("name").toString();
        String defaul = map.get("default").toString();
        MallMemberAddress addressDO = mallMemberAddressMapper.selectMallMemberAddressById(Long.parseLong(addressId));
        addressDO.setName(name);
        addressDO.setPhone(map.get("phone").toString());
        addressDO.setProvince(map.get("province").toString());
        addressDO.setCity(city);
        addressDO.setArea(area);
        addressDO.setAddress(address);
        addressDO.setIsDefault(defaul);
        addressDO.setCreateAt(DateUtils.getTime());
        addressDO.setUserId(Long.parseLong(userId));
        if ("1".equals(defaul)){
            mallMemberAddressMapper.removeDefault(userId);
        }
        return mallMemberAddressMapper.updateMallMemberAddress(addressDO);
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
    public ApiResult toDefault(Map<String,Object> map) {
        String addressId = map.get("addressId").toString();
        String userId = map.get("userId").toString();
        MallMemberAddress addressDO = mallMemberAddressMapper.selectMallMemberAddressById(Long.parseLong(addressId));
        if (!userId.equals(addressDO.getUserId().toString())){
            return ApiResult.error("收货地址异常");
        }
        mallMemberAddressMapper.removeDefault(userId);
        addressDO.setIsDefault("1");
        mallMemberAddressMapper.updateMallMemberAddress(addressDO);
        return ApiResult.ok();
    }

    /**
     * 新增收货地址
     * 
     * @param mallMemberAddress 收货地址
     * @return 结果
     */
    @Override
    public int insertMallMemberAddress(MallMemberAddress mallMemberAddress)
    {
        return mallMemberAddressMapper.insertMallMemberAddress(mallMemberAddress);
    }

    /**
     * 修改收货地址
     * 
     * @param mallMemberAddress 收货地址
     * @return 结果
     */
    @Override
    public int updateMallMemberAddress(MallMemberAddress mallMemberAddress)
    {
        return mallMemberAddressMapper.updateMallMemberAddress(mallMemberAddress);
    }

    /**
     * 批量删除收货地址
     * 
     * @param ids 需要删除的收货地址主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberAddressByIds(Long[] ids)
    {
        return mallMemberAddressMapper.deleteMallMemberAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     * 
     * @param id 收货地址主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberAddressById(Long id)
    {
        return mallMemberAddressMapper.removeMallMemberAddressById(id);
    }

    @Override
    public MemberAddressVO selectMallMemberAddressVOById(long l) {
        return mallMemberAddressMapper.selectMallMemberAddressVOById(l);
    }

    @Override
    public List<MemberAddressVO> queryAddressVOByUserId(Map<String, Object> params) {
        return mallMemberAddressMapper.queryAddressVOByUserId(params);
    }

    @Override
    public MemberAddressVO getDefaultVO(Long userId) {
        return mallMemberAddressMapper.getDefaultVO(userId);
    }
}
