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.ElecValencyConfig;
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/elecvalency")
public class ElecValencyController extends BaseController {
    @Autowired
    private IElecValencyConfigService configService;

    /**
     * 查询电价配置列表
     */
    @RequiresPermissions("basecfg:electricity:list")
    @GetMapping("/list")
    public TableDataInfo list(ElecValencyConfig elecValencyConfig) {
        startPage();
        List<ElecValencyConfig> list = configService.selectElecValencyConfigList(elecValencyConfig);
        return getDataTable(list);
    }

    /**
     * 导出电价配置列表
     */
    @RequiresPermissions("basecfg:electricity:export")
    @Log(title = "电价配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElecValencyConfig elecValencyConfig) {
        List<ElecValencyConfig> list = configService.selectElecValencyConfigList(elecValencyConfig);
        ExcelUtil<ElecValencyConfig> util = new ExcelUtil<>(ElecValencyConfig.class);
        util.exportExcel(response, list, "电价配置数据");
    }

    /**
     * 获取电价配置详细信息
     */
    @RequiresPermissions("basecfg:electricity:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(configService.selectElecValencyConfigById(id));
    }

    /**
     * 新增电价配置
     */
    @RequiresPermissions("basecfg:electricity:add")
    @Log(title = "电价配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElecValencyConfig elecValencyConfig) {
        return toAjax(configService.insertElecValencyConfig(elecValencyConfig));
    }

    /**
     * 修改电价配置
     */
    @RequiresPermissions("basecfg:electricity:edit")
    @Log(title = "电价配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElecValencyConfig elecValencyConfig) {
        return toAjax(configService.updateElecValencyConfig(elecValencyConfig));
    }

    /**
     * 删除电价配置
     */
    @RequiresPermissions("basecfg:electricity:remove")
    @Log(title = "电价配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(configService.deleteElecValencyConfigByIds(ids));
    }

    /**
     * 列出价格列表
     * @return 价格列表
     */
    @GetMapping("/listall")
    public AjaxResult listAll() {
        List<ElecValencyConfig> list = configService.selectElecValencyConfigList(new ElecValencyConfig());
        return success(list);
    }
}
