package com.aucma.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.aucma.common.utils.DateUtils;
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.aucma.common.annotation.Log;
import com.aucma.common.core.controller.BaseController;
import com.aucma.common.core.domain.AjaxResult;
import com.aucma.common.enums.BusinessType;
import com.aucma.base.domain.BaseDeviceLedger;
import com.aucma.base.service.IBaseDeviceLedgerService;
import com.aucma.common.utils.poi.ExcelUtil;
import com.aucma.common.core.page.TableDataInfo;

/**
 * 设备台账Controller
 *
 * @author Yinq
 * @date 2023-09-19
 */
@RestController
@RequestMapping("/base/deviceLedger")
public class BaseDeviceLedgerController extends BaseController {
    @Autowired
    private IBaseDeviceLedgerService baseDeviceLedgerService;

    /**
     * 查询设备台账列表
     */
    @PreAuthorize("@ss.hasPermi('base:deviceLedger:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseDeviceLedger baseDeviceLedger) {
        startPage();
        List<BaseDeviceLedger> list = baseDeviceLedgerService.selectBaseDeviceLedgerList(baseDeviceLedger);
        return getDataTable(list);
    }

    @GetMapping("/getDeviceLedgerList")
    public AjaxResult getDeviceLedgerList(BaseDeviceLedger baseDeviceLedger) {
        List<BaseDeviceLedger> list = baseDeviceLedgerService.selectBaseDeviceLedgerList(baseDeviceLedger);
        return success(list);
    }

    /**
     * 导出设备台账列表
     */
    @PreAuthorize("@ss.hasPermi('base:deviceLedger:export')")
    @Log(title = "设备台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseDeviceLedger baseDeviceLedger) {
        List<BaseDeviceLedger> list = baseDeviceLedgerService.selectBaseDeviceLedgerList(baseDeviceLedger);
        ExcelUtil<BaseDeviceLedger> util = new ExcelUtil<BaseDeviceLedger>(BaseDeviceLedger.class);
        util.exportExcel(response, list, "设备台账数据");
    }

    /**
     * 获取设备台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:deviceLedger:query')")
    @GetMapping(value = "/{objId}")
    public AjaxResult getInfo(@PathVariable("objId") Long objId) {
        return success(baseDeviceLedgerService.selectBaseDeviceLedgerByObjId(objId));
    }

    /**
     * 新增设备台账
     */
    @PreAuthorize("@ss.hasPermi('base:deviceLedger:add')")
    @Log(title = "设备台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseDeviceLedger baseDeviceLedger) {
        baseDeviceLedger.setCreatedBy(getUsername());
        baseDeviceLedger.setCreatedTime(DateUtils.getNowDate());
        return toAjax(baseDeviceLedgerService.insertBaseDeviceLedger(baseDeviceLedger));
    }

    /**
     * 修改设备台账
     */
    @PreAuthorize("@ss.hasPermi('base:deviceLedger:edit')")
    @Log(title = "设备台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseDeviceLedger baseDeviceLedger) {
        baseDeviceLedger.setUpdatedBy(getUsername());
        baseDeviceLedger.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(baseDeviceLedgerService.updateBaseDeviceLedger(baseDeviceLedger));
    }

    /**
     * 删除设备台账
     */
    @PreAuthorize("@ss.hasPermi('base:deviceLedger:remove')")
    @Log(title = "设备台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{objIds}")
    public AjaxResult remove(@PathVariable Long[] objIds) {
        return toAjax(baseDeviceLedgerService.deleteBaseDeviceLedgerByObjIds(objIds));
    }

    /**
     * 获取设备编号
     * @return
     */
    @GetMapping("/getDeviceCode")
    public AjaxResult getDeviceCode() {
        return success(baseDeviceLedgerService.getDeviceCode());
    }
}
