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;

/**
 * @program: gongshi
 * @description:
 * @author: hgueng
 * @create: 2019-12-11 13:49
 **/

@Controller
public class Product09Controller {

    @Autowired
    private SizesService sizesService;

    @Autowired
    private Designprice14Service designprice14Service;

    @Autowired
    private ProductService productService;

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

        //1.0 先去数据库查找对应的单价
        Sizes size01 = sizesService.findSizeByParam(size,name,null,null,pid);
        //2.0 没有优惠前的单价
        double sumPrice = size01.getPrice();
        //3.0 计算折扣比
        Product p = productService.selectByKey(pid);
        int[] array = {p.getNum1(),p.getNum2(),p.getNum3(),p.getNum4(),p.getNum5(),p.getNum6()};
        double[] arr = {size01.getNum1(),size01.getNum2(),size01.getNum3(),size01.getNum4(),size01.getNum5(),size01.getNum6()};
        double disPrice = 1.0-UtilController.zhekou(arr,num,array);//单价的折扣比例

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


}
