package digital.laboratory.platform.inspection.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.inspection.dto.ExportSewageAnalystReportsDTO;
import digital.laboratory.platform.inspection.dto.SewageDataDto;
import digital.laboratory.platform.inspection.dto.TaskInfoDto;
import digital.laboratory.platform.inspection.entity.TaskInfo;
import digital.laboratory.platform.inspection.service.TaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.io.IOException;
import java.util.List;

/**
 * @author xy
 * @version 1.0
 * @title TaskInfoController
 * @description
 * @create 2023/12/8 11:24
 */
@RestController
@RequestMapping("/taskInfo")
@Api(tags = "02-任务基本信息管理", description = "任务基本信息管理")
public class TaskInfoController {
    @Resource
    private TaskInfoService taskInfoService;

    //添加接口
    @PostMapping("/addTaskInfo")
    @ApiOperation(value = "添加任务基本信息", notes = "添加任务基本信息,同时也作为其他系统调用的外部接口")
    public R addTaskInfo(@RequestBody TaskInfoDto taskInfo) {
        TaskInfo ret = taskInfoService.addTaskInfo(taskInfo);
        if (ret != null) {
            return R.ok(ret, "添加成功");
        } else {
            return R.failed("false", "添加失败");
        }

    }

    //修改接口
    @PostMapping("/updateTaskInfo")
    @ApiOperation(value = "修改任务基本信息", notes = "")
    public R updateTaskInfo(@RequestBody TaskInfo taskInfo) {
        TaskInfo ret = taskInfoService.updateTaskInfo(taskInfo);
        if (ret != null) {
            return R.ok(ret, "添加成功");
        } else {
            return R.failed("false", "添加失败");
        }
    }

    //删除数据
    @GetMapping("/deleteTaskInfo")
    @ApiOperation(value = "删除任务基本信息", notes = "")
    public R deleteTaskInfo(String id) {
        Assert.notBlank(id, "参数id不能为空");
        boolean ret = taskInfoService.deleteTaskInfo(id);
        if (ret) {
            return R.ok("true", "删除成功");
        } else {
            return R.failed("false", "删除成功");
        }
    }

    //显示列表分页
    @GetMapping("/getTaskPageList")
    @ApiOperation(value = "获取分页数据", notes = "")
    public R getTaskPageList(Page page, TaskInfo taskInfo, String keywords) {
        return R.ok(taskInfoService.getTaskPageList(page, taskInfo, keywords), "获取数据成功");
    }

    //显示列表
    @GetMapping("/getTaskList")
    @ApiOperation(value = "获取列表", notes = "")
    public R getTaskList(TaskInfo taskInfo, String keywords) {
        return R.ok(taskInfoService.getTaskList(taskInfo, keywords), "获取数据成功");
    }

    @PostMapping("/create/sewageReport")
    @ApiOperation(value = "导出污水消费量计算Excel，参数：dailySmokingPerCapita为人均日吸烟量(支)")
    public R createSewageReport(String taskId, Double dailySmokingPerCapita) throws IOException {
        List<SewageDataDto> sewageReportData = taskInfoService.createSewageReportData(taskId, dailySmokingPerCapita);
        String filePath = taskInfoService.createSewageReportExcel(sewageReportData, taskId);
        return R.ok(filePath, "生成成功!");
    }

    @PostMapping("/exportSewageAnalystReports")
    @ApiOperation(value = "导出污水专项检测毒品分析报告", notes = "按月份导出")
    public R exportSewageAnalystReports(@RequestBody ExportSewageAnalystReportsDTO dto) {
        return taskInfoService.exportSewageAnalystReports(dto);
    }


}
