package com.ruoyi.ems.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ems.domain.AreaElectricityAttr;
import com.ruoyi.ems.service.IAreaElectricityAttrService;
import com.ruoyi.ems.service.IElecValencyConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 用电业务Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/basecfg/electricity")
public class ElectricityController extends BaseController {
    /**
     * 用电属性服务
     */
    @Autowired
    private IAreaElectricityAttrService attrService;

    @Autowired
    private IElecValencyConfigService configService;

    /**
     * 查询服务区用电属性列表
     */
    @RequiresPermissions("basecfg:electricity:list")
    @GetMapping("/attr/list")
    public TableDataInfo list(AreaElectricityAttr areaElectricityAttr) {
        startPage();
        List<AreaElectricityAttr> list = attrService.selectAreaElectricityAttrList(areaElectricityAttr);
        return getDataTable(list);
    }

    /**
     * 导出服务区用电属性列表
     */
    @RequiresPermissions("basecfg:electricity:export")
    @Log(title = "服务区用电属性", businessType = BusinessType.EXPORT)
    @PostMapping("/attr/export")
    public void export(HttpServletResponse response, AreaElectricityAttr areaElectricityAttr) {
        List<AreaElectricityAttr> list = attrService.selectAreaElectricityAttrList(areaElectricityAttr);
        ExcelUtil<AreaElectricityAttr> util = new ExcelUtil<AreaElectricityAttr>(AreaElectricityAttr.class);
        util.exportExcel(response, list, "服务区用电属性数据");
    }

    /**
     * 获取服务区用电属性详细信息
     */
    @RequiresPermissions("basecfg:electricity:query")
    @GetMapping(value = "/attr/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(attrService.selectAreaElectricityAttrById(id));
    }

    /**
     * 新增服务区用电属性
     */
    @RequiresPermissions("basecfg:electricity:add")
    @Log(title = "服务区用电属性", businessType = BusinessType.INSERT)
    @PostMapping(value = "/attr")
    public AjaxResult add(@RequestBody AreaElectricityAttr areaElectricityAttr) {
        return toAjax(attrService.insertAreaElectricityAttr(areaElectricityAttr));
    }

    /**
     * 修改服务区用电属性
     */
    @RequiresPermissions("basecfg:electricity:edit")
    @Log(title = "服务区用电属性", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/attr")
    public AjaxResult edit(@RequestBody AreaElectricityAttr areaElectricityAttr) {
        return toAjax(attrService.updateAreaElectricityAttr(areaElectricityAttr));
    }

    /**
     * 删除服务区用电属性
     */
    @RequiresPermissions("basecfg:electricity:remove")
    @Log(title = "服务区用电属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/attr/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(attrService.deleteAreaElectricityAttrByIds(ids));
    }
}
