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.TMonthlyQualityReportS4501;
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.ITMonthlyQualityReportS4501Service;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 质量月报S4501Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s4501")
public class TMonthlyQualityReportS4501Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS4501Service tMonthlyQualityReportS4501Service;

    /**
     * 查询质量月报S4501列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS4501 tMonthlyQualityReportS4501)
    {
        Date endDate = tMonthlyQualityReportS4501.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS4501.setEndDate(endDate);
        List<TMonthlyQualityReportS4501> tMonthlyQualityReportS4501s = tMonthlyQualityReportS4501Service.selectTMonthlyQualityReportS4501List(tMonthlyQualityReportS4501);
        if (tMonthlyQualityReportS4501s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS4501 firstValue = tMonthlyQualityReportS4501s.get(0);
        // 最大值
        BigDecimal temperatureInMax = new BigDecimal(firstValue.getTemperatureIn() == null ? "0" : firstValue.getTemperatureIn());
        BigDecimal temperatureRaiseMax = new BigDecimal(firstValue.getTemperatureRaise() == null ? "0" : firstValue.getTemperatureRaise());
        BigDecimal pressureDiffMax = new BigDecimal(firstValue.getPressureDiff() == null ? "0" : firstValue.getPressureDiff());
        BigDecimal c6C8NaMax = new BigDecimal(firstValue.getC6C8Na() == null ? "0" : firstValue.getC6C8Na());
        BigDecimal brNumberMax = new BigDecimal(firstValue.getBrIndex() == null ? "0" : firstValue.getBrIndex());
        // 最小值
        BigDecimal temperatureInMin = new BigDecimal(firstValue.getTemperatureIn() == null ? "0" : firstValue.getTemperatureIn());
        BigDecimal temperatureRaiseMin = new BigDecimal(firstValue.getTemperatureRaise() == null ? "0" : firstValue.getTemperatureRaise());
        BigDecimal pressureDiffMin = new BigDecimal(firstValue.getPressureDiff() == null ? "0" : firstValue.getPressureDiff());
        BigDecimal c6C8NaMin = new BigDecimal(firstValue.getC6C8Na() == null ? "0" : firstValue.getC6C8Na());
        BigDecimal brNumberMin = new BigDecimal(firstValue.getBrIndex() == null ? "0" : firstValue.getBrIndex());
        // 平均值
        BigDecimal temperatureInAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal temperatureRaiseAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal pressureDiffAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal c6C8NaAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal brNumberAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal temperatureInSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal temperatureRaiseSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal pressureDiffSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal c6C8NaSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal brNumberSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int temperatureInCount = 0;
        int temperatureRaiseCount = 0;
        int pressureDiffCount = 0;
        int c6C8NaCount = 0;
        int brNumberCount = 0;
        for (TMonthlyQualityReportS4501 monthlyQualityReportS4501 : tMonthlyQualityReportS4501s) {
            String temperatureIn1 = monthlyQualityReportS4501.getTemperatureIn();
            if (temperatureIn1 != null) {
                BigDecimal temperatureIn = new BigDecimal(temperatureIn1);
                if (temperatureIn.compareTo(temperatureInMax) > 0) { temperatureInMax = temperatureIn; }
                if (temperatureIn.compareTo(temperatureInMin) < 0) { temperatureInMin = temperatureIn; }
                temperatureInSum = temperatureInSum.add(temperatureIn);
                temperatureInCount++;
            }
            String temperatureRaise1 = monthlyQualityReportS4501.getTemperatureRaise();
            if (temperatureRaise1 != null) {
                BigDecimal temperatureRaise = new BigDecimal(temperatureRaise1);
                if (temperatureRaise.compareTo(temperatureRaiseMax) > 0) { temperatureRaiseMax = temperatureRaise; }
                if (temperatureRaise.compareTo(temperatureRaiseMin) < 0) { temperatureRaiseMin = temperatureRaise; }
                temperatureRaiseSum = temperatureRaiseSum.add(temperatureRaise);
                temperatureRaiseCount++;
            }
            String pressureDiff1 = monthlyQualityReportS4501.getPressureDiff();
            if (pressureDiff1 != null) {
                BigDecimal pressureDiff = new BigDecimal(pressureDiff1);
                if (pressureDiff.compareTo(pressureDiffMax) > 0) { pressureDiffMax = pressureDiff; }
                if (pressureDiff.compareTo(pressureDiffMin) < 0) { pressureDiffMin = pressureDiff; }
                pressureDiffSum = pressureDiffSum.add(pressureDiff);
                pressureDiffCount++;
            }

            String c6C8Na1 = monthlyQualityReportS4501.getC6C8Na();
            if (c6C8Na1 != null) {
                BigDecimal c6C8Na = new BigDecimal(c6C8Na1);
                if (c6C8Na.compareTo(c6C8NaMax) > 0) { c6C8NaMax = c6C8Na; }
                if (c6C8Na.compareTo(c6C8NaMin) < 0) { c6C8NaMin = c6C8Na; }
                c6C8NaSum = c6C8NaSum.add(c6C8Na);
                c6C8NaCount++;
            }
            String brIndex = monthlyQualityReportS4501.getBrIndex();
            if (brIndex != null) {
                BigDecimal brNumber = new BigDecimal(brIndex);
                if (brNumber.compareTo(brNumberMax) > 0) { brNumberMax = brNumber; }
                if (brNumber.compareTo(brNumberMin) < 0) { brNumberMin = brNumber; }
                brNumberSum = brNumberSum.add(brNumber);
                brNumberCount++;
            }
        }
        if (temperatureInCount != 0) {
            temperatureInAvg = temperatureInSum.divide(new BigDecimal(temperatureInCount), 2, RoundingMode.HALF_UP);
        }
        if (temperatureRaiseCount != 0) {
            temperatureRaiseAvg = temperatureRaiseSum.divide(new BigDecimal(temperatureRaiseCount), 2, RoundingMode.HALF_UP);
        }
        if (pressureDiffCount != 0) {
            pressureDiffAvg = pressureDiffSum.divide(new BigDecimal(pressureDiffCount), 2, RoundingMode.HALF_UP);
        }
        if (c6C8NaCount != 0) {
            c6C8NaAvg = c6C8NaSum.divide(new BigDecimal(c6C8NaCount), 2, RoundingMode.HALF_UP);
        }
        if (brNumberCount != 0) {
            brNumberAvg = brNumberSum.divide(new BigDecimal(brNumberCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("temperatureIn(max)="+ temperatureInMax.toString() + "\t");
        description.append("temperatureRaise(max)="+ temperatureRaiseMax.toString() + "\t");
        description.append("pressureDiff(max)="+ pressureDiffMax.toString() + "\t");
        description.append("c6C8Na(max)="+ c6C8NaMax.toString() + "\t");
        description.append("brNumber(max)="+ brNumberMax.toString() + "\n");
        description.append("temperatureIn(min)="+ temperatureInMin.toString() + "\t");
        description.append("temperatureRaise(min)="+ temperatureRaiseMin.toString() + "\t");
        description.append("pressureDiff(min)="+ pressureDiffMin.toString() + "\t");
        description.append("c6C8Na(min)="+ c6C8NaMin.toString() + "\t");
        description.append("brNumber(min)="+ brNumberMin.toString() + "\n");
        description.append("temperatureIn(avg)="+ temperatureInAvg.toString() + "\t");
        description.append("temperatureRaise(avg)="+ temperatureRaiseAvg.toString() + "\t");
        description.append("pressureDiff(avg)="+ pressureDiffAvg.toString() + "\t");
        description.append("c6C8Na(avg)="+ c6C8NaAvg.toString() + "\t");
        description.append("brNumber(avg)="+ brNumberAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS4501s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S4501
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S4501", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS4501 tMonthlyQualityReportS4501)
    {
        return toAjax(tMonthlyQualityReportS4501Service.insertTMonthlyQualityReportS4501(tMonthlyQualityReportS4501));
    }

    /**
     * 修改质量月报S4501
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S4501", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS4501 tMonthlyQualityReportS4501)
    {
        return toAjax(tMonthlyQualityReportS4501Service.updateTMonthlyQualityReportS4501(tMonthlyQualityReportS4501));
    }

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