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 Product04Controller {

	@Autowired
	private SizesService sizesService;
	
	@Autowired
	private Designprice14Service designprice14Service;
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping(value = "/admin/submit-discount04.html",method = RequestMethod.POST)
	@ResponseBody
	public String submitDiscount(Integer pid,String prices) {//第三行递减比率
		String[] str = prices.split(",");
		Sizes sizes = new Sizes();
		sizes.setId(pid);
		sizes.setPrice(Double.valueOf(str[0]));
		if(Double.valueOf(str[1])==999999){
			sizes.setOrders(null);
			sizes.setOrderss(null);
		}else{
			sizes.setOrders(Double.valueOf(str[1]));
			sizes.setOrderss(Double.valueOf(str[2]));
		}
		sizes.setNum1(Double.valueOf(str[3]));
		sizes.setNum2(Double.valueOf(str[4]));
		sizes.setNum3(Double.valueOf(str[5]));
		sizes.setNum4(Double.valueOf(str[6]));
		sizes.setNum5(Double.valueOf(str[7]));
		sizes.setNum6(Double.valueOf(str[8]));
		sizesService.updateNotNull(sizes);
		return "ok";
	}
	
	@RequestMapping(value = "/admin/submit-count04.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.setNum5(Integer.valueOf(str[4]));
		product.setNum4(Integer.valueOf(str[3]));
		product.setNum6(Integer.valueOf(str[5]));
		productService.updateNotNull(product);
		return "ok";
	}

	@RequestMapping(value = "calculate04.html",method = RequestMethod.POST)//计算价格
	@ResponseBody
	public Map<String,Double> calculate(Integer pid,Integer num,Integer desnum,String name01,String name02,String size,String prints,String orgins) {
		//计算设计费，先得到设计价格
		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();//得到总的设计价格
		}

		Product p = productService.selectByKey(pid);
		int[] array = {p.getNum1(),p.getNum2(),p.getNum3(),p.getNum4(),p.getNum5(),p.getNum6()};

		double sumPrice = 0.0;

		if(size.equals("Custom Size")){
			//拿到宽度的
			Sizes size041 = sizesService.findSizeByParam(size,"Width(mm)",prints,null,pid);
			//1.0 先得到宽度的价格
			int name01s = Integer.valueOf(name01)-210;
			double price01 = size041.getPrice()+name01(name01s,size041.getOrders(),size041.getOrderss());
			double[] arr01 = {size041.getNum1(),size041.getNum2(),size041.getNum3(),size041.getNum4(),size041.getNum5(),size041.getNum6()};
			double disPrice01 = 1.0-UtilController.zhekou(arr01,num,array);//单价的折扣比例

			//拿到高度的
			Sizes size042 = sizesService.findSizeByParam(size,"Height(mm)",prints,null,pid);
			//2.0 在得到高度的价格
			int name02s = Integer.valueOf(name02)-297;
			double price02 = size042.getPrice()+name01(name02s,size042.getOrders(),size042.getOrderss());
			double[] arr02 = {size042.getNum1(),size042.getNum2(),size042.getNum3(),size042.getNum4(),size042.getNum5(),size042.getNum6()};
			double disPrice02 = 1.0-UtilController.zhekou(arr02,num,array);//单价的折扣比例

			sumPrice = price01*disPrice01+price02*disPrice02;

		}else{
			//
			Sizes size043 = sizesService.findSizeByParam(size,null,prints,null,pid);
			double price02 = size043.getPrice();
			double[] arr02 = {size043.getNum1(),size043.getNum2(),size043.getNum3(),size043.getNum4(),size043.getNum5(),size043.getNum6()};
			double disPrice02 = 1.0-UtilController.zhekou(arr02,num,array);//单价的折扣比例
			sumPrice = price02*disPrice02;
		}


		/*BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP)           bg.doubleValue();*///保留两位小数
		double price = new BigDecimal(sumPrice).setScale(2, RoundingMode.UP).doubleValue();//打完折扣的单价
		double subtotal = new BigDecimal(price*num+desSumPrice).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>();
		map.put("price", price);
		map.put("subtotal", subtotal);
		map.put("desSumPrice", desSumPrice);
		map.put("shuiPrice", shuiPrice);
		map.put("sumAllPrice", sumAllPrice);
		return map;
	}


	/*private static double name01(int name01s,double price01,double orderss) {
		if(name01s <= 0) {
			return 0;
		}
		double b = price01*(1-orderss*name01s);
		name01s--;
		return b+name01(name01s,price01,orderss);
	}*/

	private static double name01(int name01s,double price01,double orderss){
		if(name01s <= 0) {
			return 0;
		}else{
			double b = 0.0;
			for(int i = 1; i< name01s ;i++){
				b += price01*(1-orderss*name01s);
			}
			return b;
		}

	}





}
