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

/**
 * 质量月报S6301Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s6301")
public class TMonthlyQualityReportS6301Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS6301Service tMonthlyQualityReportS6301Service;

    /**
     * 查询质量月报S6301列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS6301 tMonthlyQualityReportS6301)
    {
        Date endDate = tMonthlyQualityReportS6301.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS6301.setEndDate(endDate);
        List<TMonthlyQualityReportS6301> tMonthlyQualityReportS6301s = tMonthlyQualityReportS6301Service.selectTMonthlyQualityReportS6301List(tMonthlyQualityReportS6301);
        if (tMonthlyQualityReportS6301s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS6301 firstValue = tMonthlyQualityReportS6301s.get(0);
        // 最大值
        BigDecimal C5ConjugatedDienesDcpd30Max = new BigDecimal(StringUtils.isNull(firstValue.getC5ConjugatedDienesDcpd30()) ? "0" : firstValue.getC5ConjugatedDienesDcpd30());
        BigDecimal C5ConjugatedDienesDcpd40Max = new BigDecimal(StringUtils.isNull(firstValue.getC5ConjugatedDienesDcpd40()) ? "0" : firstValue.getC5ConjugatedDienesDcpd40());
        BigDecimal C250Max = new BigDecimal(StringUtils.isNull(firstValue.getC250()) ? "0" : firstValue.getC250());
        BigDecimal C4Max = new BigDecimal(StringUtils.isNull(firstValue.getC4()) ? "0" : firstValue.getC4());
        BigDecimal C410Max = new BigDecimal(StringUtils.isNull(firstValue.getC410()) ? "0" : firstValue.getC410());
        // 最小值
        BigDecimal C5ConjugatedDienesDcpd30Min = new BigDecimal(StringUtils.isNull(firstValue.getC5ConjugatedDienesDcpd30()) ? "0" : firstValue.getC5ConjugatedDienesDcpd30());
        BigDecimal C5ConjugatedDienesDcpd40Min = new BigDecimal(StringUtils.isNull(firstValue.getC5ConjugatedDienesDcpd40()) ? "0" : firstValue.getC5ConjugatedDienesDcpd40());
        BigDecimal C250Min = new BigDecimal(StringUtils.isNull(firstValue.getC250()) ? "0" : firstValue.getC250());
        BigDecimal C4Min = new BigDecimal(StringUtils.isNull(firstValue.getC4()) ? "0" : firstValue.getC4());
        BigDecimal C410Min = new BigDecimal(StringUtils.isNull(firstValue.getC410()) ? "0" : firstValue.getC410());
        // 平均值
        BigDecimal C5ConjugatedDienesDcpdAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal C250Avg = new BigDecimal(BigInteger.ZERO);
        BigDecimal C4Avg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal C5ConjugatedDienesDcpd30Sum = new BigDecimal(BigInteger.ZERO);
        BigDecimal C5ConjugatedDienesDcpd40Sum = new BigDecimal(BigInteger.ZERO);
        BigDecimal C250Sum = new BigDecimal(BigInteger.ZERO);
        BigDecimal C4Sum = new BigDecimal(BigInteger.ZERO);
        BigDecimal C410Sum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int C5ConjugatedDienesDcpd30Count = 0;
        int C5ConjugatedDienesDcpd40Count = 0;
        int C250Count = 0;
        int C4Count = 0;
        int C410Count = 0;
        for (TMonthlyQualityReportS6301 monthlyQualityReportS6301 : tMonthlyQualityReportS6301s) {
            String c5ConjugatedDienesDcpd301 = monthlyQualityReportS6301.getC5ConjugatedDienesDcpd30();
            if (c5ConjugatedDienesDcpd301 != null) {
                BigDecimal C5ConjugatedDienesDcpd30 = new BigDecimal(c5ConjugatedDienesDcpd301);
                if (C5ConjugatedDienesDcpd30.compareTo(C5ConjugatedDienesDcpd30Max) > 0) {
                    C5ConjugatedDienesDcpd30Max = C5ConjugatedDienesDcpd30;
                }
                if (C5ConjugatedDienesDcpd30.compareTo(C5ConjugatedDienesDcpd30Min) < 0) {
                    C5ConjugatedDienesDcpd30Min = C5ConjugatedDienesDcpd30;
                }
                C5ConjugatedDienesDcpd30Sum = C5ConjugatedDienesDcpd30Sum.add(C5ConjugatedDienesDcpd30);
                C5ConjugatedDienesDcpd30Count++;
            }
            String c5ConjugatedDienesDcpd401 = monthlyQualityReportS6301.getC5ConjugatedDienesDcpd40();
            if (c5ConjugatedDienesDcpd401 != null) {
                BigDecimal C5ConjugatedDienesDcpd40 = new BigDecimal(c5ConjugatedDienesDcpd401);
                if (C5ConjugatedDienesDcpd40.compareTo(C5ConjugatedDienesDcpd40Max) > 0) {
                    C5ConjugatedDienesDcpd40Max = C5ConjugatedDienesDcpd40;
                }
                if (C5ConjugatedDienesDcpd40.compareTo(C5ConjugatedDienesDcpd40Min) < 0) {
                    C5ConjugatedDienesDcpd40 = C5ConjugatedDienesDcpd40;
                }
                C5ConjugatedDienesDcpd40Sum = C5ConjugatedDienesDcpd40Sum.add(C5ConjugatedDienesDcpd40);
                C5ConjugatedDienesDcpd40Count++;
            }
            String c2501 = monthlyQualityReportS6301.getC250();
            if (c2501 != null) {
                BigDecimal C250 = new BigDecimal(c2501);
                if (C250.compareTo(C250Max) > 0) {
                    C250Max = C250;
                }
                if (C250.compareTo(C250Min) < 0) {
                    C250 = C250;
                }
                C250Sum = C250Sum.add(C250);
                C250Count++;
            }
            String c41 = monthlyQualityReportS6301.getC4();
            if (c41 != null) {
                BigDecimal C4 = new BigDecimal(c41);
                if (C4.compareTo(C4Max) > 0) {
                    C4Max = C4;
                }
                if (C4.compareTo(C4Min) < 0) {
                    C4 = C4;
                }
                C4Sum = C4Sum.add(C4);
                C4Count++;
            }
            String c4101 = monthlyQualityReportS6301.getC410();
            if (c4101 != null) {
                BigDecimal C410 = new BigDecimal(c4101);
                if (C410.compareTo(C410Max) > 0) {
                    C410Max = C410;
                }
                if (C410.compareTo(C410Min) < 0) {
                    C410 = C410;
                }
                C410Sum = C410Sum.add(C410);
                C410Count++;
            }
        }
        if ((C5ConjugatedDienesDcpd30Count+C5ConjugatedDienesDcpd40Count) != 0) {
            C5ConjugatedDienesDcpdAvg = (C5ConjugatedDienesDcpd30Sum.add(C5ConjugatedDienesDcpd40Sum)).divide(new BigDecimal(C5ConjugatedDienesDcpd30Count+C5ConjugatedDienesDcpd40Count), 2, RoundingMode.HALF_UP);
        }
        if (C250Count != 0) {
            C250Avg = C250Sum.divide(new BigDecimal(C250Count), 2, RoundingMode.HALF_UP);
        }
        if ((C4Count+C410Count) != 0) {
            C4Avg = (C4Sum.add(C410Sum)).divide(new BigDecimal((C4Count+C410Count)), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("C5ConjugatedDienesDcpd30(max)="+ (C5ConjugatedDienesDcpd30Max.compareTo(C5ConjugatedDienesDcpd40Max) > 0 ? C5ConjugatedDienesDcpd30Max : C5ConjugatedDienesDcpd40Max).toString().toString() + "\t");
        description.append("C250(max)="+ C250Max.toString() + "\t");
        description.append("C4(max)="+ (C4Max.compareTo(C410Max) > 0 ? C4Max : C410Max).toString() + "\n");
        description.append("C5ConjugatedDienesDcpd30(min)="+ (C5ConjugatedDienesDcpd30Min.compareTo(C5ConjugatedDienesDcpd40Min) < 0 ? C5ConjugatedDienesDcpd30Min : C5ConjugatedDienesDcpd40Min).toString().toString() + "\t");
        description.append("C250(min)="+ C250Min.toString() + "\t");
        description.append("C4(min)="+ (C4Min.compareTo(C410Min) < 0 ? C4Min : C410Min).toString() + "\n");
        description.append("C5ConjugatedDienesDcpd(avg)="+ C5ConjugatedDienesDcpdAvg.toString() + "\t");
        description.append("C250(avg)="+ C250Avg.toString() + "\t");
        description.append("C4(avg)="+ C4Avg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS6301s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S6301
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S6301", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS6301 tMonthlyQualityReportS6301)
    {
        return toAjax(tMonthlyQualityReportS6301Service.insertTMonthlyQualityReportS6301(tMonthlyQualityReportS6301));
    }

    /**
     * 修改质量月报S6301
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S6301", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS6301 tMonthlyQualityReportS6301)
    {
        return toAjax(tMonthlyQualityReportS6301Service.updateTMonthlyQualityReportS6301(tMonthlyQualityReportS6301));
    }

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