package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hezhi.domain.TbMemberAddress;
import com.ruoyi.hezhi.domain.dto.MemberAddressDTO;
import com.ruoyi.hezhi.mapper.TbMemberAddressMapper;
import com.ruoyi.hezhi.mapper.TbRegionMapper;
import com.ruoyi.hezhi.service.ITbMemberAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户地址Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-30
 */
@Service
public class TbMemberAddressServiceImpl implements ITbMemberAddressService
{
    @Autowired
    private TbMemberAddressMapper tbMemberAddressMapper;
    @Resource
    private TbRegionMapper tbRegionMapper;

    /**
     * 查询用户地址
     *
     * @param memberAddressId 用户地址主键
     * @return 用户地址
     */
    @Override
    public TbMemberAddress selectTbMemberAddressByMemberAddressId(Long memberAddressId)
    {
        return tbMemberAddressMapper.selectTbMemberAddressByMemberAddressId(memberAddressId);
    }

    /**
     * 查询用户地址列表
     *
     * @param tbMemberAddress 用户地址
     * @return 用户地址
     */
    @Override
    public List<TbMemberAddress> selectTbMemberAddressList(TbMemberAddress tbMemberAddress)
    {
        return tbMemberAddressMapper.selectTbMemberAddressList(tbMemberAddress);
    }

    /**
     * 新增用户地址
     *
     * @param tbMemberAddress 用户地址
     * @return 结果
     */
    @Override
    public int insertTbMemberAddress(TbMemberAddress tbMemberAddress)
    {
        tbMemberAddress.setCreateTime(DateUtils.getNowDate());
        return tbMemberAddressMapper.insertTbMemberAddress(tbMemberAddress);
    }

    /**
     * 修改用户地址
     *
     * @param tbMemberAddress 用户地址
     * @return 结果
     */
    @Override
    public int updateTbMemberAddress(TbMemberAddress tbMemberAddress)
    {
        tbMemberAddress.setUpdateTime(DateUtils.getNowDate());
        return tbMemberAddressMapper.updateTbMemberAddress(tbMemberAddress);
    }

    /**
     * 批量删除用户地址
     *
     * @param memberAddressIds 需要删除的用户地址主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberAddressByMemberAddressIds(Long[] memberAddressIds)
    {
        return tbMemberAddressMapper.deleteTbMemberAddressByMemberAddressIds(memberAddressIds);
    }

    /**
     * 删除用户地址信息
     *
     * @param memberAddressId 用户地址主键
     * @return 结果
     */
    @Override
    public int deleteTbMemberAddressByMemberAddressId(Long memberAddressId)
    {
        return tbMemberAddressMapper.deleteTbMemberAddressByMemberAddressId(memberAddressId);
    }

    /**
     * 地址管理列表
     *
     * @param params 参数
     * @return 地址管理列表
     */
    @Override
    public List<Map<String, Object>> getMemberAddressList(Map<String, Object> params) {
        return tbMemberAddressMapper.getMemberAddressList(params);
    }

    /**
     * 地址管理详情
     *
     * @param params 参数
     * @return 地址管理详情
     */
    @Override
    public Map<String, Object> getMemberAddressInfo(Map<String, Object> params) {
        return tbMemberAddressMapper.getMemberAddressInfo(params);
    }

    /**
     * 新增地址
     *
     * @param memberAddressDTO
     * @return ok
     */
    @Override
    public AjaxResult userRetrievePassword(MemberAddressDTO memberAddressDTO) {
        TbMemberAddress memberAddress = new TbMemberAddress();
        if (StringUtils.isNotBlank(memberAddressDTO.getProvinceName())){
            Long provinceId = tbRegionMapper.selectIdByName1(memberAddressDTO.getProvinceName());
            if (provinceId==null){
                return AjaxResult.error("无法找到省名称");
            }else {
                memberAddressDTO.setProvinceId(provinceId);
            }
        }
        if (StringUtils.isNotBlank(memberAddressDTO.getCityName())){
            Long cityId = tbRegionMapper.selectIdByName2(memberAddressDTO.getCityName());
            if (cityId==null){
                return AjaxResult.error("无法找到市名称");
            }else {
                memberAddressDTO.setCityId(cityId);
            }
        }
        if (StringUtils.isNotBlank(memberAddressDTO.getAreaName())){
            Long areaId = tbRegionMapper.selectIdByName3(memberAddressDTO.getAreaName(),memberAddressDTO.getCityId());
            if (areaId==null){
                return AjaxResult.error("无法找到区名称");
            }else {
                memberAddressDTO.setAreaId(areaId);
            }
        }
        memberAddress.setMemberUserId(memberAddressDTO.getMemberUserId());
        memberAddress.setName(memberAddressDTO.getName());
        memberAddress.setPhone(memberAddressDTO.getPhone());
        memberAddress.setProvinceId(memberAddressDTO.getProvinceId());
        memberAddress.setCityId(memberAddressDTO.getCityId());
        memberAddress.setAreaId(memberAddressDTO.getAreaId());
        memberAddress.setAddress(memberAddressDTO.getAddress());
        memberAddress.setLocation(memberAddressDTO.getLocation());
        memberAddress.setCreateTime(DateUtils.getNowDate());
        tbMemberAddressMapper.insertTbMemberAddress(memberAddress);
        return AjaxResult.success();
    }

    /**
     * 修改地址
     *
     * @param memberAddressDTO
     * @return ok
     */
    @Override
    public AjaxResult updateMemberAddress(MemberAddressDTO memberAddressDTO) {
        TbMemberAddress memberAddress = new TbMemberAddress();
        memberAddress.setMemberAddressId(memberAddressDTO.getMemberAddressId());
        memberAddress.setName(memberAddressDTO.getName());
        memberAddress.setPhone(memberAddressDTO.getPhone());
        memberAddress.setProvinceId(memberAddressDTO.getProvinceId());
        memberAddress.setCityId(memberAddressDTO.getCityId());
        memberAddress.setAreaId(memberAddressDTO.getAreaId());
        memberAddress.setAddress(memberAddressDTO.getAddress());
        memberAddress.setLocation(memberAddressDTO.getLocation());
        memberAddress.setUpdateTime(DateUtils.getNowDate());
        tbMemberAddressMapper.updateTbMemberAddress(memberAddress);
        return AjaxResult.success();
    }

    /**
     * 删除地址
     *
     * @param memberAddressDTO
     * @return ok
     */
    @Override
    public AjaxResult deleteMemberAddress(MemberAddressDTO memberAddressDTO) {
        tbMemberAddressMapper.deleteTbMemberAddressByMemberAddressId(memberAddressDTO.getMemberAddressId());
        return AjaxResult.success();
    }
}
