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

    @Autowired
    private SizesService sizesService;

    @Autowired
    private Designprice14Service designprice14Service;

    @RequestMapping(value = "/admin/submit-discount06.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]));
        sizesService.updateNotNull(sizes);
        return "ok";
    }

    @RequestMapping(value = "calculate06.html",method = RequestMethod.POST)//计算价格
    @ResponseBody
    public Map<String,Double> calculate(Integer pid, Integer num, Integer desnum, String size, String name,String prints) {
        //计算设计费，先得到设计价格
        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,name,prints,null,pid);
        Double price01 = sizes.getPrice();
        Double price0 ;
        if(num == 100){price0=sizes.getNum1();
        }else if(num == 250){price0=sizes.getNum2();
        }else if(num == 500){price0=sizes.getNum3();
        }else if(num == 1000){price0=sizes.getNum4();
        }else {price0=sizes.getNum5();}
        Double price00 = price01*(1-price0);

		/*BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP)           bg.doubleValue();*///保留两位小数
        double price = new BigDecimal(price00).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;
    }


}
