package com.ruoyi.web.controller.education;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.education.domain.EduSemester;
import com.ruoyi.education.service.IEduSemesterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;

/**
 * 学期信息Controller
 * 
 * @author ruoyi
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/education/semester")
public class EduSemesterController extends BaseController
{
    @Autowired
    private IEduSemesterService eduSemesterService;

    /**
     * 查询学期信息列表
     */
    @PreAuthorize("@ss.hasPermi('education:semester:list')")
    @GetMapping("/list")
    public TableDataInfo list(EduSemester eduSemester)
    {
        startPage();
        List<EduSemester> list = eduSemesterService.selectEduSemesterList(eduSemester);
        return getDataTable(list);
    }

    /**
     * 导出学期信息列表
     */
    @PreAuthorize("@ss.hasPermi('education:semester:export')")
    @Log(title = "学期信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduSemester eduSemester)
    {
        List<EduSemester> list = eduSemesterService.selectEduSemesterList(eduSemester);
        ExcelUtil<EduSemester> util = new ExcelUtil<EduSemester>(EduSemester.class);
        util.exportExcel(response, list, "学期信息数据");
    }

    /**
     * 获取学期信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('education:semester:query')")
    @GetMapping(value = "/{semesterId}")
    public AjaxResult getInfo(@PathVariable("semesterId") Long semesterId)
    {
        return success(eduSemesterService.selectEduSemesterBySemesterId(semesterId));
    }

    /**
     * 新增学期信息
     */
    @PreAuthorize("@ss.hasPermi('education:semester:add')")
    @Log(title = "学期信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduSemester eduSemester)
    {
        if ("NOT_UNIQUE".equals(eduSemesterService.checkSemesterCodeUnique(eduSemester)))
        {
            return error("新增学期'" + eduSemester.getSemesterName() + "'失败，学期编码已存在");
        }
        
        String timeConflictResult = eduSemesterService.checkSemesterTimeConflict(eduSemester);
        if ("CONFLICT".equals(timeConflictResult))
        {
            return error("新增学期'" + eduSemester.getSemesterName() + "'失败，教学时间与其他学期冲突");
        }
        else if ("ENROLL_CONFLICT".equals(timeConflictResult))
        {
            return error("新增学期'" + eduSemester.getSemesterName() + "'失败，选课时间与其他学期冲突");
        }
        
        eduSemester.setCreateBy(getUsername());
        return toAjax(eduSemesterService.insertEduSemester(eduSemester));
    }

    /**
     * 修改学期信息
     */
    @PreAuthorize("@ss.hasPermi('education:semester:edit')")
    @Log(title = "学期信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduSemester eduSemester)
    {
        if ("NOT_UNIQUE".equals(eduSemesterService.checkSemesterCodeUnique(eduSemester)))
        {
            return error("修改学期'" + eduSemester.getSemesterName() + "'失败，学期编码已存在");
        }
        
        String timeConflictResult = eduSemesterService.checkSemesterTimeConflict(eduSemester);
        if ("CONFLICT".equals(timeConflictResult))
        {
            return error("修改学期'" + eduSemester.getSemesterName() + "'失败，教学时间与其他学期冲突");
        }
        else if ("ENROLL_CONFLICT".equals(timeConflictResult))
        {
            return error("修改学期'" + eduSemester.getSemesterName() + "'失败，选课时间与其他学期冲突");
        }
        
        eduSemester.setUpdateBy(getUsername());
        return toAjax(eduSemesterService.updateEduSemester(eduSemester));
    }

    /**
     * 删除学期信息
     */
    @PreAuthorize("@ss.hasPermi('education:semester:remove')")
    @Log(title = "学期信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{semesterIds}")
    public AjaxResult remove(@PathVariable Long[] semesterIds)
    {
        return toAjax(eduSemesterService.deleteEduSemesterBySemesterIds(semesterIds));
    }

    /**
     * 发布学期
     */
    @PreAuthorize("@ss.hasPermi('education:semester:publish')")
    @Log(title = "发布学期", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{semesterId}")
    public AjaxResult publishSemester(@PathVariable Long semesterId)
    {
        EduSemester semester = eduSemesterService.selectEduSemesterBySemesterId(semesterId);
        if (StringUtils.isNull(semester))
        {
            return error("学期信息不存在");
        }
        
        if (!"0".equals(semester.getStatus()))
        {
            return error("只有草稿状态的学期才能发布");
        }
        
        return toAjax(eduSemesterService.publishSemester(semesterId));
    }

    /**
     * 归档学期
     */
    @PreAuthorize("@ss.hasPermi('education:semester:archive')")
    @Log(title = "归档学期", businessType = BusinessType.UPDATE)
    @PutMapping("/archive/{semesterId}")
    public AjaxResult archiveSemester(@PathVariable Long semesterId)
    {
        EduSemester semester = eduSemesterService.selectEduSemesterBySemesterId(semesterId);
        if (StringUtils.isNull(semester))
        {
            return error("学期信息不存在");
        }
        
        if (!"1".equals(semester.getStatus()))
        {
            return error("只有进行中状态的学期才能归档");
        }
        
        return toAjax(eduSemesterService.archiveSemester(semesterId));
    }

    /**
     * 获取当前学期
     */
    @PreAuthorize("@ss.hasPermi('education:semester:query')")
    @GetMapping("/current")
    public AjaxResult getCurrentSemester()
    {
        return success(eduSemesterService.selectCurrentSemester());
    }

    /**
     * 获取可选课学期列表
     */
    @PreAuthorize("@ss.hasPermi('education:semester:query')")
    @GetMapping("/enrollable")
    public AjaxResult getEnrollableSemesters()
    {
        return success(eduSemesterService.selectEnrollableSemesters());
    }
}