package com.platform.service.impl;

import com.platform.dao.OrderDao;
import com.platform.dao.ShippingDao;
import com.platform.entity.OrderEntity;
import com.platform.entity.OrderShippingEntity;
import com.platform.entity.ShippingEntity;
import com.platform.service.OrderService;
import com.platform.service.OrderShippingService;

import com.platform.utils.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("orderService")
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ShippingDao shippingDao;
    @Autowired
    private OrderShippingService orderShippingService;

    @Override
    public OrderEntity queryObject(Integer id) {
        return orderDao.queryObject(id);
    }


    @Override
    public long getDealOrderNum(String day) {
        return orderDao.getDealCount(day);
    }

    @Override
    public List<OrderEntity> queryList(Map<String, Object> map) {
        return orderDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return orderDao.queryTotal(map);
    }

    @Override
    public int save(OrderEntity order) {
        return orderDao.save(order);
    }

    @Override
    public int update(OrderEntity order) {
        return orderDao.update(order);
    }

    @Override
    public int delete(Integer id) {
        return orderDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return orderDao.deleteBatch(ids);
    }

    @Override
    public int confirm(Integer id, Integer brandId) {
        OrderEntity orderEntity = queryObject(id);
        Integer payStatus = orderEntity.getPayStatus();//付款状态
        if (2 != payStatus) {
            throw new RRException("此订单未付款，不能确认收货！");
        }
        Map map = new HashMap();
        map.put("orderId", id);
        map.put("brandId", brandId + "");
        List<OrderShippingEntity> list = orderShippingService.queryList(map);
        if(list != null && list.size() > 0){
            OrderShippingEntity ordershippingEntity = list.get(0);
            Integer shippingStatus = ordershippingEntity.getShippingStatus();//发货状态
            if (4 == shippingStatus) {
                throw new RRException("此订单处于退货状态，不能确认收货！");
            }
            if (0 == shippingStatus) {
                throw new RRException("此订单未发货，不能确认收货！");
            }
            ordershippingEntity.setShippingStatus(2);
            orderShippingService.update(ordershippingEntity);

            map = new HashMap();
            map.put("orderId", id);
            list = orderShippingService.queryList(map);
            int statusNum = 0;
            for(OrderShippingEntity shippingEntity : list){
                if(shippingEntity.getShippingStatus() == 2){
                    statusNum++;
                }
            }
            if(statusNum == list.size()){
                orderEntity.setOrderStatus(501);
                orderDao.update(orderEntity);
            }
            return 1;
        }

        throw new RRException("发货信息不存在");
    }

    @Override
    public int sendGoods(OrderEntity order) {

        if(null == order){
            log.error("入参为空，无法执行发货逻辑");
            return -1;
        }
        Integer payStatus = order.getPayStatus();//付款状态
        if (2 != payStatus) {
            throw new RRException("此订单未付款！");
        }
        return updateDataAfterSendGoods(order);
    }

    @Transactional(rollbackFor = Exception.class)
    public int updateDataAfterSendGoods(OrderEntity order){
        ShippingEntity shippingEntity = shippingDao.queryObject(order.getShippingId());
        if (null != shippingEntity) {
            order.setShippingName(shippingEntity.getName());
        }
        Map<String , Object> map = new HashMap<String , Object>();
        map.put("orderId", order.getId());
        map.put("brandId", order.getBrandId() + "");
        List<OrderShippingEntity> list = orderShippingService.queryList(map);
        if(list != null && list.size() > 0){
            OrderShippingEntity ordershippingEntity = list.get(0);
            ordershippingEntity.setShippingId(order.getShippingId());
            ordershippingEntity.setShippingName(shippingEntity.getName());
            ordershippingEntity.setShippingNo(order.getShippingNo());
            ordershippingEntity.setShippingStatus(1);//已发货
            orderShippingService.update(ordershippingEntity);
        }
        order.setOrderStatus(300);//订单已发货
        order.setShippingStatus(1);//已发货
        return orderDao.update(order);
    }
}
