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

import com.duowenlvshi.duowenlszp.app.util.AllPay;
import com.duowenlvshi.duowenlszp.model.Meal.entity.Meal;
import com.duowenlvshi.duowenlszp.model.Meal.server.MealServer;
import com.duowenlvshi.duowenlszp.model.Order.server.OrderServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yang fang yu
 * @since 2019/9/9 20:33
 */
@Transactional
@Component
public class OrderController {
    @Autowired
    AllPay allPay;
    @Autowired
    MealServer mealServer;
    @Autowired
    OrderServer orderServer;
    /**
     * 创建订单(废弃)
     * */
    public Map insertorder1(Map<String,Object> map){
        Map<String,Object> map1 = new HashMap();
        String mealId      =(String) map.get("mealId");
        String lawyerId    =(String) map.get("lawyerId");
        String city        =(String) map.get("city");
        String payType     =(String) map.get("payType");
        map1.put("lawyerId",lawyerId);
        map1.put("mealId",mealId);
        map1.put("city",city);
        map1.put("payType",payType);
        map1.put("orderType","1");
        map1.put("remake","");
        Meal meal = mealServer.selectMeal(mealId);
        Map map2 =orderServer.insert(meal,map1);
        Map alpayMap = new HashMap();
        alpayMap.put("body","招聘套餐");//设置标题
        alpayMap.put("id",map2.get("id"));//设置订单id
        alpayMap.put("Price",meal.getPrice());//设置金额支付宝金额
        alpayMap.put("payType",payType);//设置支付方式
        return allPay.allpay(alpayMap);
    }
    /**
     * 创建免费订单(废弃)
     * */
    public int insertorderFree(Map<String,Object> map){
        int j;
        String mealId = (String) map.get("mealId");
        int i =mealServer.selectPurchaseNuml(mealId);
        if(i>100){
            return -2;
        }else{
            Map<String,Object> map1 = new HashMap();
            String lawyerId    =(String) map.get("lawyerId");
            String city        =(String) map.get("city");
            Meal meal = mealServer.selectMeal(mealId);
            map1.put("lawyerId",lawyerId);
            map1.put("mealId",mealId);
            map1.put("city",city);
            map1.put("payType","");
            map1.put("orderType","2");
            map1.put("remake","免费");
            int duration = meal.getDuration();//获取套餐时长
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(new Date());
            rightNow.add(Calendar.MONTH, duration);
            Date dt1 = rightNow.getTime();
            map1.put("endTime",dt1);//下单时间加上套餐时长
            meal.setPrice(0.0);
            Map map2 =orderServer.insert(meal,map1);
            j=(int)map2.get("i");
        }
        return j;
    }

    /**
     * 创建订单
     * */
    public Map insertOrder(Map<String,Object> map){
        Map<String,Object> map1 = new HashMap();
        String mealId      =(String) map.get("mealId");
        String free      =(String) map.get("free");//free为适配662之前的接口1为免费
        int version = Integer.parseInt(map.get("version")+"");
        String lawyerId    =(String) map.get("lawyerId");
        String city        =(String) map.get("city");
        map1.put("lawyerId",lawyerId);
        map1.put("mealId",mealId);
        map1.put("city",city);
        Meal meal = mealServer.selectMeal(mealId);
        if("1".equals(meal.getMealType())){
            if( version >661 || "1".equals(free)){
                int j;
                Map map3 = new HashMap();
                map1.put("payType","");
                map1.put("orderType","2");
                map1.put("remake","免费");
                int duration = meal.getDuration();//获取套餐时长
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(new Date());
                rightNow.add(Calendar.MONTH, duration);
                Date dt1 = rightNow.getTime();
                map1.put("endTime",dt1);//下单时间加上套餐时长
                meal.setPrice(0.0);
                Map map2 =orderServer.insert(meal,map1);
                j=(int)map2.get("i");
                map3.put("num",j);
                return map3;
            }else{
                Map map3 = new HashMap();
                map3.put("num",-2);
                return map3;
            }
        }else if("2".equals(meal.getMealType())){
            String payType     =(String) map.get("payType");
            map1.put("payType",payType);
            map1.put("orderType","1");
            map1.put("remake","");
            Map map2 =orderServer.insert(meal,map1);
            Map alpayMap = new HashMap();
            alpayMap.put("body","招聘套餐");//设置标题
            alpayMap.put("id",map2.get("id"));//设置订单id
            alpayMap.put("Price",meal.getPrice());//设置金额支付宝金额
            alpayMap.put("payType",payType);//设置支付方式
            return allPay.allpay(alpayMap);
        }
        return null;
    }


    /**
     * 查询用户购买套餐情况
     * */
    public List<Map> selectMeal(Map<String,Object> map){
        String id = (String) map.get("token");
        List<Map> list = orderServer.selectMeal(id);
        if(list.size()>0){
            for (Map map1 : list) {
                long l =new Date().getTime();
                Date d = (Date)map1.get("endTime");
                long ll =d.getTime();
                if(l>ll){
                    map1.put("endTime","已过期");
                    map1.put("code",false);
                }else{
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String dateString = formatter.format(d);
                    map1.put("endTime","有效期至:"+dateString);
                    map1.put("code",true);
                }

            }
        }
        return list;
    }
}
