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

/**
 * 质量月报S1301Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s1301")
public class TMonthlyQualityReportS1301Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS1301Service tMonthlyQualityReportS1301Service;

    /**
     * 查询质量月报S1301列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS1301 tMonthlyQualityReportS1301)
    {
        Date endDate = tMonthlyQualityReportS1301.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS1301.setEndDate(endDate);
        List<TMonthlyQualityReportS1301> tMonthlyQualityReportS1301s = tMonthlyQualityReportS1301Service.selectTMonthlyQualityReportS1301List(tMonthlyQualityReportS1301);
        if (tMonthlyQualityReportS1301s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS1301 firstValue = tMonthlyQualityReportS1301s.get(0);
        // 最大值
        BigDecimal temperatureInMax = new BigDecimal(firstValue.getTemperatureIn() == null ? "0" : firstValue.getTemperatureIn());
        BigDecimal temperatureRaiseMax = new BigDecimal(firstValue.getTemperatureRaise() == null ? "0" : firstValue.getTemperatureRaise());
        BigDecimal styreneMax = new BigDecimal(firstValue.getStyrene() == null ? "0" : firstValue.getStyrene());
        BigDecimal dieneValueMax = new BigDecimal(firstValue.getDieneValue() == null ? "0" : firstValue.getDieneValue());
        // 最小值
        BigDecimal temperatureInMin = new BigDecimal(firstValue.getTemperatureIn() == null ? "0" : firstValue.getTemperatureIn());
        BigDecimal temperatureRaiseMin = new BigDecimal(firstValue.getTemperatureRaise() == null ? "0" : firstValue.getTemperatureRaise());
        BigDecimal styreneMin = new BigDecimal(firstValue.getStyrene() == null ? "0" : firstValue.getStyrene());
        BigDecimal dieneValueMin = new BigDecimal(firstValue.getDieneValue() == null ? "0" : firstValue.getDieneValue());
        // 平均值
        BigDecimal temperatureInAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal temperatureRaiseAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal styreneAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal dieneValueAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal temperatureInSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal temperatureRaiseSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal styreneSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal dieneValueSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int temperatureInCount = 0;
        int temperatureRaiseCount = 0;
        int styreneCount = 0;
        int dieneValueCount = 0;
        for (TMonthlyQualityReportS1301 monthlyQualityReportS1301 : tMonthlyQualityReportS1301s) {
            String temperatureIn1 = monthlyQualityReportS1301.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 = monthlyQualityReportS1301.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 styrene1 = monthlyQualityReportS1301.getStyrene();
            if (styrene1 != null) {
                BigDecimal styrene = new BigDecimal(styrene1);
                if (styrene.compareTo(styreneMax) > 0) {
                    styreneMax = styrene;
                }
                if (styrene.compareTo(styreneMin) < 0) {
                    styreneMin = styrene;
                }
                styreneSum = styreneSum.add(styrene);
                styreneCount++;
            }
            String dieneValue1 = monthlyQualityReportS1301.getDieneValue();
            if (dieneValue1 != null) {
                BigDecimal dieneValue = new BigDecimal(dieneValue1);
                if (dieneValue.compareTo(dieneValueMax) > 0) {
                    dieneValueMax = dieneValue;
                }
                if (dieneValue.compareTo(dieneValueMin) < 0) {
                    dieneValueMin = dieneValue;
                }
                dieneValueSum = dieneValueSum.add(dieneValue);
                dieneValueCount++;
            }
        }
        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 (styreneCount != 0) {
            styreneAvg = styreneSum.divide(new BigDecimal(styreneCount), 2, RoundingMode.HALF_UP);
        }
        if (dieneValueCount != 0) {
            dieneValueAvg = dieneValueSum.divide(new BigDecimal(dieneValueCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("temperatureIn(max)="+ temperatureInMax.toString() + "\t");
        description.append("temperatureRaise(max)="+ temperatureRaiseMax.toString() + "\t");
        description.append("styrene(max)="+ styreneMax.toString() + "\t");
        description.append("dieneValue(max)="+ dieneValueMax.toString() + "\n");
        description.append("temperatureIn(min)="+ temperatureInMin.toString() + "\t");
        description.append("temperatureRaise(min)="+ temperatureRaiseMin.toString() + "\t");
        description.append("styrene(min)="+ styreneMin.toString() + "\t");
        description.append("dieneValue(min)="+ dieneValueMin.toString() + "\n");
        description.append("temperatureIn(avg)="+ temperatureInAvg.toString() + "\t");
        description.append("temperatureRaise(avg)="+ temperatureRaiseAvg.toString() + "\t");
        description.append("styrene(avg)="+ styreneAvg.toString() + "\t");
        description.append("dieneValue(avg)="+ dieneValueAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS1301s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S1301
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S1301", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS1301 tMonthlyQualityReportS1301)
    {
        return toAjax(tMonthlyQualityReportS1301Service.insertTMonthlyQualityReportS1301(tMonthlyQualityReportS1301));
    }

    /**
     * 修改质量月报S1301
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S1301", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS1301 tMonthlyQualityReportS1301)
    {
        return toAjax(tMonthlyQualityReportS1301Service.updateTMonthlyQualityReportS1301(tMonthlyQualityReportS1301));
    }

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