package com.ruoyi.app.mendian;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.OperatingHours;
import com.ruoyi.system.service.IOperatingHoursService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * OperatingHoursController
 * 
 * @author ruoyi
 * @date 2025-02-11
 */
@RestController
@RequestMapping("/hours")
public class OperatingHoursController extends BaseController
{
    @Autowired
    private IOperatingHoursService operatingHoursService;

    /**
     * 添加时间段
     * @param operatingHours
     * @return
     */
    @Anonymous
    @PostMapping("/addHours")
    public AjaxResult addHours(@RequestBody OperatingHours operatingHours){
        Long id = operatingHoursService.insertOperatingHoursReturnId(operatingHours);
        if (id > 0) {
            Map<String, Long> data = new HashMap<>();
            data.put("id", id);
            return success(MessageUtils.message("no.action.success"), data);
        }
        return toAjax(id.intValue());
    }
    /**
     * 修改时间段
     * @param operatingHours
     * @return
     */
    @Anonymous
    @PostMapping("/setHours")
    public AjaxResult setHours(@RequestBody OperatingHours operatingHours){
        return toAjax(operatingHoursService.updateOperatingHours(operatingHours));
    }

    /**
     * 删除时间段
     * @return
     */
    @Anonymous
    @GetMapping("/delHours")
    public AjaxResult delHours(@RequestParam Long id){
        return toAjax(operatingHoursService.deleteOperatingHoursById(id));
    }

    /**
     * 查询时间段
     * @param mdId
     * @return
     */
    @Anonymous
    @GetMapping("/getHours")
    public AjaxResult getHours(@RequestParam Long mdId){
        OperatingHours operatingHours = new OperatingHours();
        operatingHours.setMdId(mdId);
        List<OperatingHours> list = operatingHoursService.selectOperatingHoursList(operatingHours);
        return success(MessageUtils.message("no.obtained.success"),list);
    }

    /**
     * 查询OperatingHours列表
     */
    @PreAuthorize("@ss.hasPermi('system:hours:list')")
    @GetMapping("/list")
    public TableDataInfo list(OperatingHours operatingHours)
    {
        startPage();
        List<OperatingHours> list = operatingHoursService.selectOperatingHoursList(operatingHours);
        return getDataTable(list);
    }

    /**
     * 导出OperatingHours列表
     */
    @PreAuthorize("@ss.hasPermi('system:hours:export')")
    @Log(title = "OperatingHours", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperatingHours operatingHours)
    {
        List<OperatingHours> list = operatingHoursService.selectOperatingHoursList(operatingHours);
        ExcelUtil<OperatingHours> util = new ExcelUtil<OperatingHours>(OperatingHours.class);
        util.exportExcel(response, list, "OperatingHours数据");
    }

    /**
     * 获取OperatingHours详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:hours:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(operatingHoursService.selectOperatingHoursById(id));
    }

    /**
     * 新增OperatingHours
     */
    @PreAuthorize("@ss.hasPermi('system:hours:add')")
    @Log(title = "OperatingHours", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OperatingHours operatingHours)
    {
        return toAjax(operatingHoursService.insertOperatingHours(operatingHours));
    }

    /**
     * 修改OperatingHours
     */
    @PreAuthorize("@ss.hasPermi('system:hours:edit')")
    @Log(title = "OperatingHours", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OperatingHours operatingHours)
    {
        return toAjax(operatingHoursService.updateOperatingHours(operatingHours));
    }

    /**
     * 删除OperatingHours
     */
    @PreAuthorize("@ss.hasPermi('system:hours:remove')")
    @Log(title = "OperatingHours", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(operatingHoursService.deleteOperatingHoursByIds(ids));
    }
}
