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

/**
 * 质量月报S5003Controller
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/production/s5003")
public class TMonthlyQualityReportS5003Controller extends BaseController
{
    @Autowired
    private ITMonthlyQualityReportS5003Service tMonthlyQualityReportS5003Service;

    /**
     * 查询质量月报S5003列表
     */
    @PreAuthorize("@ss.hasPermi('production:quality:list')")
    @GetMapping("/list")
    public AjaxResult list(TMonthlyQualityReportS5003 tMonthlyQualityReportS5003)
    {
        Date endDate = tMonthlyQualityReportS5003.getEndDate();
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        tMonthlyQualityReportS5003.setEndDate(endDate);
        List<TMonthlyQualityReportS5003> tMonthlyQualityReportS5003s = tMonthlyQualityReportS5003Service.selectTMonthlyQualityReportS5003List(tMonthlyQualityReportS5003);
        if (tMonthlyQualityReportS5003s.size() == 0) {
            return AjaxResult.success();
        }
        // 获取list第一个元素
        TMonthlyQualityReportS5003 firstValue = tMonthlyQualityReportS5003s.get(0);
        // 最大值
        BigDecimal propyleneMax = new BigDecimal(firstValue.getPropylene() == null ? "0" : firstValue.getPropylene());
        // 最小值
        BigDecimal propyleneMin = new BigDecimal(firstValue.getPropylene() == null ? "0" : firstValue.getPropylene());
        // 平均值
        BigDecimal propyleneAvg = new BigDecimal(BigInteger.ZERO);
        // 总和
        BigDecimal propyleneSum = new BigDecimal(BigInteger.ZERO);
        // 计数
        int propyleneCount = 0;
        for (TMonthlyQualityReportS5003 monthlyQualityReportS5003 : tMonthlyQualityReportS5003s) {
            String propylene1 = monthlyQualityReportS5003.getPropylene();
            if (propylene1 != null) {
                BigDecimal propylene = new BigDecimal(propylene1);
                if (propylene.compareTo(propyleneMax) > 0) {
                    propyleneMax = propylene;
                }
                if (propylene.compareTo(propyleneMin) < 0) {
                    propyleneMin = propylene;
                }
                propyleneSum = propyleneSum.add(propylene);
                propyleneCount++;
            }
        }
        if (propyleneCount != 0) {
            propyleneAvg = propyleneSum.divide(new BigDecimal(propyleneCount), 2, RoundingMode.HALF_UP);
        }
        StringBuffer description = new StringBuffer();
        description.append("propylene(max)="+ propyleneMax.toString() + "\t");
        description.append("propylene(min)="+ propyleneMin.toString() + "\t");
        description.append("propylene(avg)="+ propyleneAvg.toString() + "\n");
        QualityAnalysisVO vo = new QualityAnalysisVO();
        vo.setDataList(tMonthlyQualityReportS5003s);
        vo.setDescription(description.toString());
        return AjaxResult.success(vo);
    }

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

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

    /**
     * 新增质量月报S5003
     */
    @PreAuthorize("@ss.hasPermi('production:quality:add')")
    @Log(title = "质量月报S5003", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMonthlyQualityReportS5003 tMonthlyQualityReportS5003)
    {
        return toAjax(tMonthlyQualityReportS5003Service.insertTMonthlyQualityReportS5003(tMonthlyQualityReportS5003));
    }

    /**
     * 修改质量月报S5003
     */
    @PreAuthorize("@ss.hasPermi('production:quality:edit')")
    @Log(title = "质量月报S5003", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMonthlyQualityReportS5003 tMonthlyQualityReportS5003)
    {
        return toAjax(tMonthlyQualityReportS5003Service.updateTMonthlyQualityReportS5003(tMonthlyQualityReportS5003));
    }

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