package com.huimv.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.entity.ProdAnalysis;
import com.huimv.admin.entity.ProdSale;
import com.huimv.admin.entity.vo.ProdAnalysisVo;
import com.huimv.admin.service.IProdAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 重要指标 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-02-22
 */
@RestController
@RequestMapping("/prod-analysis")
@CrossOrigin
public class ProdAnalysisController {

    @Autowired
    private IProdAnalysisService prodAnalysisService;

    @RequestMapping("/list")
    public Result list(HttpServletRequest httpServletRequest, @RequestBody Map<String,String> paramsMap) {
        return prodAnalysisService.list(httpServletRequest,paramsMap);
    }

    @RequestMapping("/edit")
    public Result edit(HttpServletRequest httpServletRequest, @RequestBody ProdAnalysis prodAnalysis) {
        return prodAnalysisService.edit(httpServletRequest,prodAnalysis);
    }

    @RequestMapping("/add")
    public Result add(HttpServletRequest httpServletRequest, @RequestBody ProdAnalysisVo prodAnalysis) {
        String farmId = prodAnalysis.getFarmId();
        ProdAnalysis feed = prodAnalysis.getFeed();
        ProdAnalysis drug = prodAnalysis.getDrug();
        ProdAnalysis share = prodAnalysis.getShare();
        double feedCost = Double.parseDouble(feed.getZcb());
        double drugCost = Double.parseDouble(drug.getZcb());
        double shareCost = Double.parseDouble(share.getZcb());

        double feedRate = 0;
        double drugRate = 0;
        double shareRate = 0;

        double totalCost = feedCost + drugCost + shareCost;

        if (totalCost != 0.0) {
            // 计算每个比例（乘以100后四舍五入到整数）
            feedRate = Math.round((feedCost / totalCost) * 100);
            drugRate = Math.round((drugCost / totalCost) * 100);
            shareRate = Math.round((shareCost / totalCost) * 100);

            // 确保比例的总和为100
            double sum = feedRate + drugRate + shareRate;

            if (sum != 100.0) {
                // 如果总和不为100，调整比例以使其总和等于100
                double diff = 100.0 - sum;

                // 将差异添加到小数部分最大的比例
                if (feedRate % 1 > drugRate % 1 && feedRate % 1 > shareRate % 1) {
                    feedRate += diff;
                } else if (drugRate % 1 > feedRate % 1 && drugRate % 1 > shareRate % 1) {
                    drugRate += diff;
                } else {
                    shareRate += diff;
                }
            }
        }


        feed.setCreatTime(new Date());
        feed.setFarmId(Integer.valueOf(farmId));
        feed.setFeedCost(String.valueOf(feedRate));
        feed.setShareCost(String.valueOf(shareRate));
        feed.setVaccineCost(String.valueOf(drugRate));

        drug.setFarmId(Integer.valueOf(farmId));
        drug.setCreatTime(new Date());
        drug.setFeedCost(String.valueOf(feedRate));
        drug.setShareCost(String.valueOf(shareRate));
        drug.setVaccineCost(String.valueOf(drugRate));

        share.setFeedCost(String.valueOf(feedRate));
        share.setShareCost(String.valueOf(shareRate));
        share.setVaccineCost(String.valueOf(drugRate));
        share.setFarmId(Integer.valueOf(farmId));
        share.setCreatTime(new Date());
        List<ProdAnalysis> prodAnalyses = new ArrayList<>();
        prodAnalyses.add(feed);
        prodAnalyses.add(drug);
        prodAnalyses.add(share);
        prodAnalysisService.remove(new QueryWrapper<ProdAnalysis>().eq("display_type",1).eq("farm_id",farmId));
        prodAnalysisService.saveBatch(prodAnalyses);
        return Result.SUCCESS();
    }

}
