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.TMonthlyQualityReportS4012;
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.domain.TMonthlyQualityReportS4012;
import com.ruoyi.project.production.service.ITMonthlyQualityReportS4012Service;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 质量月报S4012Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s4012")
public class TMonthlyQualityReportS4012Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS4012Service tMonthlyQualityReportS4012Service;

    /**
     * 查询质量月报S4012列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS4012 tMonthlyQualityReportS4012)
    {
        Date endDate = tMonthlyQualityReportS4012.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS4012.setEndDate(endDate);
        List<TMonthlyQualityReportS4012> tMonthlyQualityReportS4012s = tMonthlyQualityReportS4012Service.selectTMonthlyQualityReportS4012List(tMonthlyQualityReportS4012);
        if (tMonthlyQualityReportS4012s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS4012 firstValue = tMonthlyQualityReportS4012s.get(0);
        // 最大值
        BigDecimal ethyleneMax = new BigDecimal(firstValue.getEthylene() == null ? "0" : firstValue.getEthylene());
        BigDecimal propyleneMax = new BigDecimal(firstValue.getPropylene() == null ? "0" : firstValue.getPropylene());
        // 最小值
        BigDecimal ethyleneMin = new BigDecimal(firstValue.getEthylene() == null ? "0" : firstValue.getEthylene());
        BigDecimal propyleneMin = new BigDecimal(firstValue.getPropylene() == null ? "0" : firstValue.getPropylene());
        // 平均值
        BigDecimal ethyleneAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal propyleneAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal ethyleneSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal propyleneSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int ethyleneCount = 0;
        int propyleneCount = 0;
        for (TMonthlyQualityReportS4012 monthlyQualityReportS4012 : tMonthlyQualityReportS4012s) {
            String ethylene1 = monthlyQualityReportS4012.getEthylene();
            if (ethylene1 != null) {
                BigDecimal ethylene = new BigDecimal(ethylene1);
                if (ethylene.compareTo(ethyleneMax) > 0) { ethyleneMax = ethylene; }
                if (ethylene.compareTo(ethyleneMin) < 0) { ethyleneMin = ethylene; }
                ethyleneSum = ethyleneSum.add(ethylene);
                ethyleneCount++;
            }
            String propylene1 = monthlyQualityReportS4012.getPropylene();
            if (propylene1 != null) {
                BigDecimal propylene = new BigDecimal(propylene1);
                if (propylene.compareTo(propyleneMax) > 0) {
                    propyleneMax = propylene;
                }
                if (propylene.compareTo(propyleneMin) < 0) {
                    propyleneMin = propylene;
                }
                propyleneSum = propyleneSum.add(propylene);
                propyleneCount++;
            }
        }
        if (ethyleneCount != 0) {
            ethyleneAvg = ethyleneSum.divide(new BigDecimal(ethyleneCount), 2, RoundingMode.HALF_UP);
        }
        if (propyleneCount != 0) {
            propyleneAvg = propyleneSum.divide(new BigDecimal(propyleneCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("ethylene(max)="+ ethyleneMax.toString() + "\t");
        description.append("propylene(max)="+ propyleneMax.toString() + "\n");
        description.append("ethylene(min)="+ ethyleneMin.toString() + "\t");
        description.append("propylene(min)="+ propyleneMin.toString() + "\n");
        description.append("ethylene(avg)="+ ethyleneAvg.toString() + "\t");
        description.append("propylene(avg)="+ propyleneAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS4012s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S4012
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S4012", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS4012 tMonthlyQualityReportS4012)
    {
        return toAjax(tMonthlyQualityReportS4012Service.insertTMonthlyQualityReportS4012(tMonthlyQualityReportS4012));
    }

    /**
     * 修改质量月报S4012
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S4012", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS4012 tMonthlyQualityReportS4012)
    {
        return toAjax(tMonthlyQualityReportS4012Service.updateTMonthlyQualityReportS4012(tMonthlyQualityReportS4012));
    }

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