package com.younike.system.service.impl;

import com.younike.common.exception.ServiceException;
import com.younike.common.utils.DateUtils;
import com.younike.common.utils.bean.BeanUtils;
import com.younike.system.domain.*;
import com.younike.system.domain.vo.OrderGoodsVo;
import com.younike.system.mapper.OrderGoodsMapper;
import com.younike.system.mapper.OrderRecordMapper;
import com.younike.system.service.IGoodsService;
import com.younike.system.service.IOrderGoodsServerService;
import com.younike.system.service.IOrderGoodsService;
import com.younike.system.service.IServerUserService;
import org.apache.commons.lang3.ObjectUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
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;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IServerUserService serverUserService;
    @Autowired
    private IOrderGoodsServerService orderGoodsServerService;

    /**
     * 查询订单商品
     * 
     * @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();
        orderGoodsVo.setStartTime(startTime);
        orderGoodsVo.setEndTime(endTime);
        return orderGoodsMapper.getNowWeekDate(orderGoodsVo);
    }

    @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) {
        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);
    }

    @Override
    public void saveOrderGoodsServerInfo(Long orderId, Long goodsId, Long serverId, BigDecimal price, Integer goodsCount) {
        Goods goods = goodsService.selectGoodsByGoodsId(goodsId);
        ServerUser serverUser = serverUserService.selectServerUserByServerId(serverId);

        OrderGoods orderGoods = new OrderGoods();
        BeanUtils.copyProperties(goods, orderGoods);
        orderGoods.setOrderId(orderId);
        orderGoods.setServerId(serverId);
        orderGoods.setGoodsCounts(goodsCount);
        orderGoods.setPrice(price);
        orderGoods.setCreateTime(LocalDateTime.now());
        orderGoods.setUpdateTime(LocalDateTime.now());
        if (orderGoodsMapper.insertOrderGoods(orderGoods) < 1) {
            throw new ServiceException("添加订单商品失败");
        }

        OrderGoodsServer orderGoodsServer = new OrderGoodsServer();
        orderGoodsServer.setOrderId(orderId);
        orderGoodsServer.setGoodsId(goodsId);
        orderGoodsServer.setServerId(serverId);
        orderGoodsServer.setGoodsCounts(goodsCount);
        orderGoodsServer.setSingleTime(goods.getSingleTime());
        orderGoodsServer.setGoodsCoin(goods.getCoin().longValue());
        orderGoodsServer.setGoodsName(goods.getName());
        orderGoodsServer.setGoodsServerPrice(price);
        orderGoodsServer.setGoodsUnit(goods.getGoodsUnit());
        orderGoodsServer.setServerName(serverUser.getRealName());
        orderGoodsServer.setServerSex(serverUser.getSex());
        orderGoodsServer.setServerAge(serverUser.getAge());
        orderGoodsServer.setServerAvatar(serverUser.getAvatar());
        orderGoodsServer.setCreateTime(LocalDateTime.now());
        orderGoodsServer.setUpdateTime(LocalDateTime.now());
        if (orderGoodsServerService.insertOrderGoodsServer(orderGoodsServer) < 1) {
            throw new ServiceException("添加订单服务人员关联数据失败");
        }
    }
}
