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

/**
 * 质量月报S4013Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s4013")
public class TMonthlyQualityReportS4013Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS4013Service tMonthlyQualityReportS4013Service;

    /**
     * 查询质量月报S4013列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS4013 tMonthlyQualityReportS4013)
    {
        Date endDate = tMonthlyQualityReportS4013.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS4013.setEndDate(endDate);
        List<TMonthlyQualityReportS4013> tMonthlyQualityReportS4013s = tMonthlyQualityReportS4013Service.selectTMonthlyQualityReportS4013List(tMonthlyQualityReportS4013);
        if (tMonthlyQualityReportS4013s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS4013 firstValue = tMonthlyQualityReportS4013s.get(0);
        // 最大值
        BigDecimal ethyleneMax = new BigDecimal(firstValue.getEthylene() == null ? "0" : firstValue.getEthylene());
        // 最小值
        BigDecimal ethyleneMin = new BigDecimal(firstValue.getEthylene() == null ? "0" : firstValue.getEthylene());
        // 平均值
        BigDecimal ethyleneAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal ethyleneSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int ethyleneCount = 0;
        for (TMonthlyQualityReportS4013 monthlyQualityReportS4013 : tMonthlyQualityReportS4013s) {
            String ethylene1 = monthlyQualityReportS4013.getEthylene();
            if (ethylene1 != null) {
                BigDecimal ethylene = new BigDecimal(ethylene1);
                if (ethylene.compareTo(ethyleneMax) > 0) { ethyleneMax = ethylene; }
                if (ethylene.compareTo(ethyleneMin) < 0) { ethyleneMin = ethylene; }
                ethyleneSum = ethyleneSum.add(ethylene);
                ethyleneCount++;
            }
        }
        if (ethyleneCount != 0) {
            ethyleneAvg = ethyleneSum.divide(new BigDecimal(ethyleneCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("ethylene(max)="+ ethyleneMax.toString() + "\t");
        description.append("ethylene(min)="+ ethyleneMin.toString() + "\t");
        description.append("ethylene(avg)="+ ethyleneAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS4013s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S4013
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S4013", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS4013 tMonthlyQualityReportS4013)
    {
        return toAjax(tMonthlyQualityReportS4013Service.insertTMonthlyQualityReportS4013(tMonthlyQualityReportS4013));
    }

    /**
     * 修改质量月报S4013
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S4013", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS4013 tMonthlyQualityReportS4013)
    {
        return toAjax(tMonthlyQualityReportS4013Service.updateTMonthlyQualityReportS4013(tMonthlyQualityReportS4013));
    }

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