package com.ruoyi.project.production.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ruoyi.project.production.controller.vo.DailyAnalysisDataVO;
import com.ruoyi.project.production.controller.vo.DailyAnalysisQueryVO;
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.TDailyProductionReport;
import com.ruoyi.project.production.service.ITDailyProductionReportService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 每日生产报告Controller
 *
 * @author ruoyi
 * @date 2022-08-23
 */
@RestController
@RequestMapping("/production/report")
public class TDailyProductionReportController extends BaseController
{
    @Autowired
    private ITDailyProductionReportService tDailyProductionReportService;

    /**
     * 查询每日生产报告趋势数据
     */
    @PreAuthorize("@ss.hasPermi('production:report:list')")
    @GetMapping("/getAnalysisData")
    public AjaxResult getAnalysisData(DailyAnalysisQueryVO dailyAnalysisQueryVO)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = sdf.format(dailyAnalysisQueryVO.getStartDate());
        String endDate = sdf.format(dailyAnalysisQueryVO.getEndDate());
        List<DailyAnalysisDataVO> dailyAnalysisDataVOList = tDailyProductionReportService.selectAnalysisData(dailyAnalysisQueryVO.getFieldName(), startDate, endDate);
        return AjaxResult.success(dailyAnalysisDataVOList);
    }

    /**
     * 导入每日生产报告数据
     *
     * @param file 用户上传的文件
     * @return
     * @throws Exception
     */
    @Log(title = "每日生产报告", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('production:report:add')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        tDailyProductionReportService.importData(file);
        return AjaxResult.success("文件上传成功！");
    }

    /**
     * 查询日期最新的每日生产报告列表
     */
    @PreAuthorize("@ss.hasPermi('production:report:query')")
    @GetMapping("/selectTDailyProductionReportByMaxDate")
    public AjaxResult selectTDailyProductionReportByMaxDate()
    {
        return AjaxResult.success(tDailyProductionReportService.selectTDailyProductionReportByMaxDate());
    }

    /**
     * 查询每日生产报告列表
     */
    @PreAuthorize("@ss.hasPermi('production:report:list')")
    @GetMapping("/listReportMonthly")
    public TableDataInfo listReportMonthly(TDailyProductionReport tDailyProductionReport)
    {
        startPage();
        List<TDailyProductionReport> list = tDailyProductionReportService.selectTDailyProductionReportListMonthly(tDailyProductionReport);
        return getDataTable(list);
    }

    /**
     * 查询每日生产报告列表
     */
    @PreAuthorize("@ss.hasPermi('production:report:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDailyProductionReport tDailyProductionReport)
    {
        startPage();
        List<TDailyProductionReport> list = tDailyProductionReportService.selectTDailyProductionReportList(tDailyProductionReport);
        return getDataTable(list);
    }

    /**
     * 导出每日生产报告列表
     */
    @PreAuthorize("@ss.hasPermi('production:report:export')")
    @Log(title = "每日生产报告", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TDailyProductionReport tDailyProductionReport)
    {
        List<TDailyProductionReport> list = tDailyProductionReportService.selectTDailyProductionReportList(tDailyProductionReport);
        ExcelUtil<TDailyProductionReport> util = new ExcelUtil<TDailyProductionReport>(TDailyProductionReport.class);
        return util.exportExcel(list, "report");
    }

    /**
     * 获取每日生产报告详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tDailyProductionReportService.selectTDailyProductionReportById(id));
    }

    /**
     * 新增每日生产报告
     */
    @PreAuthorize("@ss.hasPermi('production:report:add')")
    @Log(title = "每日生产报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDailyProductionReport tDailyProductionReport)
    {
        return toAjax(tDailyProductionReportService.insertTDailyProductionReport(tDailyProductionReport));
    }

    /**
     * 修改每日生产报告
     */
    @PreAuthorize("@ss.hasPermi('production:report:edit')")
    @Log(title = "每日生产报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDailyProductionReport tDailyProductionReport)
    {
        return toAjax(tDailyProductionReportService.updateTDailyProductionReport(tDailyProductionReport));
    }

    /**
     * 删除每日生产报告
     */
    @PreAuthorize("@ss.hasPermi('production:report:remove')")
    @Log(title = "每日生产报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDailyProductionReportService.deleteTDailyProductionReportByIds(ids));
    }
}
