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.HsControlDeviceInfo;
import com.willalp.control.service.IHsControlDeviceInfoService;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.common.core.page.TableDataInfo;

/**
 * 控制设备状态信息Controller
 *
 * @author willalp
 * @date 2022-02-16
 */
@RestController
@RequestMapping("/control/info")
public class HsControlDeviceInfoController extends BaseController
{
    @Autowired
    private IHsControlDeviceInfoService hsControlDeviceInfoService;

    /**
     * 查询控制设备状态信息列表
     */
    @PreAuthorize("@ss.hasPermi('control:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsControlDeviceInfo hsControlDeviceInfo)
    {
        startPage();
        List<HsControlDeviceInfo> list = hsControlDeviceInfoService.selectHsControlDeviceInfoList(hsControlDeviceInfo);
        return getDataTable(list);
    }

    /**
     * 导出控制设备状态信息列表
     */
    @PreAuthorize("@ss.hasPermi('control:info:export')")
    @Log(title = "控制设备状态信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsControlDeviceInfo hsControlDeviceInfo)
    {
        List<HsControlDeviceInfo> list = hsControlDeviceInfoService.selectHsControlDeviceInfoList(hsControlDeviceInfo);
        ExcelUtil<HsControlDeviceInfo> util = new ExcelUtil<HsControlDeviceInfo>(HsControlDeviceInfo.class);
        return util.exportExcel(list, "控制设备状态信息数据");
    }

    /**
     * 获取控制设备状态信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('control:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hsControlDeviceInfoService.getById(id));
    }

    /**
     * 新增控制设备状态信息
     */
    @PreAuthorize("@ss.hasPermi('control:info:add')")
    @Log(title = "控制设备状态信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsControlDeviceInfo hsControlDeviceInfo)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsControlDeviceInfo byId = hsControlDeviceInfoService.getById(idStr);
            if (null == byId) {
                hsControlDeviceInfo.setId(Long.valueOf(idStr));
                break;
            }
        }
        hsControlDeviceInfo.setCreateBy(SecurityUtils.getUsername());
        hsControlDeviceInfo.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsControlDeviceInfoService.save(hsControlDeviceInfo));
    }

    /**
     * 修改控制设备状态信息
     */
    @PreAuthorize("@ss.hasPermi('control:info:edit')")
    @Log(title = "控制设备状态信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsControlDeviceInfo hsControlDeviceInfo)
    {
        hsControlDeviceInfo.setUpdateBy(SecurityUtils.getUsername());
        hsControlDeviceInfo.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsControlDeviceInfoService.updateById(hsControlDeviceInfo));
    }

    /**
     * 删除控制设备状态信息
     */
    @PreAuthorize("@ss.hasPermi('control:info:remove')")
    @Log(title = "控制设备状态信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hsControlDeviceInfoService.removeByIds(Arrays.asList(ids)));
    }
}
