package com.willalp.web.controller.control;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
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.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.enums.BusinessType;
import com.willalp.control.domain.HsControlTiming;
import com.willalp.control.service.IHsControlTimingService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

/**
 * 设备定时任务Controller
 *
 * @author willalp
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/control/timing")
public class HsControlTimingController extends BaseController
{
    @Autowired
    private IHsControlTimingService hsControlTimingService;

    /**
     * 查询设备定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('control:timing:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsControlTiming hsControlTiming)
    {
        startPage();
        List<HsControlTiming> list = hsControlTimingService.selectHsControlTimingList(hsControlTiming);
        return getDataTable(list);
    }

    /**
     * 导出设备定时任务列表
     */
    @PreAuthorize("@ss.hasPermi('control:timing:export')")
    @Log(title = "设备定时任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsControlTiming hsControlTiming)
    {
        List<HsControlTiming> list = hsControlTimingService.selectHsControlTimingList(hsControlTiming);
        ExcelUtil<HsControlTiming> util = new ExcelUtil<HsControlTiming>(HsControlTiming.class);
        return util.exportExcel(list, "设备定时任务数据");
    }

    /**
     * 获取设备定时任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('control:timing:query')")
    @GetMapping(value = "/{timingId}")
    public AjaxResult getInfo(@PathVariable("timingId") Long timingId)
    {
        return AjaxResult.success(hsControlTimingService.getById(timingId));
    }

    /**
     * 新增设备定时任务
     */
    @PreAuthorize("@ss.hasPermi('control:timing:add')")
    @Log(title = "设备定时任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsControlTiming hsControlTiming)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsControlTiming byId = hsControlTimingService.getById(idStr);
            if (null == byId) {
                hsControlTiming.setTimingId(Long.valueOf(idStr));
                break;
            }
        }
        hsControlTiming.setCreateBy(SecurityUtils.getUsername());
        hsControlTiming.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsControlTimingService.save(hsControlTiming));
    }

    /**
     * 修改设备定时任务
     */
    @PreAuthorize("@ss.hasPermi('control:timing:edit')")
    @Log(title = "设备定时任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsControlTiming hsControlTiming)
    {
        hsControlTiming.setUpdateBy(SecurityUtils.getUsername());
        hsControlTiming.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsControlTimingService.updateById(hsControlTiming));
    }

    /**
     * 删除设备定时任务
     */
    @PreAuthorize("@ss.hasPermi('control:timing:remove')")
    @Log(title = "设备定时任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{timingIds}")
    public AjaxResult remove(@PathVariable Long[] timingIds)
    {
        return toAjax(hsControlTimingService.removeByIds(Arrays.asList(timingIds)));
    }
}
