package com.jiayue.ssi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiayue.ssi.annotation.OperateLog;
import com.jiayue.ssi.annotation.PreventReplay;
import com.jiayue.ssi.backenum.AuditType;
import com.jiayue.ssi.backenum.BusinessType;
import com.jiayue.ssi.constant.CustomException;
import com.jiayue.ssi.entity.ElectricField;
import com.jiayue.ssi.service.ElectricFieldService;
import com.jiayue.ssi.util.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
* 场站接口
*
* @author xsl
* @since 2023/03/13
*/
 @RestController
@RequestMapping("/electricField")
@Slf4j
public class ElectricFieldController {

    @Autowired
    ElectricFieldService electricFieldService;

    /**
     * 获取场站信息
     *
     * @return 场站信息
     */
    @GetMapping(value = "/getAll")
    @PreAuthorize("@ss.hasPermi('biz:electric:getAll')")
    @PreventReplay
    public ResponseVO getAll() throws CustomException {
        try {
            ElectricField electricField = electricFieldService.getOne(new QueryWrapper<>());
            return ResponseVO.success(electricField);
        } catch (Exception e) {
            throw new CustomException("获取场站信息异常", e);
        }
    }

    /**
     * 保存场站
     */
    @PostMapping
    @OperateLog(title = "场站信息", businessType = BusinessType.UPDATE, auditType = AuditType.BIZ)
    @PreAuthorize("@ss.hasPermi('biz:electric:update')")
    @PreventReplay
    public ResponseVO update(@RequestBody ElectricField electricField) throws CustomException {
        try {
            if (StringUtils.isEmpty(electricField.getStationCode())) {
                return ResponseVO.fail("场站编号不能为空!");
            } else if (electricField.getStationCode().length() > 6) {
                return ResponseVO.fail(electricField.getStationCode() + "场站编号长度不能超过6个字符！");
            }

            if (StringUtils.isEmpty(electricField.getName())) {
                return ResponseVO.fail("场站名称不能为空!");
            } else if (electricField.getName().length() > 50) {
                return ResponseVO.fail(electricField.getName() + "场站名称长度不能超过50个字符！");
            }

            if (StringUtils.isEmpty(electricField.getSign())) {
                return ResponseVO.fail("场站标识不能为空!");
            } else if (electricField.getSign().length() > 10) {
                return ResponseVO.fail(electricField.getSign() + "场站标识长度不能超过10个字符！");
            }


            String pattern = "^[0-9]{1,8}+\\.{0,1}[0-9]{0,2}$";
            if (!electricField.getCapacity().toString().matches(pattern)){
                return ResponseVO.fail("装机容量整数位不能超过8位，小数位不能超过2位！");
            }

            if (electricField.getGridce() == null) {
                return ResponseVO.fail("并网设备数不能为空!");
            } else if (!String.valueOf(electricField.getGridce()).matches("^([0-9][0-9]{0,1}|100)$")) {
                return ResponseVO.fail("并网设备数请输入0-100整数");
            }

            if (StringUtils.isEmpty(electricField.getFieldType())) {
                return ResponseVO.fail("场站类型不能为空!");
            } else if (electricField.getFieldType().length() > 1) {
                return ResponseVO.fail("场站类型长度不能超过1个字符！");
            }

            if (StringUtils.isEmpty(electricField.getCompany())) {
                return ResponseVO.fail("所属公司不能为空!");
            } else if (electricField.getCompany().length() > 100) {
                return ResponseVO.fail("所属公司长度不能超过100个字符！");
            }

            if (StringUtils.isEmpty(electricField.getLocation())) {
                return ResponseVO.fail("场站位置不能为空!");
            } else if (electricField.getLocation().length() > 100) {
                return ResponseVO.fail("场站位置长度不能超过100个字符！");
            }

            boolean bo = electricFieldService.saveOrUpdate(electricField);
            if (bo) {
                return ResponseVO.success("场站信息保存成功");
            } else {
                log.error("场站信息保存失败");
                return ResponseVO.fail("场站信息保存失败");
            }
        } catch (Exception e) {
            throw new CustomException("场站信息保存异常", e);
        }
    }

}
