package com.ruoyi.mall.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.dto.MallAppAddressDTO;
import com.ruoyi.mall.mapper.MallRegionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallAppAddressMapper;
import com.ruoyi.mall.domain.MallAppAddress;
import com.ruoyi.mall.service.IMallAppAddressService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * app地址管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-28
 */
@Service
public class MallAppAddressServiceImpl implements IMallAppAddressService
{
    @Resource
    private MallAppAddressMapper mallAppAddressMapper;
    @Resource
    private MallRegionMapper mallRegionMapper;

    /**
     * 查询app地址管理
     *
     * @param addressId app地址管理主键
     * @return app地址管理
     */
    @Override
    public MallAppAddress selectMallAppAddressByAddressId(Long addressId)
    {
        return mallAppAddressMapper.selectMallAppAddressByAddressId(addressId);
    }

    /**
     * 查询app地址管理列表
     *
     * @param mallAppAddress app地址管理
     * @return app地址管理
     */
    @Override
    public List<MallAppAddress> selectMallAppAddressList(MallAppAddress mallAppAddress)
    {
        return mallAppAddressMapper.selectMallAppAddressList(mallAppAddress);
    }

    /**
     * 新增app地址管理
     *
     * @param mallAppAddress app地址管理
     * @return 结果
     */
    @Override
    public int insertMallAppAddress(MallAppAddress mallAppAddress)
    {
        mallAppAddress.setCreateAt(DateUtils.getTime());
        mallAppAddress.setDelFlag("0");
        return mallAppAddressMapper.insertMallAppAddress(mallAppAddress);
    }

    /**
     * 修改app地址管理
     *
     * @param mallAppAddress app地址管理
     * @return 结果
     */
    @Override
    public int updateMallAppAddress(MallAppAddress mallAppAddress)
    {
        mallAppAddress.setUpdateAt(DateUtils.getTime());
        return mallAppAddressMapper.updateMallAppAddress(mallAppAddress);
    }

    /**
     * 批量删除app地址管理
     *
     * @param addressIds 需要删除的app地址管理主键
     * @return 结果
     */
    @Override
    public int deleteMallAppAddressByAddressIds(Long[] addressIds)
    {
        return mallAppAddressMapper.deleteMallAppAddressByAddressIds(addressIds);
    }

    /**
     * 删除app地址管理信息
     *
     * @param addressId app地址管理主键
     * @return 结果
     */
    @Override
    public int deleteMallAppAddressByAddressId(Long addressId)
    {
        return mallAppAddressMapper.deleteMallAppAddressByAddressId(addressId);
    }

    /**
     * 查询app地址管理列表
     *
     * @param params 参数
     * @return app地址管理集合
     */
    @Override
    public List<MallAppAddress> getAddressManageList(Map<String, Object> params) {
        return mallAppAddressMapper.getAddressManageList(params);
    }

    /**
     * 新增app地址管理
     *
     * @param resourceDTO app地址管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ApiResult insertMallAppAddressByDTO(MallAppAddressDTO resourceDTO) {
        MallAppAddress mallAppAddress = new MallAppAddress();
        mallAppAddress.setSysUserId(resourceDTO.getSysUserId());
        mallAppAddress.setAddressType(resourceDTO.getAddressType());
        mallAppAddress.setName(resourceDTO.getName());
        mallAppAddress.setPhone(resourceDTO.getPhone());
        mallAppAddress.setProvinceId(resourceDTO.getProvinceId());
        String provinceName = mallRegionMapper.getRegionName(resourceDTO.getProvinceId());
        mallAppAddress.setProvinceName(provinceName);
        mallAppAddress.setCityId(resourceDTO.getCityId());
        String cityName = mallRegionMapper.getRegionName(resourceDTO.getCityId());
        mallAppAddress.setCityName(cityName);
        mallAppAddress.setAreaId(resourceDTO.getAreaId());
        String areaName = mallRegionMapper.getRegionName(resourceDTO.getAreaId());
        mallAppAddress.setAreaName(areaName);
        mallAppAddress.setAddress(resourceDTO.getAddress());
        mallAppAddress.setCreateAt(DateUtils.getTime());
        mallAppAddressMapper.insertMallAppAddress(mallAppAddress);
        return ApiResult.ok("新增成功");
    }

    /**
     * 修改app地址管理
     *
     * @param resourceDTO app地址管理
     * @return 结果
     */
    @Override
    public ApiResult updateMallAppAddressByDTO(MallAppAddressDTO resourceDTO) {
        MallAppAddress mallAppAddress = new MallAppAddress();
        mallAppAddress.setAddressId(resourceDTO.getAddressId());
        mallAppAddress.setName(resourceDTO.getName());
        mallAppAddress.setPhone(resourceDTO.getPhone());
        mallAppAddress.setProvinceId(resourceDTO.getProvinceId());
        String provinceName = mallRegionMapper.getRegionName(resourceDTO.getProvinceId());
        mallAppAddress.setProvinceName(provinceName);
        mallAppAddress.setCityId(resourceDTO.getCityId());
        String cityName = mallRegionMapper.getRegionName(resourceDTO.getCityId());
        mallAppAddress.setCityName(cityName);
        mallAppAddress.setAreaId(resourceDTO.getAreaId());
        String areaName = mallRegionMapper.getRegionName(resourceDTO.getAreaId());
        mallAppAddress.setAreaName(areaName);
        mallAppAddress.setAddress(resourceDTO.getAddress());
        mallAppAddress.setUpdateAt(DateUtils.getTime());
        mallAppAddressMapper.updateMallAppAddress(mallAppAddress);
        return ApiResult.ok("修改成功");
    }
}
