package com.ruoyi.project.production.controller;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.production.controller.vo.QualityAnalysisVO;
import com.ruoyi.project.production.domain.TMonthlyQualityReportS2007;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.production.service.ITMonthlyQualityReportS2007Service;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 质量月报S2007Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s2007")
public class TMonthlyQualityReportS2007Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS2007Service tMonthlyQualityReportS2007Service;

    /**
     * 查询质量月报S2007列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS2007 tMonthlyQualityReportS2007)
    {
        Date endDate = tMonthlyQualityReportS2007.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS2007.setEndDate(endDate);
        List<TMonthlyQualityReportS2007> tMonthlyQualityReportS2007s = tMonthlyQualityReportS2007Service.selectTMonthlyQualityReportS2007List(tMonthlyQualityReportS2007);
        if (tMonthlyQualityReportS2007s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS2007 firstValue = tMonthlyQualityReportS2007s.get(0);
        String firstProductionString = firstValue.getProduction();
        if (firstProductionString != null) {
            if (firstProductionString.indexOf("/") != -1) {
                firstProductionString = firstProductionString.substring(1, firstProductionString.indexOf("/"));
            }
        }
        // 最大值
        BigDecimal viscosityMax = new BigDecimal(firstValue.getViscosity() == null ? "0" : firstValue.getViscosity());
        BigDecimal endPointMax = new BigDecimal(firstValue.getEndPoint() == null ? "0" : firstValue.getEndPoint());
        BigDecimal pressureDifferenceMax = new BigDecimal(firstValue.getPressureDifference() == null ? "0" : firstValue.getPressureDifference());
        BigDecimal productionMax = new BigDecimal(firstProductionString == null ? "0" : firstProductionString);
        // 最小值
        BigDecimal viscosityMin = new BigDecimal(firstValue.getViscosity() == null ? "0" : firstValue.getViscosity());
        BigDecimal endPointMin = new BigDecimal(firstValue.getEndPoint() == null ? "0" : firstValue.getEndPoint());
        BigDecimal pressureDifferenceMin = new BigDecimal(firstValue.getPressureDifference() == null ? "0" : firstValue.getPressureDifference());
        BigDecimal productionMin = new BigDecimal(firstProductionString == null ? "0" : firstProductionString);
        // 平均值
        BigDecimal viscosityAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal endPointAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal pressureDifferenceAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal productionAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal viscositySum = new BigDecimal(BigInteger.ZERO);
        BigDecimal endPointSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal pressureDifferenceSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal productionSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int viscosityCount = 0;
        int endPointCount = 0;
        int pressureDifferenceCount = 0;
        int productionCount = 0;
        for (TMonthlyQualityReportS2007 monthlyQualityReportS2007 : tMonthlyQualityReportS2007s) {
            String viscosity1 = monthlyQualityReportS2007.getViscosity();
            if (viscosity1 != null) {
                BigDecimal viscosity = new BigDecimal(viscosity1);
                if (viscosity.compareTo(viscosityMax) > 0) { viscosityMax = viscosity; }
                if (viscosity.compareTo(viscosityMin) < 0) { viscosityMin = viscosity; }
                viscositySum = viscositySum.add(viscosity);
                viscosityCount++;
            }
            String endPoint1 = monthlyQualityReportS2007.getEndPoint();
            if (endPoint1 != null) {
                BigDecimal endPoint = new BigDecimal(endPoint1);
                if (endPoint.compareTo(endPointMax) > 0) { endPointMax = endPoint; }
                if (endPoint.compareTo(endPointMin) < 0) { endPointMin = endPoint; }
                endPointSum = endPointSum.add(endPoint);
                endPointCount++;
            }
            String pressureDifference1 = monthlyQualityReportS2007.getPressureDifference();
            if (pressureDifference1 != null) {
                BigDecimal pressureDifference = new BigDecimal(pressureDifference1);
                if (pressureDifference.compareTo(pressureDifferenceMax) > 0) { pressureDifferenceMax = pressureDifference; }
                if (pressureDifference.compareTo(pressureDifferenceMin) < 0) { pressureDifferenceMin = pressureDifference; }
                pressureDifferenceSum = pressureDifferenceSum.add(pressureDifference);
                pressureDifferenceCount++;
            }
            String productionString = monthlyQualityReportS2007.getProduction();
            if (productionString != null) {
                if (productionString.indexOf("/") != -1) {
                    productionString = productionString.substring(1, productionString.indexOf("/"));
                }
                BigDecimal production = new BigDecimal(productionString);
                if (production.compareTo(productionMax) > 0) { productionMax = production; }
                if (production.compareTo(productionMin) < 0) { productionMin = production; }
                productionSum = productionSum.add(production);
                productionCount++;
            }
        }
        if (viscosityCount != 0) {
            viscosityAvg = viscositySum.divide(new BigDecimal(viscosityCount), 2, RoundingMode.HALF_UP);
        }
        if (endPointCount != 0) {
            endPointAvg = endPointSum.divide(new BigDecimal(endPointCount), 2, RoundingMode.HALF_UP);
        }
        if (pressureDifferenceCount != 0) {
            pressureDifferenceAvg = pressureDifferenceSum.divide(new BigDecimal(pressureDifferenceCount), 2, RoundingMode.HALF_UP);
        }
        if (productionCount != 0) {
            productionAvg = productionSum.divide(new BigDecimal(productionCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("QO粘度(max)="+ viscosityMax.toString() + "\t");
        description.append("汽油终馏点(max)="+ endPointMax.toString() + "\t");
        description.append("汽油段压差(max)="+ pressureDifferenceMax.toString() + "\t");
        description.append("HVGO产量(max)=I"+ productionMax.toString() + "/1000\n");
        description.append("QO粘度(min)="+ viscosityMin.toString() + "\t");
        description.append("汽油终馏点(min)="+ endPointMin.toString() + "\t");
        description.append("汽油段压差(min)="+ pressureDifferenceMin.toString() + "\t");
        description.append("HVGO产量(min)=I"+ productionMin.toString() + "/1000\n");
        description.append("QO粘度(avg)="+ viscosityAvg.toString() + "\t");
        description.append("汽油终馏点(avg)="+ endPointAvg.toString() + "\t");
        description.append("汽油段压差(avg)="+ pressureDifferenceAvg.toString() + "\t");
        description.append("HVGO产量(avg)=I"+ productionAvg.toString() + "/1000\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS2007s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

    /**
     * 导出质量月报S2007列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:export')")
    @Log(title = "质量月报S2007", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TMonthlyQualityReportS2007 tMonthlyQualityReportS2007)
    {
        List<TMonthlyQualityReportS2007> list = tMonthlyQualityReportS2007Service.selectTMonthlyQualityReportS2007List(tMonthlyQualityReportS2007);
        ExcelUtil<TMonthlyQualityReportS2007> util = new ExcelUtil<TMonthlyQualityReportS2007>(TMonthlyQualityReportS2007.class);
        return util.exportExcel(list, "quality");
    }

    /**
     * 获取质量月报S2007详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:quality:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tMonthlyQualityReportS2007Service.selectTMonthlyQualityReportS2007ById(id));
    }

    /**
     * 新增质量月报S2007
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S2007", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS2007 tMonthlyQualityReportS2007)
    {
        return toAjax(tMonthlyQualityReportS2007Service.insertTMonthlyQualityReportS2007(tMonthlyQualityReportS2007));
    }

    /**
     * 修改质量月报S2007
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S2007", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS2007 tMonthlyQualityReportS2007)
    {
        return toAjax(tMonthlyQualityReportS2007Service.updateTMonthlyQualityReportS2007(tMonthlyQualityReportS2007));
    }

    /**
     * 删除质量月报S2007
     */
    @PreAuthorize("@ss.hasPermi('production:quality:remove')")
    @Log(title = "质量月报S2007", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tMonthlyQualityReportS2007Service.deleteTMonthlyQualityReportS2007ByIds(ids));
    }
}
