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

/**
 * 设备使用统计Controller
 *
 * @author willalp
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/control/stat")
public class HsControlStatController extends BaseController
{
    @Autowired
    private IHsControlStatService hsControlStatService;

    /**
     * 查询设备使用统计列表
     */
    @PreAuthorize("@ss.hasPermi('control:stat:list')")
    @GetMapping("/list")
    public TableDataInfo list(HsControlStat hsControlStat)
    {
        startPage();
        List<HsControlStat> list = hsControlStatService.selectHsControlStatList(hsControlStat);
        return getDataTable(list);
    }

    /**
     * 导出设备使用统计列表
     */
    @PreAuthorize("@ss.hasPermi('control:stat:export')")
    @Log(title = "设备使用统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsControlStat hsControlStat)
    {
        List<HsControlStat> list = hsControlStatService.selectHsControlStatList(hsControlStat);
        ExcelUtil<HsControlStat> util = new ExcelUtil<HsControlStat>(HsControlStat.class);
        return util.exportExcel(list, "设备使用统计数据");
    }

    /**
     * 获取设备使用统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('control:stat:query')")
    @GetMapping(value = "/{statId}")
    public AjaxResult getInfo(@PathVariable("statId") String statId)
    {
        return AjaxResult.success(hsControlStatService.getById(statId));
    }

    /**
     * 新增设备使用统计
     */
    @PreAuthorize("@ss.hasPermi('control:stat:add')")
    @Log(title = "设备使用统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsControlStat hsControlStat)
    {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsControlStat byId = hsControlStatService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsControlStat.setStatId(idStr);
                break;
            }
        }
        hsControlStat.setCreateBy(SecurityUtils.getUsername());
        hsControlStat.setCreateTime(DateUtils.getNowDate());
        return toAjax(hsControlStatService.save(hsControlStat));
    }

    /**
     * 修改设备使用统计
     */
    @PreAuthorize("@ss.hasPermi('control:stat:edit')")
    @Log(title = "设备使用统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsControlStat hsControlStat)
    {
        hsControlStat.setUpdateBy(SecurityUtils.getUsername());
        hsControlStat.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsControlStatService.updateById(hsControlStat));
    }

    /**
     * 删除设备使用统计
     */
    @PreAuthorize("@ss.hasPermi('control:stat:remove')")
    @Log(title = "设备使用统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{statIds}")
    public AjaxResult remove(@PathVariable String[] statIds)
    {
        return toAjax(hsControlStatService.removeByIds(Arrays.asList(statIds)));
    }
}
