package com.ruoyi.mall.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.mall.domain.dto.MallAppOrderGoodsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallAppOrderGoodsMapper;
import com.ruoyi.mall.domain.MallAppOrderGoods;
import com.ruoyi.mall.service.IMallAppOrderGoodsService;

import javax.annotation.Resource;

/**
 * app-线下订单商品Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-29
 */
@Service
public class MallAppOrderGoodsServiceImpl implements IMallAppOrderGoodsService
{
    @Resource
    private MallAppOrderGoodsMapper mallAppOrderGoodsMapper;

    /**
     * 查询app-线下订单商品
     *
     * @param id app-线下订单商品主键
     * @return app-线下订单商品
     */
    @Override
    public MallAppOrderGoods selectMallAppOrderGoodsById(Long id)
    {
        return mallAppOrderGoodsMapper.selectMallAppOrderGoodsById(id);
    }

    /**
     * 查询app-线下订单商品列表
     *
     * @param mallAppOrderGoods app-线下订单商品
     * @return app-线下订单商品
     */
    @Override
    public List<MallAppOrderGoods> selectMallAppOrderGoodsList(MallAppOrderGoods mallAppOrderGoods)
    {
        return mallAppOrderGoodsMapper.selectMallAppOrderGoodsList(mallAppOrderGoods);
    }

    /**
     * 新增app-线下订单商品
     *
     * @param mallAppOrderGoods app-线下订单商品
     * @return 结果
     */
    @Override
    public int insertMallAppOrderGoods(MallAppOrderGoods mallAppOrderGoods)
    {
        mallAppOrderGoods.setDelFlag("0");
        return mallAppOrderGoodsMapper.insertMallAppOrderGoods(mallAppOrderGoods);
    }

    /**
     * 修改app-线下订单商品
     *
     * @param mallAppOrderGoods app-线下订单商品
     * @return 结果
     */
    @Override
    public int updateMallAppOrderGoods(MallAppOrderGoods mallAppOrderGoods)
    {
        return mallAppOrderGoodsMapper.updateMallAppOrderGoods(mallAppOrderGoods);
    }

    /**
     * 批量删除app-线下订单商品
     *
     * @param ids 需要删除的app-线下订单商品主键
     * @return 结果
     */
    @Override
    public int deleteMallAppOrderGoodsByIds(Long[] ids)
    {
        return mallAppOrderGoodsMapper.deleteMallAppOrderGoodsByIds(ids);
    }

    /**
     * 删除app-线下订单商品信息
     *
     * @param id app-线下订单商品主键
     * @return 结果
     */
    @Override
    public int deleteMallAppOrderGoodsById(Long id)
    {
        return mallAppOrderGoodsMapper.deleteMallAppOrderGoodsById(id);
    }

    /**
     * 批量新增app-线下订单商品
     *
     * @param list app-线下订单商品列表
     * @return 结果
     */
    @Override
    public int bachInsertMallAppOrderGoods(List<MallAppOrderGoodsDTO> list, Long orderId, String orderNo) {
        List<MallAppOrderGoods> mallAppOrderGoodsList = list.stream()
                .map(person ->{
                    MallAppOrderGoods add = new MallAppOrderGoods();
                    add = BeanUtil.toBean(person,MallAppOrderGoods.class);
                    add.setOrderId(orderId);
                    add.setOrderNo(orderNo);
                    // 商品数量
                    Integer goodsTotal = person.getGoodsTotal();
                    // 商品单价
                    BigDecimal goodsPrice = person.getGoodsPrice().setScale(2, RoundingMode.HALF_UP);
                    add.setTotalPrice(goodsPrice.multiply(new BigDecimal(goodsTotal)));
                    add.setDelFlag("0");
                    return add;
                }) // 转换对象类型
                .collect(Collectors.toList());
        return mallAppOrderGoodsMapper.bachInsertMallAppOrderGoods(mallAppOrderGoodsList);
    }

    /**
     * 查询app-线下订单商品列表
     *
     * @param orderId app-线下订单ID
     * @return app-线下订单商品
     */
    @Override
    public List<MallAppOrderGoods> selectMallAppOrderGoodsListByOrderId(Long orderId) {
        return mallAppOrderGoodsMapper.selectMallAppOrderGoodsListByOrderId(orderId);
    }

    /**
     * 删除app-线下订单商品信息
     *
     * @param orderId app-线下订单ID
     * @return 结果
     */
    @Override
    public int deleteMallAppOrderGoodsByOrderId(Long orderId) {
        return mallAppOrderGoodsMapper.deleteMallAppOrderGoodsByOrderId(orderId);
    }

    /**
     * 查询app-线下订单商品名称列表
     *
     * @param params 参数
     * @return app-线下订单商品集合
     */
    @Override
    public List<Map<String, Object>> getAppGoodsNameList(Map<String, Object> params) {
        return mallAppOrderGoodsMapper.getAppGoodsNameList(params);
    }
}
