package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.common.OrderNumberUtils;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.entity.Order;
import com.lightinit.hsdataportal.entity.OrderGoods;
import com.lightinit.hsdataportal.entity.OrderGoodsExample;
import com.lightinit.hsdataportal.mapper.OrderGoodsMapper;
import com.lightinit.hsdataportal.mapper.OrderMapper;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IOrderService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements IOrderService{

    @Autowired
    private OrderMapper orderMapper ;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper ;


    @Override
    public List<OrderListModel> queryOpenList(OrderSearchModel orderSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        List<OrderListModel> orderListModels = orderMapper.queryMyOrder(orderSearchModel) ;
        for (OrderListModel orderListModel : orderListModels) {
            OrderGoodsExample orderGoodsExample = new OrderGoodsExample() ;
            OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria() ;
            criteria.andOrderIdEqualTo(orderListModel.getOrderId()) ;
            List<OrderGoods> orderGoodsList = orderGoodsMapper.selectByExample(orderGoodsExample) ;
            //lamda表达式给值
            List<OrderGoodsModel> orderGoodsModels = orderGoodsList.stream().map(orderGoods->{
                OrderGoodsModel orderGoodsModel = new OrderGoodsModel() ;
                BeanUtils.copyProperties(orderGoods,orderGoodsModel);
                BeanUtils.copyProperties(orderGoods,orderListModel);
                return orderGoodsModel;
            }).collect(Collectors.toList());
            orderListModel.setOrderGoodsModels(orderGoodsModels);
        }
        return orderListModels;
    }

    @Override
    public long queryOpenCount(OrderSearchModel orderSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return orderMapper.queryMyOrderCount(orderSearchModel);
    }

    @Override
    @Transactional
    public ResultState deleteOrder(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState<Long> resultState = new ResultState<Long>() ;
        //删除order_goods
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample() ;
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria() ;
        criteria.andOrderIdEqualTo(id) ;
        int result =  orderGoodsMapper.deleteByExample(orderGoodsExample) ;

        //删除order
        int result1 = orderMapper.deleteByPrimaryKey(id) ;
        if (!(result>0&&result1>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("取消订单失败");
        }
        return resultState;
    }

    @Override
    public ResultState generateOrder(Long id,Order order) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ResultState resultState = new ResultState();
        Order order1 = new Order();
        order1.setOrderNumber(OrderNumberUtils.generate());
        order1.setUserId(id);
        order1.setPlatformType(DicPlatformType.OPEN);
        order1.setBusinessType("账户充值");
        order1.setOrderAmount(order.getOrderAmount());
        order1.setOrderTime(new Date());

        int result = orderMapper.insert(order1);
        if(!(result>0)) {
            resultState.setMsg("生成订单失败");
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("orderAmount",order1.getOrderAmount());
        map.put("orderNumber",order1.getOrderNumber());
        resultState.setData(map);
        return resultState;
    }
}
