package com.aucma.base.controller;

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

import com.aucma.base.domain.TreeSelects;
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.BaseMonitorInfo;
import com.aucma.base.service.IBaseMonitorInfoService;
import com.aucma.common.utils.poi.ExcelUtil;

/**
 * 计量设备信息Controller
 *
 * @author Yinq
 * @date 2023-09-21
 */
@RestController
@RequestMapping("/base/monitorInfo")
public class BaseMonitorInfoController extends BaseController {
    @Autowired
    private IBaseMonitorInfoService baseMonitorInfoService;

    /**
     * 查询计量设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:monitorInfo:list')")
    @GetMapping("/list")
    public AjaxResult list(BaseMonitorInfo baseMonitorInfo) {
        List<BaseMonitorInfo> list = baseMonitorInfoService.selectBaseMonitorInfoList(baseMonitorInfo);
        return success(list);
    }
    /**
     * 查询计量设备信息下拉树结构
     * */
    @PostMapping("/monitorInfoTree")
    public AjaxResult getMonitorInfoTree(BaseMonitorInfo baseMonitorInfo) {
        List<TreeSelects> list = baseMonitorInfoService.selectBaseMonitorInfoTreeList(baseMonitorInfo);
        return success(list);
    }

    /**
     * 导出计量设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:monitorInfo:export')")
    @Log(title = "计量设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseMonitorInfo baseMonitorInfo) {
        List<BaseMonitorInfo> list = baseMonitorInfoService.selectBaseMonitorInfoList(baseMonitorInfo);
        ExcelUtil<BaseMonitorInfo> util = new ExcelUtil<BaseMonitorInfo>(BaseMonitorInfo.class);
        util.exportExcel(response, list, "计量设备信息数据");
    }

    /**
     * 获取计量设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:monitorInfo:query')")
    @GetMapping(value = "/{objId}")
    public AjaxResult getInfo(@PathVariable("objId") Long objId) {
        return success(baseMonitorInfoService.selectBaseMonitorInfoByObjId(objId));
    }

    /**
     * 新增计量设备信息
     */
    @PreAuthorize("@ss.hasPermi('base:monitorInfo:add')")
    @Log(title = "计量设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseMonitorInfo baseMonitorInfo) {
        baseMonitorInfo.setCreatedBy(getUsername());
        baseMonitorInfo.setCreatedTime(DateUtils.getNowDate());
        return toAjax(baseMonitorInfoService.insertBaseMonitorInfo(baseMonitorInfo));
    }

    /**
     * 修改计量设备信息
     */
    @PreAuthorize("@ss.hasPermi('base:monitorInfo:edit')")
    @Log(title = "计量设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseMonitorInfo baseMonitorInfo) {
        baseMonitorInfo.setUpdatedBy(getUsername());
        baseMonitorInfo.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(baseMonitorInfoService.updateBaseMonitorInfo(baseMonitorInfo));
    }

    /**
     * 删除计量设备信息
     */
    @PreAuthorize("@ss.hasPermi('base:monitorInfo:remove')")
    @Log(title = "计量设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{objIds}")
    public AjaxResult remove(@PathVariable Long[] objIds) {
        return toAjax(baseMonitorInfoService.deleteBaseMonitorInfoByObjIds(objIds));
    }

    //根据名字模糊查询
    @GetMapping("/like/{monitorName}")
    public AjaxResult like(@PathVariable("monitorName")String monitorName){
        if (monitorName.isEmpty()){

            return null;
        }
        return success(baseMonitorInfoService.selectBaseMonitorInfoByNameLike(monitorName));
    }
}
