package com.aegis.web.controller.emergency.duty;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.core.page.TableDataInfo;
import com.aegis.common.enums.BusinessType;
import com.aegis.emergency.duty.domain.DutyShift;
import com.aegis.emergency.duty.service.DutyShiftService;

/**
 * 值班排班Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/duty/shifts")
public class DutyShiftController extends BaseController {

    @Autowired
    private DutyShiftService dutyShiftService;

    /**
     * 查询排班列表
     */
    @PreAuthorize("@ss.hasPermi('duty:shift:list')")
    @GetMapping("/list")
    public TableDataInfo list(DutyShift condition) {
        startPage();
        List<DutyShift> list = dutyShiftService.listByCondition(condition);
        return getDataTable(list);
    }

    /**
     * 根据ID查询排班
     */
    @PreAuthorize("@ss.hasPermi('duty:shift:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        DutyShift shift = dutyShiftService.getById(id);
        if (shift == null) {
            return error("排班不存在或已删除");
        }
        return success(shift);
    }

    /**
     * 查询当前正在值班的排班
     */
    @GetMapping("/current")
    public AjaxResult getCurrentShift() {
        DutyShift currentShift = dutyShiftService.getCurrentShift();
        return success(currentShift);
    }

    /**
     * 新增排班
     */
    @PreAuthorize("@ss.hasPermi('duty:shift:add')")
    @Log(title = "值班排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DutyShift shift) {
        return toAjax(dutyShiftService.save(shift));
    }

    /**
     * 修改排班
     */
    @PreAuthorize("@ss.hasPermi('duty:shift:edit')")
    @Log(title = "值班排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DutyShift shift) {
        return toAjax(dutyShiftService.updateById(shift));
    }

    /**
     * 删除排班
     */
    @PreAuthorize("@ss.hasPermi('duty:shift:remove')")
    @Log(title = "值班排班", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        boolean allSuccess = true;
        for (Long id : ids) {
            allSuccess &= dutyShiftService.removeById(id);
        }
        return toAjax(allSuccess);
    }
}