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.constant.enums.PvRotationModeEnum;
import com.jiayue.ipfcst.common.data.entity.PvModuleModel;
import com.jiayue.ipfcst.console.service.PvModuleModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 光伏组件信息restful接口
 *
 * @author tl
 * @version 3.0
 * @since 2020/6/28 10:12
 */
@RestController
@Slf4j
public class PvModuleModelController {

    private final PvModuleModelService pvModuleModelService;

    @Autowired public PvModuleModelController(PvModuleModelService pvModuleModelService) {
        this.pvModuleModelService = pvModuleModelService;
    }


    /**
     * 分页获取光伏组件信息
     * @param size 条数
     * @param page 页码
     * @return 光伏组件信息
     */

    @GetMapping(value="pvModuleModel/{page}/{size}")
    public ResponseVO getPvModuleModel(@PathVariable("page")  Integer page,
                                       @PathVariable("size")  Integer size) {
        Map<String, Object> map = new HashMap<>();
        try{
            map = this.pvModuleModelService.get(page,size);

            return ResponseVO.success(map);
        }catch (Exception e){
            e.printStackTrace();
            log.error(" 获取光伏组件信息异常");
            return ResponseVO.success(null);
        }

    }


    /**
     * 获取光伏组件信息
     *
     * @return 光伏组件信息
     */

    @GetMapping(value="pvModuleModel/")
    public ResponseVO getAll() {

        try{
            List<PvModuleModel> ps =  this.pvModuleModelService.getAll();

            return ResponseVO.success(ps);
        }catch (Exception e){
            e.printStackTrace();
            log.error(" 获取所有光伏组件异常");
            return ResponseVO.success(null);
        }

    }

    /**
     * 保存光伏组件信息
     *
     * @param pvModuleModel 参数
     * @return 执行结果
     */
    @SaveValidate
    @PostMapping(value = "pvModuleModel/")
    public ResponseVO savePvModuleModel(@RequestBody PvModuleModel pvModuleModel) {
        try {
            pvModuleModelService.save(pvModuleModel);
            return ResponseVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 保存光伏组件信息异常");
            return ResponseVO.fail();
        }

    }

    /**
     * 更新光伏组件信息
     *
     * @param pvModuleModel 参数
     * @return 执行结果
     */
    @SaveValidate
    @PutMapping(value = "pvModuleModel/")
    public ResponseVO updatePvModuleModel(@RequestBody PvModuleModel pvModuleModel) {
        try {
            pvModuleModelService.save(pvModuleModel);
            return ResponseVO.success();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 更新光伏组件信息异常");
            return ResponseVO.fail();
        }

    }

    /**
     * 删除光伏组件信息
     *
     * @param ids 删除id
     * @return 执行结果
     */
    @SaveValidate
    @DeleteMapping(value="pvModuleModel/{ids}")
    public ResponseVO deletePvModuleModel(@PathVariable("ids")String ids){
        try{
            pvModuleModelService.delete(ids);
            return ResponseVO.success();
        }catch(Exception e){
            e.printStackTrace();
            log.error(" 删除光伏组件信息异常");
            return ResponseVO.fail();
        }

    }

  /**
   * 获取光伏组件旋转方式枚举
   *
   * @return 执行结果
   */
    @GetMapping(value = "getPvRotationMode/")
    public ResponseVO getPvRotationMode() {
        List<Map<String, Object>> provinceType = new ArrayList<>();
        Map<String, Object> map;
        try {
            for (PvRotationModeEnum e : PvRotationModeEnum.values()) {
                map = new HashMap<>();
                map.put("label", e.getMessage());
                map.put("key", e.name());
                map.put("value", e.name());
                provinceType.add(map);
            }
            return ResponseVO.success(provinceType);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 获取光伏组件旋转类型异常");
            return ResponseVO.fail(provinceType);
        }
    }
}
