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.TMonthlyQualityReportS5001;
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.TMonthlyQualityReportS5001;
import com.ruoyi.project.production.service.ITMonthlyQualityReportS5001Service;
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;

/**
 * 质量月报S5001Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s5001")
public class TMonthlyQualityReportS5001Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS5001Service tMonthlyQualityReportS5001Service;

    /**
     * 查询质量月报S5001列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS5001 tMonthlyQualityReportS5001)
    {
        Date endDate = tMonthlyQualityReportS5001.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS5001.setEndDate(endDate);
        List<TMonthlyQualityReportS5001> tMonthlyQualityReportS5001s = tMonthlyQualityReportS5001Service.selectTMonthlyQualityReportS5001List(tMonthlyQualityReportS5001);
        for (TMonthlyQualityReportS5001 monthlyQualityReportS5001 : tMonthlyQualityReportS5001s) {
            String propadieneStr = monthlyQualityReportS5001.getPropadiene();
            String methylacetyleneStr = monthlyQualityReportS5001.getMethylacetylene();
            BigDecimal propadiene = new BigDecimal(propadieneStr == null ? "0" : propadieneStr);
            BigDecimal methylacetylene = new BigDecimal(methylacetyleneStr == null ? "0" : methylacetyleneStr);
            BigDecimal mapd = propadiene.add(methylacetylene);
            monthlyQualityReportS5001.setMapd(mapd.toString());
        }
        if (tMonthlyQualityReportS5001s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS5001 firstValue = tMonthlyQualityReportS5001s.get(0);
        // 最大值
        BigDecimal buteneMax = new BigDecimal(firstValue.getButene() == null ? "0" : firstValue.getButene());
        BigDecimal propyleneMax = new BigDecimal(firstValue.getPropylene() == null ? "0" : firstValue.getPropylene());
        BigDecimal mapdMax = new BigDecimal(firstValue.getMapd() == null ? "0" : firstValue.getMapd());
        // 最小值
        BigDecimal buteneMin = new BigDecimal(firstValue.getButene() == null ? "0" : firstValue.getButene());
        BigDecimal propyleneMin = new BigDecimal(firstValue.getPropylene() == null ? "0" : firstValue.getPropylene());
        BigDecimal mapdMin = new BigDecimal(firstValue.getMapd() == null ? "0" : firstValue.getMapd());
        // 平均值
        BigDecimal buteneAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal propyleneAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal mapdAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal buteneSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal propyleneSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal mapdSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int buteneCount = 0;
        int propyleneCount = 0;
        int mapdCount = 0;
        for (TMonthlyQualityReportS5001 monthlyQualityReportS5001 : tMonthlyQualityReportS5001s) {
            String butene1 = monthlyQualityReportS5001.getButene();
            if (butene1 != null) {
                BigDecimal butene = new BigDecimal(butene1);
                if (butene.compareTo(buteneMax) > 0) { buteneMax = butene; }
                if (butene.compareTo(buteneMin) < 0) { buteneMin = butene; }
                buteneSum = buteneSum.add(butene);
                buteneCount++;
            }
            String propylene1 = monthlyQualityReportS5001.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++;
            }
            String mapd1 = monthlyQualityReportS5001.getMapd();
            if (mapd1 != null) {
                BigDecimal mapd = new BigDecimal(mapd1);
                if (mapd.compareTo(mapdMax) > 0) {
                    mapdMax = mapd;
                }
                if (mapd.compareTo(mapdMin) < 0) {
                    mapdMin = mapd;
                }
                mapdSum = mapdSum.add(mapd);
                mapdCount++;
            }
        }
        if (buteneCount != 0) {
            buteneAvg = buteneSum.divide(new BigDecimal(buteneCount), 2, RoundingMode.HALF_UP);
        }
        if (propyleneCount != 0) {
            propyleneAvg = propyleneSum.divide(new BigDecimal(propyleneCount), 2, RoundingMode.HALF_UP);
        }
        if (mapdCount != 0) {
            mapdAvg = mapdSum.divide(new BigDecimal(mapdCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("butene(max)="+ buteneMax.toString() + "\t");
        description.append("propylene(max)="+ propyleneMax.toString() + "\t");
        description.append("mapd(max)="+ mapdMax.toString() + "\n");
        description.append("butene(min)="+ buteneMin.toString() + "\t");
        description.append("propylene(min)="+ propyleneMin.toString() + "\t");
        description.append("mapd(min)="+ mapdMin.toString() + "\n");
        description.append("butene(avg)="+ buteneAvg.toString() + "\t");
        description.append("propylene(avg)="+ propyleneAvg.toString() + "\t");
        description.append("mapd(avg)="+ mapdAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS5001s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S5001
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S5001", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS5001 tMonthlyQualityReportS5001)
    {
        return toAjax(tMonthlyQualityReportS5001Service.insertTMonthlyQualityReportS5001(tMonthlyQualityReportS5001));
    }

    /**
     * 修改质量月报S5001
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S5001", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS5001 tMonthlyQualityReportS5001)
    {
        return toAjax(tMonthlyQualityReportS5001Service.updateTMonthlyQualityReportS5001(tMonthlyQualityReportS5001));
    }

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