package com.xinelu.manage.service.goodsOrder.impl;

import com.xinelu.common.enums.ConfirmRefundStatusEnum;
import com.xinelu.common.enums.GooodsOrderStatusEnum;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.manage.domain.goodsOrder.GoodsOrder;
import com.xinelu.manage.dto.goodsorder.GoodsOrderDTO;
import com.xinelu.manage.mapper.goodsOrder.GoodsOrderMapper;
import com.xinelu.manage.service.goodsOrder.IGoodsOrderService;
import com.xinelu.manage.vo.goodsorder.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 商品订单Service业务层处理
 *
 * @author xinyilu
 * @date 2022-10-18
 */
@Service
public class GoodsOrderServiceImpl implements IGoodsOrderService {
    @Resource
    private GoodsOrderMapper goodsOrderMapper;

    /**
     * 查询商品订单
     *
     * @param id 商品订单主键
     * @return 商品订单
     */
    @Override
    public GoodsOrder selectGoodsOrderById(Long id) {
        return goodsOrderMapper.selectGoodsOrderById(id);
    }

    /**
     * 查询商品订单列表
     *
     * @param goodsOrder 商品订单
     * @return 商品订单
     */
    @Override
    public List<GoodsInfoOrderVO> selectGoodsOrderList(GoodsInfoOrderVO goodsOrder) {
        return goodsOrderMapper.selectGoodsOrderList(goodsOrder);
    }

    /**
     * 新增商品订单
     *
     * @param goodsOrder 商品订单
     * @return 结果
     */
    @Override
    public int insertGoodsOrder(GoodsOrder goodsOrder) {
        //设置创建人创建时间
        goodsOrder.setCreateTime(LocalDateTime.now());
        goodsOrder.setCreateBy(SecurityUtils.getUsername());
        return goodsOrderMapper.insertGoodsOrder(goodsOrder);
    }

    /**
     * 修改商品订单
     *
     * @param goodsOrder 商品订单
     * @return 结果
     */
    @Override
    public int updateGoodsOrder(GoodsOrder goodsOrder) {
        //订单状态 已退款：REFUNDED
        goodsOrder.setOrderStatus(GooodsOrderStatusEnum.REFUNDED.getInfo());
        //确认退款状态 已确认：CONFIRMED
        goodsOrder.setConfirmRefundStatus(ConfirmRefundStatusEnum.CONFIRMED.getInfo());
        return goodsOrderMapper.updateGoodsOrder(goodsOrder);
    }

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

    /**
     * 删除商品订单信息
     *
     * @param id 商品订单主键
     * @return 结果
     */
    @Override
    public int deleteGoodsOrderById(Long id) {
        return goodsOrderMapper.deleteGoodsOrderById(id);
    }


    /**
     * 查询商品订单详情
     *
     * @param orderNo 商品订单编号
     * @return com.xinyilu.base.vo.goodsorder.GoodsOrderVO
     **/
    @Override
    public GoodsOrderVO getGoodsOrderByOrder(String orderNo) {
        return goodsOrderMapper.getGoodsOrderByOrder(orderNo);
    }


    /**
     * 修改商品订单 添加物流单号
     *
     * @param goodsOrder 商品订单
     * @return 结果
     */
    @Override
    public int updateGoodsOrderExpressNo(GoodsOrder goodsOrder) {
        return goodsOrderMapper.updateGoodsOrder(goodsOrder);
    }

    /**
     * 根据订单id查询商品退款原因
     *
     * @param goodsOrderId 订单id
     * @return com.xinyilu.base.vo.goodsorder.GoodsOrderRefundVO
     **/
    @Override
    public GoodsOrderRefundVO getGoodsOrderRefund(Long goodsOrderId) {
        return goodsOrderMapper.getGoodsOrderRefund(goodsOrderId);
    }

    @Override
    public List<AppGoodsOrderVO> getConsultationOrders(GoodsOrderDTO goodsOrderDTO) {
        return goodsOrderMapper.getConsultationOrders(goodsOrderDTO);
    }

    @Override
    public int updateStatus(GoodsOrderDTO goodsOrderDTO) {
        GoodsOrder goodsOrder = new GoodsOrder();
        goodsOrder.setId(goodsOrderDTO.getGoodsOrderId());
        goodsOrder.setOrderStatus(goodsOrderDTO.getOrderStatus());
        return goodsOrderMapper.updateGoodsOrder(goodsOrder);
    }

    /**
     * 查询订单信息具体明细
     *
     * @param orderNo 订单编号
     * @return GoodsOrderDetailsVO
     */
    @Override
    public GoodsOrderDetailsVO getGoodsOrderDetailsByOrderNo(String orderNo) {
        //根据订单编号查询预约订单管理列表页面查看明细
        return goodsOrderMapper.selectGoodsOrderDetailsByOrderNo(orderNo);
    }
}
