package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.dictionary.DicOrderType;
import com.lightinit.hsdataplatform.entity.OrderGoods;
import com.lightinit.hsdataplatform.entity.OrderGoodsExample;
import com.lightinit.hsdataplatform.mapper.OrderGoodsMapper;
import com.lightinit.hsdataplatform.mapper.OrderMapper;
import com.lightinit.hsdataplatform.model.admin.OrderDetailModel;
import com.lightinit.hsdataplatform.model.admin.OrderGoodDetailModel;
import com.lightinit.hsdataplatform.model.admin.OrderListModel;
import com.lightinit.hsdataplatform.model.admin.OrderSearchModel;
import com.lightinit.hsdataplatform.service.admin.IOrderService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/6/6 15:52
 * @Description:
 **/
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;
    @Override
    public List<OrderListModel> queryOrderList(OrderSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        if(DicOrderType.ALL.equals(inputModel.getOrderType())){
            inputModel.setOrderType(null);
        }
        return orderMapper.queryOrderList(inputModel);
    }

    @Override
    public long queryOrderCount(OrderSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        if(DicOrderType.ALL.equals(inputModel.getOrderType())){
            inputModel.setOrderType(null);
        }
        return orderMapper.queryOrderCount(inputModel);
    }

    @Override
    public OrderDetailModel queryOrderDetail(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        OrderDetailModel orderDetailModel = new OrderDetailModel();
        OrderSearchModel orderSearchModel = new OrderSearchModel();
        orderSearchModel.setId(id);
        List<OrderListModel> orderListModels = orderMapper.queryOrderList(orderSearchModel);
        OrderListModel orderListModel=null;     //用于存放实体
        if(orderListModels !=null && orderListModels.size()>0){
            orderListModel = new OrderListModel();
            BeanUtils.copyProperties(orderListModels.get(0),orderListModel);
        }
        orderDetailModel.setOrderListModel(orderListModel);
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
        List<OrderGoodDetailModel> list=new ArrayList<>();
        OrderGoodDetailModel orderGoodDetailModel=null;
        if(orderGoods !=null && orderGoods.size()>0){
            for (OrderGoods orderGood : orderGoods) {
                orderGoodDetailModel=new OrderGoodDetailModel();
                BeanUtils.copyProperties(orderGood,orderGoodDetailModel);
                list.add(orderGoodDetailModel);
            }
        }
        orderDetailModel.setOrderGoodDetailModelList(list);
        return orderDetailModel;
    }
}
