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

/**
 * 质量月报S6101Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s6101")
public class TMonthlyQualityReportS6101Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS6101Service tMonthlyQualityReportS6101Service;

    /**
     * 查询质量月报S6101列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS6101 tMonthlyQualityReportS6101)
    {
        Date endDate = tMonthlyQualityReportS6101.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS6101.setEndDate(endDate);
        List<TMonthlyQualityReportS6101> tMonthlyQualityReportS6101s = tMonthlyQualityReportS6101Service.selectTMonthlyQualityReportS6101List(tMonthlyQualityReportS6101);
        if (tMonthlyQualityReportS6101s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS6101 firstValue = tMonthlyQualityReportS6101s.get(0);
        // 最大值
        BigDecimal brNumberMax = new BigDecimal(StringUtils.isNull(firstValue.getBrNumber()) ? "0" : firstValue.getBrNumber());
        BigDecimal totalSulfurMax = new BigDecimal(StringUtils.isNull(firstValue.getTotalSulfur()) ? "0" : firstValue.getTotalSulfur());
        BigDecimal totalNitrogenMax = new BigDecimal(StringUtils.isNull(firstValue.getTotalNitrogen()) ? "0" : firstValue.getTotalNitrogen());
        // 最小值
        BigDecimal brNumberMin = new BigDecimal(StringUtils.isNull(firstValue.getBrNumber()) ? "0" : firstValue.getBrNumber());
        BigDecimal totalSulfurMin = new BigDecimal(StringUtils.isNull(firstValue.getTotalSulfur()) ? "0" : firstValue.getTotalSulfur());
        BigDecimal totalNitrogenMin = new BigDecimal(StringUtils.isNull(firstValue.getTotalNitrogen()) ? "0" : firstValue.getTotalNitrogen());
        // 平均值
        BigDecimal brNumberAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal totalSulfurAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal totalNitrogenAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal brNumberSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal totalSulfurSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal totalNitrogenSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int brNumberCount = 0;
        int totalSulfurCount = 0;
        int totalNitrogenCount = 0;
        for (TMonthlyQualityReportS6101 monthlyQualityReportS6101 : tMonthlyQualityReportS6101s) {
            String brNumber1 = monthlyQualityReportS6101.getBrNumber();
            if (brNumber1 != null) {
                BigDecimal brNumber = new BigDecimal(brNumber1);
                if (brNumber.compareTo(brNumberMax) > 0) {
                    brNumberMax = brNumber;
                }
                if (brNumber.compareTo(brNumberMin) < 0) {
                    brNumberMin = brNumber;
                }
                brNumberSum = brNumberSum.add(brNumber);
                brNumberCount++;
            }
            String totalSulfur1 = monthlyQualityReportS6101.getTotalSulfur();
            if (totalSulfur1 != null) {
                BigDecimal totalSulfur = new BigDecimal(totalSulfur1);
                if (totalSulfur.compareTo(totalSulfurMax) > 0) {
                    totalSulfurMax = totalSulfur;
                }
                if (totalSulfur.compareTo(totalSulfurMin) < 0) {
                    totalSulfurMin = totalSulfur;
                }
                totalSulfurSum = totalSulfurSum.add(totalSulfur);
                totalSulfurCount++;
            }
            String totalNitrogen1 = monthlyQualityReportS6101.getTotalNitrogen();
            if (totalNitrogen1 != null) {
                BigDecimal totalNitrogen = new BigDecimal(totalNitrogen1);
                if (totalNitrogen.compareTo(totalNitrogenMax) > 0) {
                    totalNitrogenMax = totalNitrogen;
                }
                if (totalNitrogen.compareTo(totalNitrogenMin) < 0) {
                    totalNitrogenMin = totalNitrogen;
                }
                totalNitrogenSum = totalNitrogenSum.add(totalNitrogen);
                totalNitrogenCount++;
            }
        }
        if (brNumberCount != 0) {
            brNumberAvg = brNumberSum.divide(new BigDecimal(brNumberCount), 2, RoundingMode.HALF_UP);
        }
        if (totalSulfurCount != 0) {
            totalSulfurAvg = totalSulfurSum.divide(new BigDecimal(totalSulfurCount), 2, RoundingMode.HALF_UP);
        }
        if (totalNitrogenCount != 0) {
            totalNitrogenAvg = totalNitrogenSum.divide(new BigDecimal(totalNitrogenCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("brNumber(max)="+ brNumberMax.toString() + "\t");
        description.append("totalSulfur(max)="+ totalSulfurMax.toString() + "\t");
        description.append("totalNitrogen(max)="+ totalNitrogenMax.toString() + "\n");
        description.append("brNumber(min)="+ brNumberMin.toString() + "\t");
        description.append("totalSulfur(min)="+ totalSulfurMin.toString() + "\t");
        description.append("totalNitrogen(min)="+ totalNitrogenMin.toString() + "\n");
        description.append("brNumber(avg)="+ brNumberAvg.toString() + "\t");
        description.append("totalSulfur(avg)="+ totalSulfurAvg.toString() + "\t");
        description.append("totalNitrogen(avg)="+ totalNitrogenAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS6101s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S6101
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S6101", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS6101 tMonthlyQualityReportS6101)
    {
        return toAjax(tMonthlyQualityReportS6101Service.insertTMonthlyQualityReportS6101(tMonthlyQualityReportS6101));
    }

    /**
     * 修改质量月报S6101
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S6101", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS6101 tMonthlyQualityReportS6101)
    {
        return toAjax(tMonthlyQualityReportS6101Service.updateTMonthlyQualityReportS6101(tMonthlyQualityReportS6101));
    }

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