package com.energy.manage.service.domain.dto.units;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;
import java.util.List;

/**
 * 保存更新部件信息相关参数
 */
@Getter
@Setter
@ApiModel("保存更新部件信息相关参数")
public class UnitDto {

    /**
     * 机型编号
     */
    @ApiModelProperty("机型编号")
    @NotNull(message = "机型编号不能为空")
    private String millTypeCode;

    @ApiModelProperty("主轴相关参数")
    UnitBearingsDto unitBearingsDto;


    @ApiModelProperty("齿轮箱相关参数")
    UnitGearDto unitGearDto;


    @ApiModelProperty("发电机相关参数")
    UnitDynamoDto unitDynamoDto;


    /**
     * 齿轮箱相关参数
     */
    @Setter
    @Getter
    @ApiModel
    public static class UnitGearDto {

        /**
         * 主键
         */
        @ApiModelProperty("主键")
        private Integer id;

        /**
         * 部件编号
         */
        @ApiModelProperty("部件编号")
        private String code;

        /**
         * 润滑油品牌
         */
        @ApiModelProperty("润滑油品牌")
        private String lubricantBrand;
        /**
         * 润滑油型号
         */
        @ApiModelProperty("润滑油型号")
        private String lubricantModel;


        /**
         * 齿轮箱轴承集合
         */
        @ApiModelProperty("齿轮箱轴承集合")
        List<UnitGearboxBearingsDto> unitGearboxBearingsDtoList;

        /**
         * 齿轮箱结构集合
         */
        @ApiModelProperty("齿轮箱结构集合")
        List<UnitGearboxStructureDto> unitGearboxStructureDtoList;

        // 判断所有属性是否都为空
        public boolean allPropertiesAreNull() {
            return (this.id == null || this.id == 0) &&
                    (this.code == null || this.code.isEmpty()) &&
                    (this.lubricantBrand == null || this.lubricantBrand.isEmpty()) &&
                    (this.lubricantModel == null || this.lubricantModel.isEmpty()) &&
                    (CollectionUtils.isEmpty(this.unitGearboxBearingsDtoList)) &&
                    (CollectionUtils.isEmpty(this.unitGearboxStructureDtoList));
        }

        // 判断齿轮箱轴承集合所有属性是否都为空
        public boolean allPropertiesGearboxBearingsListNull() {
           List<UnitGearboxBearingsDto> list =  this.unitGearboxBearingsDtoList;
           for(int i =0;i<list.size();i++){
               UnitGearboxBearingsDto unitGearboxBearingsDto = list.get(i);
               if(unitGearboxBearingsDto.allPropertiesAreNull()){
                   list.remove(i);
               }
           }
           if(list.size()<=0){
               return false;
           }
           return true;
        }

        // 判断齿轮箱结构集合所有属性是否都为空
        public boolean allPropertiesGearboxStructureListNull() {
            List<UnitGearboxStructureDto> list =  this.unitGearboxStructureDtoList;
            for(int i =0;i<list.size();i++){
                UnitGearboxStructureDto unitGearboxStructureDto = list.get(i);
                if(unitGearboxStructureDto.allPropertiesAreNull()){
                    list.remove(i);
                }
            }
            if(list.size()<=0){
                return false;
            }
            return true;
        }

        /**
         * 数据校验
         *
         * @param unitGearDto
         * @return
         */
        public static boolean isValid(UnitGearDto unitGearDto) {
            if (unitGearDto == null) {
                return false;
            }
            if (unitGearDto.getId() == null) {
                return false;
            }

            if (unitGearDto.getCode() == null || unitGearDto.getCode().isEmpty()) {
                return false;
            }

            if (unitGearDto.getLubricantBrand() == null || unitGearDto.getLubricantBrand().isEmpty()) {
                return false;
            }

            if (unitGearDto.getLubricantModel() == null || unitGearDto.getLubricantModel().isEmpty()) {
                return false;
            }

            if (CollectionUtils.isEmpty(unitGearDto.getUnitGearboxStructureDtoList())) {
                return false;
            }

            if (CollectionUtils.isEmpty(unitGearDto.getUnitGearboxBearingsDtoList())) {
                return false;
            }

            return true;
        }




    }

    /**
     * 齿轮箱结构表
     */
    @Setter
    @Getter
    @ApiModel
    public static class UnitGearboxStructureDto {

        @ApiModelProperty("主键")
        private Integer id;

        /**
         * 齿轮箱总表编号
         */
        @ApiModelProperty("齿轮箱总表编号")
        private String gearboxCode;
        /**
         * 齿轮箱结构类型;1.行星轮2.平行轮
         */
        @ApiModelProperty("齿轮箱结构类型;1.行星轮2.平行轮")
        private Integer gearboxStructure;
        /**
         * 齿轮箱结构等级;sort_num1,2......
         */
        @ApiModelProperty("齿轮箱结构等级;sort_num1,2......")
        private Integer planetaryGearGrade;
        /**
         * 大齿轮齿数
         */
        @ApiModelProperty("大齿轮齿数")
        private String largeGearTeethCount;
        /**
         * 小齿轮齿数
         */
        @ApiModelProperty("小齿轮齿数")
        private String smallGearTeethCount;
        /**
         * 轴承品牌
         */
        @ApiModelProperty("轴承品牌")
        private String bearingBrand;
        /**
         * 轴承型号
         */
        @ApiModelProperty("轴承型号")
        private String bearingModel;
        /**
         * 齿圈齿数
         */
        @ApiModelProperty("齿圈齿数")
        private String gearRingTeethCount;
        /**
         * 太阳轮齿数
         */
        @ApiModelProperty("太阳轮齿数")
        private String sunWheelTeethCount;
        /**
         * 行星轮个数
         */
        @ApiModelProperty("行星轮个数")
        private String planetWheelCount;
        /**
         * 行星轮齿数
         */
        @ApiModelProperty("行星轮齿数")
        private String planetWheelTeethCount;
        /**
         * 太阳轮轴承品牌
         */
        @ApiModelProperty("太阳轮轴承品牌")
        private String sunWheelBearingBrand;
        /**
         * 太阳轮轴承型号
         */
        @ApiModelProperty("太阳轮轴承型号")
        private String sunWheelBearingModel;

        /**
         * 数据校验
         *
         * @param dto
         * @return
         */
        public static boolean isValid(UnitGearboxStructureDto dto) {
            if (dto == null) {
                return false;
            }
            if (dto.getId() == null) {
                return false;
            }

            if (dto.getGearboxCode() == null || dto.getGearboxCode().isEmpty()) {
                return false;
            }

            if (dto.getGearboxStructure() == null) {
                return false;
            }

            if (dto.getPlanetaryGearGrade() == null) {
                return false;
            }

            if (dto.getLargeGearTeethCount() == null || dto.getLargeGearTeethCount().isEmpty()) {
                return false;
            }

            if (dto.getSmallGearTeethCount() == null || dto.getSmallGearTeethCount().isEmpty()) {
                return false;
            }

            if (dto.getBearingBrand() == null || dto.getBearingBrand().isEmpty()) {
                return false;
            }

            if (dto.getBearingModel() == null || dto.getBearingModel().isEmpty()) {
                return false;
            }

            if (dto.getGearRingTeethCount() == null || dto.getGearRingTeethCount().isEmpty()) {
                return false;
            }

            if (dto.getSunWheelTeethCount() == null || dto.getSunWheelTeethCount().isEmpty()) {
                return false;
            }

            if (dto.getPlanetWheelCount() == null || dto.getPlanetWheelCount().isEmpty()) {
                return false;
            }

            if (dto.getPlanetWheelTeethCount() == null || dto.getPlanetWheelTeethCount().isEmpty()) {
                return false;
            }

            if (dto.getSunWheelBearingBrand() == null || dto.getSunWheelBearingBrand().isEmpty()) {
                return false;
            }

            if (dto.getSunWheelBearingModel() == null || dto.getSunWheelBearingModel().isEmpty()) {
                return false;
            }

            return true;
        }

        // 判断所有属性是否都为空
        public boolean allPropertiesAreNull() {
            return (this.id == null || this.id == 0) &&
                    (this.gearboxCode == null || this.gearboxCode.isEmpty()) &&
                    (this.gearboxStructure == null || this.gearboxStructure == 0) &&
                    (this.planetaryGearGrade == null || this.planetaryGearGrade == 0) &&
                    (this.largeGearTeethCount == null || this.largeGearTeethCount.isEmpty()) &&
                    (this.smallGearTeethCount == null || this.smallGearTeethCount.isEmpty()) &&
                    (this.bearingBrand == null || this.bearingBrand.isEmpty()) &&
                    (this.bearingModel == null || this.bearingModel.isEmpty()) &&
                    (this.gearRingTeethCount == null || this.gearRingTeethCount.isEmpty()) &&
                    (this.sunWheelTeethCount == null || this.sunWheelTeethCount.isEmpty()) &&
                    (this.planetWheelCount == null || this.planetWheelCount.isEmpty()) &&
                    (this.planetWheelTeethCount == null || this.planetWheelTeethCount.isEmpty()) &&
                    (this.sunWheelBearingBrand == null || this.sunWheelBearingBrand.isEmpty()) &&
                    (this.sunWheelBearingModel == null || this.sunWheelBearingModel.isEmpty());
        }
    }


    /**
     * 齿轮箱轴承
     */
    @Setter
    @Getter
    @ApiModel
    public static class UnitGearboxBearingsDto {

        @ApiModelProperty("主键")
        private Integer id;

        /**
         * 齿轮箱编号
         */
        @ApiModelProperty("齿轮箱编号")
        private String gearboxCode;

        /**
         * 齿轮箱轴承类型1.低速轴，2.低速中间轴3.高速轴
         */
        @ApiModelProperty("齿轮箱轴承类型1.低速轴，2.低速中间轴3.高速轴")
        private Integer parallelWheelGrade;
        /**
         * 齿轮箱轴承编号
         */
        @ApiModelProperty("齿轮箱轴承编号")
        private String gearboxBearingNumber;
        /**
         * 齿轮箱轴承级别1.2.3
         */
        @ApiModelProperty(" 齿轮箱轴承级别1.2.3")
        private String gearboxBearingLevel;
        /**
         * 轴承品牌
         */
        @ApiModelProperty("轴承品牌")
        private String bearingBrand;
        /**
         * 轴承型号
         */
        @ApiModelProperty("轴承型号")
        private String bearingModel;
        /**
         * 齿圈齿数
         */
        @ApiModelProperty("齿圈齿数")
        private String gearRingTeethCount;
        /**
         * 轴承-RS品牌
         */
        @ApiModelProperty("轴承-RS品牌")
        private String bearingRsBrand;
        /**
         * 轴承-RS型号
         */
        @ApiModelProperty(" 轴承-RS型号")
        private String bearingRsModel;
        /**
         * 轴承-GS品牌
         */
        @ApiModelProperty("轴承-GS品牌")
        private String bearingGsBrand;
        /**
         * 轴承-GS型号
         */
        @ApiModelProperty("轴承-GS型号")
        private String bearingGsModel;

        /**
         * 数据校验
         *
         * @param gearboxBearing
         * @return
         */
        public static boolean isValid(UnitGearboxBearingsDto gearboxBearing) {
            if (gearboxBearing == null) {
                return false;
            }
            if (gearboxBearing.getId() == null) {
                return false;
            }

            if (gearboxBearing.getGearboxCode() == null || gearboxBearing.getGearboxCode().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getParallelWheelGrade() == null) {
                return false;
            }

            if (gearboxBearing.getGearboxBearingNumber() == null || gearboxBearing.getGearboxBearingNumber().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getGearboxBearingLevel() == null || gearboxBearing.getGearboxBearingLevel().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getBearingBrand() == null || gearboxBearing.getBearingBrand().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getBearingModel() == null || gearboxBearing.getBearingModel().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getGearRingTeethCount() == null || gearboxBearing.getGearRingTeethCount().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getBearingRsBrand() == null || gearboxBearing.getBearingRsBrand().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getBearingRsModel() == null || gearboxBearing.getBearingRsModel().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getBearingGsBrand() == null || gearboxBearing.getBearingGsBrand().isEmpty()) {
                return false;
            }

            if (gearboxBearing.getBearingGsModel() == null || gearboxBearing.getBearingGsModel().isEmpty()) {
                return false;
            }

            return true;
        }


        // 判断所有属性是否都为空
        public boolean allPropertiesAreNull() {
            return (this.id == null || this.id == 0) &&
                    (this.gearboxCode == null || this.gearboxCode.isEmpty()) &&
                    (this.parallelWheelGrade == null || this.parallelWheelGrade == 0) &&
                    (this.gearboxBearingNumber == null || this.gearboxBearingNumber.isEmpty()) &&
                    (this.gearboxBearingLevel == null || this.gearboxBearingLevel.isEmpty()) &&
                    (this.bearingBrand == null || this.bearingBrand.isEmpty()) &&
                    (this.bearingModel == null || this.bearingModel.isEmpty()) &&
                    (this.gearRingTeethCount == null || this.gearRingTeethCount.isEmpty()) &&
                    (this.bearingRsBrand == null || this.bearingRsBrand.isEmpty()) &&
                    (this.bearingRsModel == null || this.bearingRsModel.isEmpty()) &&
                    (this.bearingGsBrand == null || this.bearingGsBrand.isEmpty()) &&
                    (this.bearingGsModel == null || this.bearingGsModel.isEmpty());
        }


    }


    /**
     * 主轴相关参数
     */
    @Setter
    @Getter
    @ApiModel
    public static class UnitBearingsDto {

        /**
         * 主键
         */
        @ApiModelProperty("主键")
        private Integer id;

        /**
         * 主轴部件编号;主轴部件编号
         */
        @ApiModelProperty("主轴部件编号")
        private String code;

        /**
         * 润滑油品牌
         */
        @ApiModelProperty("润滑油品牌")
        private String lubricantBrand;
        /**
         * 润滑油型号
         */
        @ApiModelProperty("润滑油型号")
        private String lubricantModel;
        /**
         * 前轴承品牌
         */
        @ApiModelProperty("前轴承品牌")
        private String frontBearingBrand;
        /**
         * 前轴承型号
         */
        @ApiModelProperty("前轴承型号")
        private String frontBearingModel;
        /**
         * 后轴承品牌
         */
        @ApiModelProperty("后轴承品牌")
        private String rearBearingBrand;
        /**
         * 后轴承型号
         */
        @ApiModelProperty("后轴承型号")
        private String rearBearingModel;

        /**
         * 数据验证
         *
         * @param component
         * @return
         */
        public static boolean isValid(UnitBearingsDto component) {

            if (component == null) {
                return false;
            }
            if (component.getId() == null) {
                return false;
            }

            if (component.getCode() == null || component.getCode().isEmpty()) {
                return false;
            }

            if (component.getLubricantBrand() == null || component.getLubricantBrand().isEmpty()) {
                return false;
            }

            if (component.getLubricantModel() == null || component.getLubricantModel().isEmpty()) {
                return false;
            }

            if (component.getFrontBearingBrand() == null || component.getFrontBearingBrand().isEmpty()) {
                return false;
            }

            if (component.getFrontBearingModel() == null || component.getFrontBearingModel().isEmpty()) {
                return false;
            }

            if (component.getRearBearingBrand() == null || component.getRearBearingBrand().isEmpty()) {
                return false;
            }

            if (component.getRearBearingModel() == null || component.getRearBearingModel().isEmpty()) {
                return false;
            }

            return true;
        }

        // 判断所有属性是否都为空
        public boolean allPropertiesAreNull() {
            return (this.id == null || this.id == 0) &&
                    (this.code == null || this.code.isEmpty()) &&
                    (this.lubricantBrand == null || this.lubricantBrand.isEmpty()) &&
                    (this.lubricantModel == null || this.lubricantModel.isEmpty()) &&
                    (this.frontBearingBrand == null || this.frontBearingBrand.isEmpty()) &&
                    (this.frontBearingModel == null || this.frontBearingModel.isEmpty()) &&
                    (this.rearBearingBrand == null || this.rearBearingBrand.isEmpty()) &&
                    (this.rearBearingModel == null || this.rearBearingModel.isEmpty());
        }

    }


    /**
     * 发电机相关参数
     */
    @Setter
    @Getter
    @ApiModel
    public static class UnitDynamoDto {

        /**
         * 主键
         */
        @ApiModelProperty("主键")
        private Integer id;

        /**
         * 极对数
         */
        @ApiModelProperty("极对数")
        private Integer polePairs;
        /**
         * 冷却方式
         */
        @ApiModelProperty("冷却方式")
        private Integer coolingMethod;
        /**
         * 润滑油品牌
         */
        @ApiModelProperty("润滑油品牌")
        private String lubricantBrand;
        /**
         * 润滑油型号
         */
        @ApiModelProperty("润滑油型号")
        private String lubricantModel;
        /**
         * 驱动端轴承品牌
         */
        @ApiModelProperty("驱动端轴承品牌")
        private String driveEndBearingBrand;
        /**
         * 驱动端轴承型号
         */
        @ApiModelProperty("驱动端轴承型号")
        private String driveEndBearingModel;
        /**
         * 非驱动端轴承品牌
         */
        @ApiModelProperty("非驱动端轴承品牌")
        private String nonDriveEndBearingBrand;
        /**
         * 非驱动端轴承型号
         */
        @ApiModelProperty("非驱动端轴承型号")
        private String nonDriveEndBearingModel;

        /**
         * 数据校验
         *
         * @param motor
         * @return
         */
        public static boolean isValid(UnitDynamoDto motor) {
            if (motor == null) {
                return false;
            }
            if (motor.getId() == null) {
                return false;
            }

            if (motor.getPolePairs() == null) {
                return false;
            }

            if (motor.getCoolingMethod() == null) {
                return false;
            }

            if (motor.getLubricantBrand() == null || motor.getLubricantBrand().isEmpty()) {
                return false;
            }

            if (motor.getLubricantModel() == null || motor.getLubricantModel().isEmpty()) {
                return false;
            }

            if (motor.getDriveEndBearingBrand() == null || motor.getDriveEndBearingBrand().isEmpty()) {
                return false;
            }

            if (motor.getDriveEndBearingModel() == null || motor.getDriveEndBearingModel().isEmpty()) {
                return false;
            }

            if (motor.getNonDriveEndBearingBrand() == null || motor.getNonDriveEndBearingBrand().isEmpty()) {
                return false;
            }

            if (motor.getNonDriveEndBearingModel() == null || motor.getNonDriveEndBearingModel().isEmpty()) {
                return false;
            }

            return true;
        }

        // 判断所有属性是否都为空
        public boolean allPropertiesAreNull() {
            return (this.id == null || this.id == 0) &&
                    (this.polePairs == null || this.polePairs == 0) &&
                    (this.coolingMethod == null || this.coolingMethod == 0) &&
                    (this.lubricantBrand == null || this.lubricantBrand.isEmpty()) &&
                    (this.lubricantModel == null || this.lubricantModel.isEmpty()) &&
                    (this.driveEndBearingBrand == null || this.driveEndBearingBrand.isEmpty()) &&
                    (this.driveEndBearingModel == null || this.driveEndBearingModel.isEmpty()) &&
                    (this.nonDriveEndBearingBrand == null || this.nonDriveEndBearingBrand.isEmpty()) &&
                    (this.nonDriveEndBearingModel == null || this.nonDriveEndBearingModel.isEmpty());
        }


    }


    // 通用方法：传入一个对象，当对象中的字符串属性为空时将其设置为null
    public static <T> T validateAndSetEmptyToNull(T obj) {
        if (obj == null) {
            return null;
        }
        boolean allFieldsNull = true;
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object value = field.get(obj);

                if (value instanceof String) {
                    String strValue = (String) value;
                    if (strValue != null && !strValue.trim().isEmpty()) {
                        allFieldsNull = false;
                    }else{
                        field.set(obj, null);
                    }
                }
                if (value instanceof Integer) {
                    Integer strValue = (Integer) value;
                    if (strValue != null && strValue > 0) {
                        allFieldsNull = false;
                    }else{
                        field.set(obj, null);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        if (allFieldsNull) {
            try {
                return (T) obj.getClass().newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return obj;
    }


}
