package com.ruoyi.frequency.mallorderaddress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.frequency.malladdress.entity.MallAddress;
import com.ruoyi.frequency.malladdress.service.MallAddressService;
import com.ruoyi.frequency.mallorderaddress.entity.MallOrderAddress;
import com.ruoyi.frequency.mallorderaddress.mapper.MallOrderAddressMapper;
import com.ruoyi.frequency.mallorderaddress.service.MallOrderAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单地址Service业务层处理
 *
 * @author liwenlong
 * @date 2024-01-25
 */
@Service
public class MallOrderAddressServiceImpl extends ServiceImpl<MallOrderAddressMapper, MallOrderAddress>  implements MallOrderAddressService
{
    @Autowired
    private MallOrderAddressMapper mallOrderAddressMapper;

    /**
     * 查询订单地址
     *
     * @param id 订单地址主键
     * @return 订单地址
     */
    @Override
    public MallOrderAddress selectMallOrderAddressById(Long id)
    {
        return mallOrderAddressMapper.selectMallOrderAddressById(id);
    }

    /**
     * 查询订单地址列表
     *
     * @param mallOrderAddress 订单地址
     * @return 订单地址
     */
    @Override
    public List<MallOrderAddress> selectMallOrderAddressList(MallOrderAddress mallOrderAddress)
    {
        return mallOrderAddressMapper.selectMallOrderAddressList(mallOrderAddress);
    }

    /**
     * 新增订单地址
     *
     * @param mallOrderAddress 订单地址
     * @return 结果
     */
    @Override
    public int insertMallOrderAddress(MallOrderAddress mallOrderAddress)
    {
        mallOrderAddress.setCreateData();
        return mallOrderAddressMapper.insert(mallOrderAddress);
    }

    /**
     * 修改订单地址
     *
     * @param mallOrderAddress 订单地址
     * @return 结果
     */
    @Override
    public int updateMallOrderAddress(MallOrderAddress mallOrderAddress)
    {
        mallOrderAddress.setUpdateData();
        return mallOrderAddressMapper.updateMallOrderAddress(mallOrderAddress);
    }

    /**
     * 批量删除订单地址
     *
     * @param ids 需要删除的订单地址主键
     * @return 结果
     */
    @Override
    public int deleteMallOrderAddressByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(MallOrderAddress::getDelFlag,delFlag).in(MallOrderAddress::getId,ids).update();
        return ids.length;
    }

    @Autowired
    private MallAddressService addressService;

    @Override
    public MallOrderAddress assembleOrderAddress(Long orderId, Long addressId) {
        //用户地址
        MallAddress address = addressService.getById(addressId);

        MallOrderAddress orderAddress = this.baseMapper.selectOne(new QueryWrapper<MallOrderAddress>().lambda()
                .eq(MallOrderAddress::getOrderId, orderId));
        if (orderAddress == null) {
            orderAddress = new MallOrderAddress();
        }
        orderAddress.setOrderId(orderId)
                .setAreaCode(address.getAreaCode())
                .setConsigneeUsername(address.getUsername())
                .setConsigneeMobile(address.getMobile())
                .setProvinceName(address.getProvince())
                .setCityName(address.getCity())
                .setAreaName(address.getArea())
                .setAddress(address.getAddress())
                .setDetail(address.getDetail());
        return orderAddress;
    }
}
