package cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskschedule;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskschedule.vo.IotProjectTaskSchedulePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskschedule.vo.IotProjectTaskScheduleRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskschedule.vo.IotProjectTaskScheduleSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskattrs.IotTaskAttrModelProperty;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskschedule.IotProjectTaskScheduleDO;
import cn.iocoder.yudao.module.pms.service.iotprojecttaskschedule.IotProjectTaskScheduleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 项目任务时间表/施工进度")
@RestController
@RequestMapping("/rq/iot-project-task-schedule")
@Validated
public class IotProjectTaskScheduleController {

    @Resource
    private IotProjectTaskScheduleService iotProjectTaskScheduleService;

    @PostMapping("/create")
    @Operation(summary = "创建项目任务时间表/施工进度")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:create')")
    public CommonResult<Long> createIotProjectTaskSchedule(@Valid @RequestBody IotProjectTaskScheduleSaveReqVO createReqVO) {
        return success(iotProjectTaskScheduleService.createIotProjectTaskSchedule(createReqVO));
    }

    @PostMapping("/saveTaskSchedule")
    @Operation(summary = "保存项目日报 任务进度表")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:create')")
    public CommonResult<Long> saveTaskSchedule(@Valid @RequestBody List<IotProjectTaskScheduleSaveReqVO> schedule) {
        return success(iotProjectTaskScheduleService.saveTaskSchedule(schedule));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目任务时间表/施工进度")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:update')")
    public CommonResult<Boolean> updateIotProjectTaskSchedule(@Valid @RequestBody IotProjectTaskScheduleSaveReqVO updateReqVO) {
        iotProjectTaskScheduleService.updateIotProjectTaskSchedule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目任务时间表/施工进度")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:delete')")
    public CommonResult<Boolean> deleteIotProjectTaskSchedule(@RequestParam("id") Long id) {
        iotProjectTaskScheduleService.deleteIotProjectTaskSchedule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目任务时间表/施工进度")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:query')")
    public CommonResult<IotProjectTaskScheduleRespVO> getIotProjectTaskSchedule(@RequestParam("id") Long id) {
        IotProjectTaskScheduleDO iotProjectTaskSchedule = iotProjectTaskScheduleService.getIotProjectTaskSchedule(id);
        return success(BeanUtils.toBean(iotProjectTaskSchedule, IotProjectTaskScheduleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目任务时间表/施工进度分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:query')")
    public CommonResult<PageResult<IotProjectTaskScheduleRespVO>> getIotProjectTaskSchedulePage(@Valid IotProjectTaskSchedulePageReqVO pageReqVO) {
        PageResult<IotProjectTaskScheduleDO> pageResult = iotProjectTaskScheduleService.getIotProjectTaskSchedulePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotProjectTaskScheduleRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得项目任务时间表/施工进度列表")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:query')")
    public CommonResult<List<IotProjectTaskScheduleRespVO>> getIotProjectTaskSchedules(@Valid IotProjectTaskSchedulePageReqVO pageReqVO) {
        List<IotProjectTaskScheduleDO> projectTaskSchedules = iotProjectTaskScheduleService.getIotProjectTaskSchedules(pageReqVO);
        return success(BeanUtils.toBean(projectTaskSchedules, IotProjectTaskScheduleRespVO.class));
    }

    @GetMapping("/scheduleExtProperties")
    @Operation(summary = "获得项目任务计划对应的扩展属性列表")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:query')")
    public CommonResult<List<IotTaskAttrModelProperty>> scheduleExtProperties(@Valid IotProjectTaskSchedulePageReqVO pageReqVO) {
        List<IotTaskAttrModelProperty> projectTaskSchedules = iotProjectTaskScheduleService.scheduleExtProperties(pageReqVO);
        return success(projectTaskSchedules);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目任务时间表/施工进度 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-schedule:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotProjectTaskScheduleExcel(@Valid IotProjectTaskSchedulePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotProjectTaskScheduleDO> list = iotProjectTaskScheduleService.getIotProjectTaskSchedulePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目任务时间表/施工进度.xls", "数据", IotProjectTaskScheduleRespVO.class,
                        BeanUtils.toBean(list, IotProjectTaskScheduleRespVO.class));
    }

}