package com.jiayue.ipfcst.console.controller;

import com.jiayue.ipfcst.aop.SaveValidate;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.common.data.entity.InverterInfo;
import com.jiayue.ipfcst.console.service.InverterInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 逆变器restful接口
 *
 * @author tl
 * @version 3.0
 * @since 2020/6/28 10:12
 */
@RestController
@Slf4j
public class InverterInfoController {

    private final InverterInfoService inverterInfoService;

    @Autowired
    public InverterInfoController(InverterInfoService inverterInfoService) {
        this.inverterInfoService = inverterInfoService;
    }

    /**
     * 获取逆变器（分页）
     *
     * @return 执行结果
     */
    @GetMapping(value = {"inverterInfo/{page}/{size}/{sample}", "inverterInfo/{page}/{size}"})
    public ResponseVO getPageAll(@PathVariable("page") Integer page,
                             @PathVariable("size") Integer size,
                             @PathVariable(value = "sample", required = false) Boolean sample) {

        Page<InverterInfo> inverterPage = this.inverterInfoService.get(sample, page, size);
        return ResponseVO.success(inverterPage);
    }

    /**
     * 获取逆变器
     *
     * @return 执行结果
     */
    @GetMapping(value = {"inverterInfo"})
    public ResponseVO getAll() {
      List<InverterInfo> list = this.inverterInfoService.getAll();
      return ResponseVO.success(list);
    }

    /**
     * 保存逆变器信息
     *
     * @param inverterInfo 参数
     * @return 执行结果
     */
    @SaveValidate
    @PostMapping(value = "inverterInfo/")
    public ResponseVO saveInverter(@RequestBody InverterInfo inverterInfo) {
        try {
            inverterInfoService.save(inverterInfo);
            return ResponseVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 保存逆变器信息异常");
            return ResponseVO.fail();
        }

    }

    /**
     * 批量保存逆变器信息
     *
     * @param inverterInfo 参数
     * @return 执行结果
     */
    @SaveValidate
    @PostMapping(value = "inverterInfo/saveAll/{startValue}/{endValue}")
    public ResponseVO allSaveInverter(@RequestBody InverterInfo inverterInfo,@PathVariable("startValue") Integer startValue,@PathVariable("endValue") Integer endValue) {
        try {
          inverterInfoService.addALL(inverterInfo,startValue,endValue);
            return ResponseVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 保存逆变器信息异常");
            return ResponseVO.fail();
        }
    }


    /**
     * 更新逆变器信息
     *
     * @param inverterInfo 参数
     * @return 执行结果
     */
    @SaveValidate
    @PutMapping(value = "inverterInfo/")
    public ResponseVO updateInverter(@RequestBody InverterInfo inverterInfo) {
        try {
            inverterInfoService.save(inverterInfo);
            return ResponseVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 更新逆变器信息异常");
            return ResponseVO.fail();
        }

    }


    /**
     * 删除逆变器信息
     *
     * @param ids 删除id
     * @return 执行结果
     */
    @SaveValidate
    @DeleteMapping(value = "inverterInfo/{ids}")
    public ResponseVO deleteInverter(@PathVariable("ids") String ids) {
        try {
            inverterInfoService.delete(ids);
            return ResponseVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 删除逆变器异常");
            return ResponseVO.fail();
        }

    }


}




