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

/**
 * 质量月报S2400Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s2400")
public class TMonthlyQualityReportS2400Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS2400Service tMonthlyQualityReportS2400Service;

    /**
     * 查询质量月报S2400列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS2400 tMonthlyQualityReportS2400)
    {
        Date endDate = tMonthlyQualityReportS2400.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS2400.setEndDate(endDate);
        List<TMonthlyQualityReportS2400> tMonthlyQualityReportS2400s = tMonthlyQualityReportS2400Service.selectTMonthlyQualityReportS2400List(tMonthlyQualityReportS2400);
        if (tMonthlyQualityReportS2400s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS2400 firstValue = tMonthlyQualityReportS2400s.get(0);
        // 最大值
        BigDecimal codMax = new BigDecimal(firstValue.getCod() == null ? "0" : firstValue.getCod());
        BigDecimal phMax = new BigDecimal(firstValue.getPh() == null ? "0" : firstValue.getPh());
        // 最小值
        BigDecimal codMin = new BigDecimal(firstValue.getCod() == null ? "0" : firstValue.getCod());
        BigDecimal phMin = new BigDecimal(firstValue.getPh() == null ? "0" : firstValue.getPh());
        // 平均值
        BigDecimal codAvg = new BigDecimal(BigInteger.ZERO);
        BigDecimal phAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal codSum = new BigDecimal(BigInteger.ZERO);
        BigDecimal phSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int codCount = 0;
        int phCount = 0;
        for (TMonthlyQualityReportS2400 monthlyQualityReportS2400 : tMonthlyQualityReportS2400s) {
            String cod1 = monthlyQualityReportS2400.getCod();
            if (cod1 != null) {
                BigDecimal cod = new BigDecimal(cod1);
                if (cod.compareTo(codMax) > 0) { codMax = cod; }
                if (cod.compareTo(codMin) < 0) { codMin = cod; }
                codSum = codSum.add(cod);
                codCount++;
            }
            String ph1 = monthlyQualityReportS2400.getPh();
            if (ph1 != null) {
                BigDecimal ph = new BigDecimal(ph1);
                if (ph.compareTo(phMax) > 0) { phMax = ph; }
                if (ph.compareTo(phMin) < 0) { phMin = ph; }
                phSum = phSum.add(ph);
                phCount++;
            }
        }
        if (codCount != 0) {
            codAvg = codSum.divide(new BigDecimal(codCount), 2, RoundingMode.HALF_UP);
        }
        if (phCount != 0) {
            phAvg = phSum.divide(new BigDecimal(phCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("cod(max)="+ codMax.toString() + "\t");
        description.append("ph(max)="+ phMax.toString() + "\t");
        description.append("cod(min)="+ codMin.toString() + "\t");
        description.append("ph(min)="+ phMin.toString() + "\t");
        description.append("cod(avg)="+ codAvg.toString() + "\t");
        description.append("ph(avg)="+ phAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS2400s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S2400
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S2400", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS2400 tMonthlyQualityReportS2400)
    {
        return toAjax(tMonthlyQualityReportS2400Service.insertTMonthlyQualityReportS2400(tMonthlyQualityReportS2400));
    }

    /**
     * 修改质量月报S2400
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S2400", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS2400 tMonthlyQualityReportS2400)
    {
        return toAjax(tMonthlyQualityReportS2400Service.updateTMonthlyQualityReportS2400(tMonthlyQualityReportS2400));
    }

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