package com.rongwei.bsserver.sys.controller;

import com.rongwei.bscommon.sys.service.MaintenanceService;
import com.rongwei.bscommon.sys.utils.ExceptionUtils;
import com.rongwei.bsentity.dto.BatchCreateDTO;
import com.rongwei.bsentity.dto.PlanYearCopyDTO;
import com.rongwei.bsentity.dto.TaskCreatorDTO;
import com.rongwei.rwcommon.base.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author shangmi
 * @title MaintenanceController
 * @date 2023/11/24 16:45
 * @description 保养检修
 */
@RestController
@RequestMapping("maintenance")
public class MaintenanceController {

    private final static Logger log = LoggerFactory.getLogger(MaintenanceController.class);

    @Autowired
    private MaintenanceService maintenanceService;

    /**
     * 发布任务
     *
     * @param taskCreator 入参
     * @return {@link R}
     * @date 2023/11/27 16:25
     * @author shangmi
     */
    @PostMapping("saveTask")
    public R saveTask(@RequestBody TaskCreatorDTO taskCreator){
        try {
            log.info("进入接口：/maintenance/saveTask,入参：{}",taskCreator);
            return maintenanceService.saveTask(taskCreator);
        }catch (Exception e){
            ExceptionUtils.printExceptionDetail(e,"生成任务失败，请检查传入参数");
            return R.error(e.getMessage());
        }
    }

    /**
     * 批量生成保养计划
     *
     * @param batchCreate
     * @return {@link R}
     * @date 2023/12/26 10:39
     * @author shangmi
     *
     */

    @PostMapping("batchSavePlans")
    public R batchSavePlans(@RequestBody BatchCreateDTO batchCreate){
        try {
            log.info("进入接口：/maintenance/batchSavePlans,入参：{}",batchCreate);
            return maintenanceService.batchSavePlans(batchCreate);
        }catch (Exception e){
            ExceptionUtils.printExceptionDetail(e,"批量生成保养计划失败，请检查传入参数，问题为：");
            return R.error(e.getMessage());
        }

    }
    /**
     * 从10号开始 往后每天对计划中的任务进行识别 并生成任务
     *
     * @param
     * @return {@link R}
     * @date 2023/12/26 10:38
     * @author shangmi
     *
     */

    @Scheduled(cron = "0 0 22 28/1 * *")
    @PostMapping("batchSavePlansTiming")
    public R batchSavePlansTiming(){
        try {
            log.info("进入接口：/maintenance/batchSavePlansTiming,入参：无");
            return maintenanceService.batchSavePlansTiming();
        }catch (Exception e){
            ExceptionUtils.printExceptionDetail(e,"定时生成任务失败，请检查传入参数，问题为：");
            return R.error(e.getMessage());
        }
    }

    /**
     * 生成年度任务
     *
     * @param planYearCopy
     * @return {@link R}
     * @date 2024/2/5 10:36
     * @author shangmi
     *
     */

    @PostMapping("saveYearByTask")
    public R saveYearByTask(@RequestBody PlanYearCopyDTO planYearCopy){
        try {
            log.info("进入接口：/maintenance/saveYearByTask,入参:{}",planYearCopy);
            return maintenanceService.saveYearByTask(planYearCopy);
        }catch (Exception e){
            ExceptionUtils.printExceptionDetail(e,"生成年度任务失败，请检查传入参数，为题为：");
            return R.error(e.getMessage());
        }
    }

    /**
     * 附件导入
     *
     * @param multipartFile
     * @return {@link R}
     * @date 2024/2/5 10:35
     * @author shangmi
     *
     */

    @PostMapping("importStandardSave")
    public R uploadStandardSave(@RequestParam("file") MultipartFile multipartFile){
        try {
            log.info("进入接口：/maintenance/importStandardSave,入参:{}", multipartFile);
            return maintenanceService.uploadStandardSave(multipartFile);
            // return null;
        }catch (Exception e){
            ExceptionUtils.printExceptionDetail(e,"导入失败，请检查后再导入:");
            return R.error(e.getMessage());
        }
    }
}
