package com.duowenlvshi.duowenlszp.model.Order.server;

import com.duowenlvshi.duowenlszp.model.Order.entity.Order;
import com.duowenlvshi.duowenlszp.model.Meal.entity.Meal;
import com.duowenlvshi.duowenlszp.model.Meal.server.MealServer;
import com.duowenlvshi.duowenlszp.model.Order.dao.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yang fang yu
 * @since 2019/9/9 20:34
 */
@Service
public class OrderServer {
    @Autowired(required = false)
    OrderMapper orderMapper;
    @Autowired
    MealServer mealServer;


    public Map insert (Meal meal, Map<String,Object> map){
        String dd = mealServer.selectDiscount();
        double discount = Double.parseDouble(dd);//折扣率
        Order order = new Order();
        Date d=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
        String s = format.format(d);
        int x=(int)((Math.random()*9+1)*100000);
        String id = s+x;
        Order o = orderMapper.selectByPrimaryKey(id);
        while(o !=null){
             d=new Date();
             format=new SimpleDateFormat("yyyyMMddHHmmss");
             s = format.format(d);
             x=(int)(Math.random()*100000);
             id = s+x;
             o = orderMapper.selectByPrimaryKey(id);
        }
        order.setId(id);
        order.setMealId(map.get("mealId")+"");
        order.setLawyerId(map.get("lawyerId")+"");
        order.setCity(map.get("city")+"");
        order.setPaytype(map.get("payType")+"");
        order.setOrderType(map.get("orderType")+"");
        order.setPrice(meal.getPrice()*discount);//原价乘折扣率
        order.setRemake(map.get("remake")+"");
        order.setOrderstate("0");
        order.setPaystate("0");
        order.setMealname(meal.getMealname());
        order.setPostsize(meal.getPostsize());
        order.setPushsize(meal.getPushsize());
        order.setCandidatesize(meal.getCandidatesize());
        order.setDuration(meal.getDuration());
        order.setOrderstate("2");
        if(map.get("endTime")!=null){//传入套餐有效期 是免费的 设置订单状态为结束
            order.setEndTime((Date) map.get("endTime"));
            order.setOrderstate("1");

            Map map2 = new HashMap();
            int num = mealServer.selectPurchaseNuml(map.get("mealId")+"");
            ++num;
            map2.put("num",num);
            map2.put("id",map.get("mealId"));
            mealServer.updatePurchaseNuml(map2);
        }
        order.setCreatedate(new Date());
        order.setIsValid("1");
        int i = orderMapper.insertSelective(order);
        Map map1 =new HashMap();
        map1.put("i",i);
        map1.put("id",order.getId());


        return map1;
    }
    public Order selectByPrimaryKey(String id){
        return orderMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改订单参数
     * */
    public int updateByPrimaryKeySelective(Order Order){

        return orderMapper.updateByPrimaryKeySelective(Order);
    }
    /**
     *   查询用户购买套餐详情
     * */
    public List<Map> selectMeal(String id){
        return orderMapper.selectMeal(id);
    }
}
