package com.casic.missiles.controller.workbench;

import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.workbench.CalendarScheduleDeleteRequest;
import com.casic.missiles.dto.workbench.CalendarScheduleListRequest;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.workbench.WorkbenchCalendarSchedule;
import com.casic.missiles.service.workbench.IWorkbenchCalendarScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工作台-日历日程安排表 前端控制器
 * </p>
 *
 * @author wangpeng
 * @since 2023-06-16
 */
@Api(tags = "工作台模块-日历日程")
@RestController
@RequestMapping("/workbench/calendarSchedule")
public class WorkbenchCalendarScheduleController {
    @Autowired
    private IWorkbenchCalendarScheduleService calendarScheduleService;

    @ApiOperation("日程列表")
    @PostMapping("/list")
    public ReturnDTO<Map<String, List<WorkbenchCalendarSchedule>>> calendarScheduleList(@RequestBody @Valid CalendarScheduleListRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(calendarScheduleService.calendarScheduleList(request));
    }

    @ApiOperation("日程新增（批量）")
    @PostMapping("/add")
    @ResponseBody
    public ReturnDTO add(@RequestBody @Valid List<WorkbenchCalendarSchedule> calendarScheduleList, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return calendarScheduleService.calendarScheduleAdd(calendarScheduleList);
    }

    @ApiOperation("日程编辑")
    @PostMapping("/update")
    @ResponseBody
    public ReturnDTO update(@RequestBody @Valid List<WorkbenchCalendarSchedule> calendarScheduleList, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return calendarScheduleService.updateCalendarSchedule(calendarScheduleList);
    }

    @ApiOperation("日程删除")
    @PostMapping("/delete")
    @ResponseBody
    public ReturnDTO delete(@RequestBody @Valid CalendarScheduleDeleteRequest request, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return calendarScheduleService.deleteCalendarSchedule(request);
    }
}
