package com.prolog.rdc.tms.controller.report;


import com.prolog.common.util.DateUtils;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.log.aop.annotation.UserLog;
import com.prolog.product.core.easyexcel.util.ExcelUtils;
import com.prolog.rdc.tms.model.dto.report.YogurtOperationExportDTO;
import com.prolog.rdc.tms.model.dto.report.YogurtOperationReportDTO;
import com.prolog.rdc.tms.model.vo.report.YogurtOperationReportVO;
import com.prolog.rdc.tms.service.report.YogurtOperationReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = "酸奶运营统计报表控制层")
@RequestMapping("/yogurt_operation")
public class YogurtOperationReportController {

    @Autowired
    private YogurtOperationReportService service;


    @UserLog("酸奶运营报表分页查询")
    @ApiOperation(value = "酸奶运营报表分页查询", notes = "酸奶运营报表分页查询")
    @PostMapping("/page")
    public RestMessage<Page<YogurtOperationReportDTO>> pageYogurtOperationReportExecute(@RequestBody @Valid YogurtOperationReportVO vo) {
        try {
            Page<YogurtOperationReportDTO> page = service.pageYogurtOperationReport(vo);
            return RestMessage.newInstance(true, "查询成功！", page);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }

    @UserLog("导出所有数据")
    @ApiOperation(value = "导出所有数据", notes = "导出所有数据")
    @PostMapping("/export_all")
    public RestMessage<Object> exportDataExecute(HttpServletResponse response, @RequestBody YogurtOperationReportVO vo) {
        try {
            List<YogurtOperationExportDTO> list = service.exportData(vo);
            ExcelUtils.export(response, YogurtOperationExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }

    @UserLog("导出当前页数据")
    @ApiOperation(value = "导出当前页数据", notes = "导出当前页数据")
    @PostMapping("/export_page")
    public RestMessage<Object> exportPageDataExecute(HttpServletResponse response, @RequestBody YogurtOperationReportVO vo) {
        try {
            List<YogurtOperationExportDTO> list = service.exportPageData(vo);
            ExcelUtils.export(response, YogurtOperationExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }

    @UserLog("导出选择行数据")
    @ApiOperation(value = "导出选择行数据", notes = "导出选择行数据")
    @PostMapping("/export_choose")
    public RestMessage<Object> exportChooseDataExecute(HttpServletResponse response, @RequestBody YogurtOperationReportVO vo) {
        try {
            List<YogurtOperationExportDTO> list = service.exportData(vo);
            ExcelUtils.export(response, YogurtOperationExportDTO.class, DateUtils.format(DateUtils.nowDate(), DateUtils.PATTERN_FULL_SIMPLE), list);
            return RestMessage.newInstance(true, "导出成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, e.getMessage(), null);
        }
    }
}
