package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.OrganizationCourseDurationSettings;
import com.ym.mec.biz.dal.page.OrganizationDurationSettingsQueryInfo;
import com.ym.mec.biz.service.OrganizationCourseDurationSettingsService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RequestMapping("organizationCourseDurationSettings")
@Api(tags = "分部课程时长设置服务")
@RestController
public class OrganizationCourseDurationSettingsController extends BaseController {

    @Autowired
    private OrganizationCourseDurationSettingsService organizationCourseDurationSettingsService;

    @ApiOperation(value = "分页查询列表")
    @GetMapping(value = "/queryPage")
    @PreAuthorize("@pcs.hasPermissions('organizationCourseDurationSettings/queryPage')")
    public HttpResponseResult<PageInfo<OrganizationCourseDurationSettings>> queryPage(OrganizationDurationSettingsQueryInfo queryInfo) {
        return succeed(organizationCourseDurationSettingsService.queryPage(queryInfo));
    }

    @ApiOperation(value = "新增分部课程时长")
    @PostMapping(value = "/insert")
    @PreAuthorize("@pcs.hasPermissions('organizationCourseDurationSettings/insert')")
    public HttpResponseResult<OrganizationCourseDurationSettings> insert(OrganizationCourseDurationSettings organizationCourseDurationSettings) {
        OrganizationCourseDurationSettings originalObj = organizationCourseDurationSettingsService.queryByOrganIdAndCourseType(
                organizationCourseDurationSettings.getOrganId(), organizationCourseDurationSettings.getCourseType());
        if (originalObj != null) {
            return failed("当前分部已经存在该课程类型的课程时长设置");
        }
        organizationCourseDurationSettingsService.insert(organizationCourseDurationSettings);
        return succeed(organizationCourseDurationSettings);
    }

    @ApiOperation(value = "修改分部课程时长设置")
    @PostMapping(value = "/update")
    @PreAuthorize("@pcs.hasPermissions('organizationCourseDurationSettings/update')")
    public HttpResponseResult<OrganizationCourseDurationSettings> update(OrganizationCourseDurationSettings organizationCourseDurationSettings) {
        organizationCourseDurationSettings.setUpdateTime(new Date());
        organizationCourseDurationSettingsService.update(organizationCourseDurationSettings);
        return succeed(organizationCourseDurationSettings);
    }

    @ApiOperation(value = "删除分部课程类型时长设置")
    @PostMapping("/del/{id}")
    @PreAuthorize("@pcs.hasPermissions('organizationCourseDurationSettings/del')")
    public Object del(@ApiParam(value = "编号", required = true) @PathVariable("id") Integer id) {
        organizationCourseDurationSettingsService.delete(id);
        return succeed();
    }

    @ApiOperation(value = "获取分部对应类型课程的课程时长")
    @GetMapping(value = "/queryByOrganIdAndCourseType")
    @PreAuthorize("@pcs.hasPermissions('organizationCourseDurationSettings/queryByOrganIdAndCourseType')")
    public HttpResponseResult<OrganizationCourseDurationSettings> queryByOrganIdAndCourseType(Integer organId, CourseSchedule.CourseScheduleType courseScheduleType) {
        return succeed(organizationCourseDurationSettingsService.queryByOrganIdAndCourseType(organId, courseScheduleType));
    }

    @ApiOperation(value = "获取分部课程的课程时长")
    @GetMapping(value = "/queryByOrganId")
    @PreAuthorize("@pcs.hasPermissions('organizationCourseDurationSettings/queryByOrganId')")
    public HttpResponseResult<Map<String,String>> queryByOrganId(Integer organId) {
        return succeed(organizationCourseDurationSettingsService.queryByOrganId(organId));
    }

}
