package com.yc.education.controller.admin;

import com.yc.education.model.Product;
import com.yc.education.model.Sizes;
import com.yc.education.service.Designprice14Service;
import com.yc.education.service.ProductService;
import com.yc.education.service.SizesService;
import com.yc.education.util.ProductDesign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;


@Controller
public class Product18Controller {
	
	@Autowired
	private Designprice14Service designprice14Service;

	@Autowired
	private SizesService sizesService;
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value = "/admin/submit-size18.html",method = RequestMethod.POST)
	@ResponseBody
	public String submitSize(Integer pid,String prices) {
		String[] str = prices.split(",");
		Sizes sizes = new Sizes();
		sizes.setId(pid);
		sizes.setPrice(Double.valueOf(str[0]));
		sizes.setNum1(Double.valueOf(str[1]));
		sizes.setNum2(Double.valueOf(str[2]));
		sizes.setNum3(Double.valueOf(str[3]));
		sizes.setNum4(Double.valueOf(str[4]));
		sizes.setNum5(Double.valueOf(str[5]));
		sizes.setNum6(Double.valueOf(str[6]));
		sizesService.updateNotNull(sizes);
		return "ok";
	}
	
	@RequestMapping(value = "/admin/submit-count18.html",method = RequestMethod.POST)
	@ResponseBody
	public String submitCount(Integer pid,String prices) {
		String[] str = prices.split(",");
		Product product = new Product();
		product.setId(pid);
		product.setNum1(Integer.valueOf(str[0]));
		product.setNum2(Integer.valueOf(str[1]));
		product.setNum3(Integer.valueOf(str[2]));
		product.setNum4(Integer.valueOf(str[3]));
		product.setNum5(Integer.valueOf(str[4]));
		product.setNum6(Integer.valueOf(str[5]));
		productService.updateNotNull(product);
		return "ok";
	}
	
	@RequestMapping(value = "calculate18.html",method = RequestMethod.POST)//计算价格
	@ResponseBody
	public Map<String,Double> calculate(Integer pid,Integer num,Integer desnum,String size,String color) {
		//计算设计费，先得到设计价格
		double desPrice = designprice14Service.selectByKey(1).getDesiprice();
		double desSumPrice = 0.0;
		if(desnum==null) {
			desSumPrice = 0.00;
		}else {
			desSumPrice = new BigDecimal(ProductDesign.add(desPrice,desnum)).setScale(2, RoundingMode.UP).doubleValue();//得到总的设计价格
		}
		//通过尺寸得到出售价格
		Sizes sizes = sizesService.findSizeByParam(size,null,null,null,pid);
		double sizePrice = sizes.getPrice();
		//没有优惠前的单价
		double sumPrice = sizePrice;
		//计算折扣比,先得到商家折扣比例
		double[] arr = {sizes.getNum1(),sizes.getNum2(),sizes.getNum3(),sizes.getNum4(),sizes.getNum5(),sizes.getNum6()};
		Product p = productService.selectByKey(pid);
		int[] array = {p.getNum1(),p.getNum2(),p.getNum3(),p.getNum4(),p.getNum5(),p.getNum6()};
		double disPrice = 1.0-zhekou(arr,num,array);//单价的折扣比例
		double price = new BigDecimal(sumPrice*disPrice).setScale(2, RoundingMode.UP).doubleValue();//打完折扣的单价
		double shuiPrice = new BigDecimal((num*price+desSumPrice)*0.1).setScale(2, RoundingMode.UP).doubleValue();
		double sumAllPrice = new BigDecimal(num*price+desSumPrice+shuiPrice).setScale(2, RoundingMode.UP).doubleValue();
		Map<String,Double> map = new HashMap<String, Double>();
		double subtotal = new BigDecimal(price*num+desSumPrice).setScale(2, RoundingMode.UP).doubleValue();//小计
		map.put("subtotal", subtotal);
		map.put("price", price);
		map.put("desSumPrice", desSumPrice);
		map.put("shuiPrice", shuiPrice);
		map.put("sumAllPrice", sumAllPrice);
		return map;
	}
		
		//折扣价
		public static double zhekou(double[] arr,int num,int[] array) {
			if(num < array[0]) {
				return 0;
			}
			double a = 0;
			if(num>=array[0]&&num<=array[1]) {
				a=arr[0];
				for(int i=array[1];i<num;i++) {
					a+=0.016;
				}
			}else if(num>array[1]&&num<=array[2]) {
				a=arr[1];
			}else if(num>array[2]&&num<=array[3]) {
				a=arr[2];
			}else if(num>array[3]&&num<=array[4]) {
				a=arr[3];
			}else if(num>array[4]&&num<=array[5]) {
				a=arr[4];
			}else  {
				a=arr[5];
			}
			num--;
			return a+zhekou(arr,num,array);
		}		



}
