package com.ruoyi.ems.controller;

import com.huashe.common.domain.AjaxResult;
import com.huashe.common.exception.BusinessException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
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.ems.domain.EmsObjAbility;
import com.ruoyi.ems.model.AbilityPayload;
import com.ruoyi.ems.service.IAbilityCallService;
import com.ruoyi.ems.service.IEmsObjAbilityService;
import io.swagger.annotations.Api;
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-09-23
 */
@RestController
@RequestMapping("/object/ability")
@Api(value = "EmsObjAbilityController", description = "能源对象能力管理")
public class EmsObjAbilityController extends BaseController {
    @Autowired
    private IEmsObjAbilityService abilityService;

    @Autowired
    private IAbilityCallService abilityCallService;

    /**
     * 查询能源对象能力列表
     */
    @PostMapping("/call")
    public AjaxResult callAbility(@RequestBody AbilityPayload abilityPayload) {
        AjaxResult ajaxResult = null;

        try {
            String ret = abilityCallService.devAbilityCall(abilityPayload);
            ajaxResult = success(ret);
        }
        catch (BusinessException e) {
            ajaxResult = error(e.getMessage());
        }
        catch (Exception e) {
            ajaxResult = error("内部错误!");
        }

        return ajaxResult;
    }

    /**
     * 查询能源对象能力列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmsObjAbility objAbility) {
        startPage();
        List<EmsObjAbility> list = abilityService.selectObjAbilityList(objAbility);
        return getDataTable(list);
    }

    /**
     * 导出能源对象能力列表
     */
    @Log(title = "能源对象能力", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsObjAbility objAbility) {
        List<EmsObjAbility> list = abilityService.selectObjAbilityList(objAbility);
        ExcelUtil<EmsObjAbility> util = new ExcelUtil<>(EmsObjAbility.class);
        util.exportExcel(response, list, "能源对象能力数据");
    }

    /**
     * 获取能源对象能力详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(abilityService.selectObjAbilityById(id));
    }

    /**
     * 新增能源对象能力
     */
    @Log(title = "能源对象能力", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsObjAbility objAbility) {
        return toAjax(abilityService.insertObjAbility(objAbility));
    }

    /**
     * 新增能源对象能力
     */
    @Log(title = "能源对象能力", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<EmsObjAbility> list) {
        return toAjax(abilityService.insertBatch(list));
    }

    /**
     * 修改能源对象能力
     */
    @Log(title = "能源对象能力", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsObjAbility objAbility) {
        return toAjax(abilityService.updateObjAbility(objAbility));
    }

    /**
     * 删除能源对象能力
     */
    @Log(title = "能源对象能力", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(abilityService.deleteObjAbilityByIds(ids));
    }
}
