package com.ruoyi.project.production.controller;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;

import com.ruoyi.project.production.controller.vo.QualityAnalysisVO;
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.TMonthlyQualityReportS0501;
import com.ruoyi.project.production.service.ITMonthlyQualityReportS0501Service;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 质量月报S0501Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s0501")
public class TMonthlyQualityReportS0501Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS0501Service tMonthlyQualityReportS0501Service;

    /**
     * 查询质量月报S0501列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/listMonth")
    public AjaxResult listMonth(TMonthlyQualityReportS0501 tMonthlyQualityReportS0501)
    {
        Date startDate = tMonthlyQualityReportS0501.getStartDate();
        Date endDate = tMonthlyQualityReportS0501.getEndDate();
        int startYear = startDate.getYear() + 1900;
        int endYear = endDate.getYear() + 1900;
        int startMonth = startDate.getMonth() + 1;
        int endMonth = endDate.getMonth() + 1;
        // 趋势图数据
        Map<String, TMonthlyQualityReportS0501> dataMap = new HashMap<>();

        for (int i = 0; i < 12; i++) {
            TMonthlyQualityReportS0501 reportS0501 = new TMonthlyQualityReportS0501();
            reportS0501.setMonth(startMonth);
            reportS0501.setYear(startYear);
            TMonthlyQualityReportS0501 reportS05011 = tMonthlyQualityReportS0501Service.selectTMonthlyQualityReportS0501ListMonth(reportS0501);
            if (reportS05011 != null) {
                dataMap.put(startYear + "-" + startMonth, reportS05011);
            }
            if (startMonth == 12) {
                startMonth = 1;
                startYear++;
            } else {
                startMonth++;
            }
            if (!(startYear != endYear || startMonth != endMonth + 1)) {
                break;
            }
        }

        if (dataMap.size() == 0) {
            return AjaxResult.success();
        }
        // 获取map第一个元素
        List<String> keys = new ArrayList<>(dataMap.keySet());
        String firstKey = keys.get(0);
        TMonthlyQualityReportS0501 firstValue = dataMap.get(firstKey);
        // 最大值
        BigDecimal pMax = new BigDecimal(firstValue.getP() == null ? "0" : firstValue.getP());
        BigDecimal iMax = new BigDecimal(firstValue.getI() == null ? "0" : firstValue.getI());
        BigDecimal oMax = new BigDecimal(firstValue.getO() == null ? "0" : firstValue.getO());
        BigDecimal nMax = new BigDecimal(firstValue.getN() == null ? "0" : firstValue.getN());
        BigDecimal fiftyMax = new BigDecimal(firstValue.getFifty() == null ? "0" : firstValue.getFifty());
        // 最小值
        BigDecimal pMin = new BigDecimal(firstValue.getP() == null ? "0" : firstValue.getP());
        BigDecimal iMin = new BigDecimal(firstValue.getI() == null ? "0" : firstValue.getI());
        BigDecimal oMin = new BigDecimal(firstValue.getO() == null ? "0" : firstValue.getO());
        BigDecimal nMin = new BigDecimal(firstValue.getN() == null ? "0" : firstValue.getN());
        BigDecimal fiftyMin = new BigDecimal(firstValue.getFifty() == null ? "0" : firstValue.getFifty());
        // 平均值
        BigDecimal pAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal iAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal oAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal nAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal fiftyAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal pSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal iSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal oSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal nSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal fiftySum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int pCount = 0;
        int iCount = 0;
        int oCount = 0;
        int nCount = 0;
        int fiftyCount = 0;
        for(Map.Entry entry : dataMap.entrySet()) {
            TMonthlyQualityReportS0501 mapValue = (TMonthlyQualityReportS0501) entry.getValue();
            String p1 = mapValue.getP();
            if (p1 != null) {
                BigDecimal p = new BigDecimal(p1);
                if (p.compareTo(pMax) > 0) { pMax = p; }
                if (p.compareTo(pMin) < 0) { pMin = p; }
                pSum = pSum.add(p);
            }
            String i1 = mapValue.getI();
            if (i1 != null) {
                BigDecimal i = new BigDecimal(i1);
                if (i.compareTo(iMax) > 0) { iMax = i; }
                if (i.compareTo(iMin) < 0) { iMin = i; }
                iSum = iSum.add(i);
            }
            String o1 = mapValue.getO();
            if (o1 != null) {
                BigDecimal o = new BigDecimal(o1);
                if (o.compareTo(oMax) > 0) { oMax = o; }
                if (o.compareTo(oMin) < 0) { oMin = o; }
                oSum = oSum.add(o);
            }
            String n1 = mapValue.getN();
            if (n1 != null) {
                BigDecimal n = new BigDecimal(n1);
                if (n.compareTo(nMax) > 0) { nMax = n; }
                if (n.compareTo(nMin) < 0) { nMin = n; }
                nSum = nSum.add(n);
            }
            String fifty1 = mapValue.getFifty();
            if (fifty1 != null) {
                BigDecimal fifty = new BigDecimal(fifty1);
                if (fifty.compareTo(fiftyMax) > 0) { fiftyMax = fifty; }
                if (fifty.compareTo(fiftyMin) < 0) { fiftyMin = fifty; }
                fiftySum = fiftySum.add(fifty);
            }
        }
        if (pCount != 0) {
            pAvg = pSum.divide(new BigDecimal(pCount), 2, RoundingMode.HALF_UP);
        }
        if (iCount != 0) {
            iAvg = iSum.divide(new BigDecimal(iCount), 2, RoundingMode.HALF_UP);
        }
        if (oCount != 0) {
            oAvg = oSum.divide(new BigDecimal(oCount), 2, RoundingMode.HALF_UP);
        }
        if (nCount != 0) {
            nAvg = nSum.divide(new BigDecimal(nCount), 2, RoundingMode.HALF_UP);
        }
        if (fiftyCount != 0) {
            fiftyAvg = fiftySum.divide(new BigDecimal(fiftyCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("正构烷烃(max)="+ pMax.toString() + "\t");
        description.append("异构烷烃(max)="+ iMax.toString() + "\t");
        description.append("芳烃(max)="+ oMax.toString() + "\t");
        description.append("烷烃(max)="+ nMax.toString() + "\t");
        description.append("50%BP(max)="+ fiftyMax.toString() + "\n");
        description.append("正构烷烃(min)="+ pMin.toString() + "\t");
        description.append("异构烷烃(min)="+ iMin.toString() + "\t");
        description.append("芳烃(min)="+ oMin.toString() + "\t");
        description.append("烷烃(min)="+ nMin.toString() + "\t");
        description.append("50%BP(min)="+ fiftyMin.toString() + "\n");
        description.append("正构烷烃(avg)="+ pAvg.toString() + "\t");
        description.append("异构烷烃(avg)="+ iAvg.toString() + "\t");
        description.append("芳烃(avg)="+ oAvg.toString() + "\t");
        description.append("烷烃(avg)="+ nAvg.toString() + "\t");
        description.append("50%BP(avg)="+ fiftyAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataMap(dataMap);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

    /**
     * 查询质量月报S0501列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS0501 tMonthlyQualityReportS0501)
    {
        Date endDate = tMonthlyQualityReportS0501.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS0501.setEndDate(endDate);
        List<TMonthlyQualityReportS0501> tMonthlyQualityReportS0501s = tMonthlyQualityReportS0501Service.selectTMonthlyQualityReportS0501List(tMonthlyQualityReportS0501);
        if (tMonthlyQualityReportS0501s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS0501 firstValue = tMonthlyQualityReportS0501s.get(0);
        // 最大值
        BigDecimal pMax = new BigDecimal(firstValue.getP() == null ? "0" : firstValue.getP());
        BigDecimal iMax = new BigDecimal(firstValue.getI() == null ? "0" : firstValue.getI());
        BigDecimal oMax = new BigDecimal(firstValue.getO() == null ? "0" : firstValue.getO());
        BigDecimal nMax = new BigDecimal(firstValue.getN() == null ? "0" : firstValue.getN());
        BigDecimal fiftyMax = new BigDecimal(firstValue.getFifty() == null ? "0" : firstValue.getFifty());
        // 最小值
        BigDecimal pMin = new BigDecimal(firstValue.getP() == null ? "0" : firstValue.getP());
        BigDecimal iMin = new BigDecimal(firstValue.getI() == null ? "0" : firstValue.getI());
        BigDecimal oMin = new BigDecimal(firstValue.getO() == null ? "0" : firstValue.getO());
        BigDecimal nMin = new BigDecimal(firstValue.getN() == null ? "0" : firstValue.getN());
        BigDecimal fiftyMin = new BigDecimal(firstValue.getFifty() == null ? "0" : firstValue.getFifty());
        // 平均值
        BigDecimal pAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal iAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal oAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal nAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal fiftyAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal pSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal iSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal oSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal nSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal fiftySum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int pCount = 0;
        int iCount = 0;
        int oCount = 0;
        int nCount = 0;
        int fiftyCount = 0;
        for (TMonthlyQualityReportS0501 monthlyQualityReportS0501 : tMonthlyQualityReportS0501s) {
            String p1 = monthlyQualityReportS0501.getP();
            if (p1 != null) {
                BigDecimal p = new BigDecimal(p1);
                if (p.compareTo(pMax) > 0) { pMax = p; }
                if (p.compareTo(pMin) < 0) { pMin = p; }
                pSum = pSum.add(p);
                pCount++;
            }
            String i1 = monthlyQualityReportS0501.getI();
            if (i1 != null) {
                BigDecimal i = new BigDecimal(i1);
                if (i.compareTo(iMax) > 0) { iMax = i; }
                if (i.compareTo(iMin) < 0) { iMin = i; }
                iSum = iSum.add(i);
                iCount++;
            }
            String o1 = monthlyQualityReportS0501.getO();
            if (o1 != null) {
                BigDecimal o = new BigDecimal(o1);
                if (o.compareTo(oMax) > 0) { oMax = o; }
                if (o.compareTo(oMin) < 0) { oMin = o; }
                oSum = oSum.add(o);
                oCount++;
            }
            String n1 = monthlyQualityReportS0501.getN();
            if (n1 != null) {
                BigDecimal n = new BigDecimal(n1);
                if (n.compareTo(nMax) > 0) { nMax = n; }
                if (n.compareTo(nMin) < 0) { nMin = n; }
                nSum = nSum.add(n);
                nCount++;
            }
            String fifty1 = monthlyQualityReportS0501.getFifty();
            if (fifty1 != null) {
                BigDecimal fifty = new BigDecimal(fifty1);
                if (fifty.compareTo(fiftyMax) > 0) { fiftyMax = fifty; }
                if (fifty.compareTo(fiftyMin) < 0) { fiftyMin = fifty; }
                fiftySum = fiftySum.add(fifty);
                fiftyCount++;
            }
        }
        if (pCount != 0) {
            pAvg = pSum.divide(new BigDecimal(pCount), 2, RoundingMode.HALF_UP);
        }
        if (iCount != 0) {
            iAvg = iSum.divide(new BigDecimal(iCount), 2, RoundingMode.HALF_UP);
        }
        if (oCount != 0) {
            oAvg = oSum.divide(new BigDecimal(oCount), 2, RoundingMode.HALF_UP);
        }
        if (nCount != 0) {
            nAvg = nSum.divide(new BigDecimal(nCount), 2, RoundingMode.HALF_UP);
        }
        if (fiftyCount != 0) {
            fiftyAvg = fiftySum.divide(new BigDecimal(fiftyCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("正构烷烃(max)="+ pMax.toString() + "\t");
        description.append("异构烷烃(max)="+ iMax.toString() + "\t");
        description.append("芳烃(max)="+ oMax.toString() + "\t");
        description.append("烷烃(max)="+ nMax.toString() + "\t");
        description.append("50%BP(max)="+ fiftyMax.toString() + "\n");
        description.append("正构烷烃(min)="+ pMin.toString() + "\t");
        description.append("异构烷烃(min)="+ iMin.toString() + "\t");
        description.append("芳烃(min)="+ oMin.toString() + "\t");
        description.append("烷烃(min)="+ nMin.toString() + "\t");
        description.append("50%BP(min)="+ fiftyMin.toString() + "\n");
        description.append("正构烷烃(avg)="+ pAvg.toString() + "\t");
        description.append("异构烷烃(avg)="+ iAvg.toString() + "\t");
        description.append("芳烃(avg)="+ oAvg.toString() + "\t");
        description.append("烷烃(avg)="+ nAvg.toString() + "\t");
        description.append("50%BP(avg)="+ fiftyAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS0501s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S0501
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S0501", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS0501 tMonthlyQualityReportS0501)
    {
        return toAjax(tMonthlyQualityReportS0501Service.insertTMonthlyQualityReportS0501(tMonthlyQualityReportS0501));
    }

    /**
     * 修改质量月报S0501
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S0501", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS0501 tMonthlyQualityReportS0501)
    {
        return toAjax(tMonthlyQualityReportS0501Service.updateTMonthlyQualityReportS0501(tMonthlyQualityReportS0501));
    }

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