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

/**
 * 质量月报S3004Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s3004")
public class TMonthlyQualityReportS3004Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS3004Service tMonthlyQualityReportS3004Service;

    /**
     * 查询质量月报S3004列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS3004 tMonthlyQualityReportS3004)
    {
        Date endDate = tMonthlyQualityReportS3004.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS3004.setEndDate(endDate);
        List<TMonthlyQualityReportS3004> tMonthlyQualityReportS3004s = tMonthlyQualityReportS3004Service.selectTMonthlyQualityReportS3004List(tMonthlyQualityReportS3004);
        if (tMonthlyQualityReportS3004s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS3004 firstValue = tMonthlyQualityReportS3004s.get(0);
        // 最大值
        BigDecimal naOhMax = new BigDecimal(firstValue.getNaOh() == null ? "0" : firstValue.getNaOh());
        // 最小值
        BigDecimal naOhMin = new BigDecimal(firstValue.getNaOh() == null ? "0" : firstValue.getNaOh());
        // 平均值
        BigDecimal naOhAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal naOhSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int naOhCount = 0;
        for (TMonthlyQualityReportS3004 monthlyQualityReportS3004 : tMonthlyQualityReportS3004s) {
            String naOh1 = monthlyQualityReportS3004.getNaOh();
            if (naOh1 != null) {
                BigDecimal naOh = new BigDecimal(naOh1);
                if (naOh.compareTo(naOhMax) > 0) { naOhMax = naOh; }
                if (naOh.compareTo(naOhMin) < 0) { naOhMin = naOh; }
                naOhSum = naOhSum.add(naOh);
                naOhCount++;
            }
        }
        if (naOhCount != 0) {
            naOhAvg = naOhSum.divide(new BigDecimal(naOhCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("naOh(max)="+ naOhMax.toString() + "\t");
        description.append("naOh(min)="+ naOhMin.toString() + "\t");
        description.append("naOh(avg)="+ naOhAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS3004s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S3004
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S3004", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS3004 tMonthlyQualityReportS3004)
    {
        return toAjax(tMonthlyQualityReportS3004Service.insertTMonthlyQualityReportS3004(tMonthlyQualityReportS3004));
    }

    /**
     * 修改质量月报S3004
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S3004", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS3004 tMonthlyQualityReportS3004)
    {
        return toAjax(tMonthlyQualityReportS3004Service.updateTMonthlyQualityReportS3004(tMonthlyQualityReportS3004));
    }

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