package com.younike.system.service.impl;

import com.younike.common.utils.DateUtil;
import com.younike.common.utils.DateUtils;
import com.younike.system.domain.OrderGoods;
import com.younike.system.domain.OrderRecord;
import com.younike.system.domain.vo.OrderGoodsVo;
import com.younike.system.mapper.OrderGoodsMapper;
import com.younike.system.mapper.OrderRecordMapper;
import com.younike.system.service.IOrderGoodsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 订单商品Service业务层处理
 *
 * @author elev
 * @date 2022-01-06
 */
@Service
public class OrderGoodsServiceImpl implements IOrderGoodsService {
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Autowired
    private OrderRecordMapper orderRecordMapper;

    /**
     * 查询订单商品
     *
     * @param orderId 订单商品主键
     * @return 订单商品
     */
    @Override
    public OrderGoods selectOrderGoodsByOrderId(Long orderId) {
        return orderGoodsMapper.selectOrderGoodsByOrderId(orderId);
    }

    /**
     * 查询订单商品列表
     *
     * @param orderGoods 订单商品
     * @return 订单商品
     */
    @Override
    public List<OrderGoods> selectOrderGoodsList(OrderGoods orderGoods) {
        return orderGoodsMapper.selectOrderGoodsList(orderGoods);
    }

    /**
     * 新增订单商品
     *
     * @param orderGoods 订单商品
     * @return 结果
     */
    @Override
    public int insertOrderGoods(OrderGoods orderGoods) {
        orderGoods.setCreateTime(LocalDateTime.now());
        return orderGoodsMapper.insertOrderGoods(orderGoods);
    }

    /**
     * 修改订单商品
     *
     * @param orderGoods 订单商品
     * @return 结果
     */
    @Override
    public int updateOrderGoods(OrderGoods orderGoods) {
        orderGoods.setUpdateTime(LocalDateTime.now());
        return orderGoodsMapper.updateOrderGoods(orderGoods);
    }

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

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

    @Override
    public OrderGoodsVo getNowWeekDate(OrderGoodsVo orderGoodsVo) {
//        Date startTime = DateUtils.getMondayOfThisWeek();
//        Date endTime = DateUtils.getSundayOfThisWeek();
        Date endTime =new Date();
        Date startTime = DateUtil.addDateDays(new Date(), -7);
        orderGoodsVo.setStartTime(startTime);
        orderGoodsVo.setEndTime(endTime);
        OrderGoodsVo vo = new OrderGoodsVo();
        OrderGoodsVo nowWeekDate = orderGoodsMapper.getNowWeekDate(orderGoodsVo);
        OrderGoodsVo refundNowWeekDate = orderGoodsMapper.getRefundNowWeekDate(orderGoodsVo);

        vo.setTotalAmount(nowWeekDate.getTotalAmount().add(refundNowWeekDate.getTotalAmount()));
        vo.setOrderTimeSum(nowWeekDate.getOrderTimeSum().add(refundNowWeekDate.getOrderTimeSum()));
        vo.setOrderNum(nowWeekDate.getOrderNum().add(refundNowWeekDate.getOrderNum()));
        return vo;
    }

    @Override
    public List<OrderGoodsVo> getOrderList(OrderGoodsVo orderGoodsVo) {
        return orderGoodsMapper.getOrderList(orderGoodsVo);
    }

    @Override
    public OrderGoodsVo getOrderInfo(OrderGoodsVo orderGoodsVo) {
        List<OrderRecord> orderRecordList = orderRecordMapper.getOrderRecordList(orderGoodsVo);
        OrderGoodsVo orderInfo = orderGoodsMapper.getOrderInfo(orderGoodsVo);
        orderInfo.setOrderRecordList(orderRecordList);
        return orderInfo;
    }

    @Override
    public OrderGoodsVo getOrderNumDate(OrderGoodsVo orderGoodsVo) {

        OrderGoodsVo vo = new OrderGoodsVo();
        OrderGoodsVo nowWeekDate = orderGoodsMapper.getNowWeekDate(orderGoodsVo);
        OrderGoodsVo refundNowWeekDate = orderGoodsMapper.getRefundNowWeekDate(orderGoodsVo);

        vo.setTotalAmount(nowWeekDate.getTotalAmount().add(refundNowWeekDate.getTotalAmount()));
        vo.setOrderTimeSum(nowWeekDate.getOrderTimeSum().add(refundNowWeekDate.getOrderTimeSum()));
        vo.setOrderNum(nowWeekDate.getOrderNum().add(refundNowWeekDate.getOrderNum()));
        return vo;

//        return orderGoodsMapper.getNowWeekDate(orderGoodsVo);
    }

    /**
     * 查询商家某商品订单数
     *
     * @param goodsId    商品id
     * @param merchantId 商家id
     * @return
     */
    @Override
    public int getOrderCountByGoodsIdAndMerchantId(Long goodsId, Long merchantId) {
        return orderGoodsMapper.getOrderCountByGoodsIdAndMerchantId(goodsId, merchantId);
    }

    /**
     * 查询服务人员某商品订单数
     *
     * @param goodsId  商品id
     * @param serverId 服务人员id
     * @return
     */
    @Override
    public int getOrderCountByGoodsIdAndServerId(Long goodsId, Long serverId) {
        return orderGoodsMapper.getOrderCountByGoodsIdAndServerId(goodsId, serverId);
    }

    /**
     * （003，004，007，（006的部分退款）） 获取订单列表(包含部分退款的订单)
     * @param orderGoodsVo
     * @return
     */
    @Override
    public List<OrderGoodsVo> getRefundOrderList(OrderGoodsVo orderGoodsVo) {
        return orderGoodsMapper.getRefundOrderList(orderGoodsVo);
    }
}
