package com.energy.manage.service.controller.units;


import com.dtflys.forest.annotation.Get;
import com.energy.manage.common.po.units.UnitBearingsPO;
import com.energy.manage.common.po.units.UnitDynamoPO;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.controller.base.BaseServiceController;
import com.energy.manage.service.domain.dto.units.UnitDictBrandModelDto;
import com.energy.manage.service.domain.dto.units.UnitDictConstantsDelDto;
import com.energy.manage.service.domain.dto.units.UnitDictRoutineDto;
import com.energy.manage.service.domain.dto.units.UnitDto;
import com.energy.manage.service.domain.vo.units.UnitDictRoutineListVo;
import com.energy.manage.service.domain.vo.units.UnitVo;
import com.energy.manage.service.service.units.UnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@Api(value = "UnitController", tags = "风机部件管理接口")
@RequestMapping(value = "/api/unit")
public class UnitController extends BaseServiceController {


    @Autowired
    private UnitService unitService;


    @UserLoginToken
    @PostMapping(value = "/getBrandModelNameOrModelNumber")
    @ApiOperation(value = "根据部件类型，查询部件类型对应品牌/型号")
    public ResultResp<List<String>> getBrandModelNameOrModelNumber(@RequestBody @Valid UnitDictBrandModelDto unitDictBrandModelDto) {
        if (unitDictBrandModelDto.getUnitType() == null) {
            log.error("查询部件，部件类型unitType不能为空");
            return ResultResp.NOTNULLPARAM();
        }
        List<String> list = unitService.getBrandModelNameOrModelNumber(unitDictBrandModelDto);
        return ResultResp.SUCCESS(list);
    }


    @UserLoginToken
    @PostMapping(value = "/getUnitDictRoutineList")
    @ApiOperation(value = "根据部件类型，查询部件常用类型字典")
    public ResultResp<List<UnitDictRoutineListVo>> getUnitDictRoutineList(@RequestBody @Valid UnitDictRoutineDto unitDictRoutineDto) {
        List<UnitDictRoutineListVo> list = unitService.getUnitDictRoutineList(unitDictRoutineDto);
        return ResultResp.SUCCESS(list);
    }


    @UserLoginToken
    @PostMapping(value = "/modificationUnit")
    @ApiOperation(value = "更新部件相关信息")
    public ResultResp modificationUnit(@RequestBody @Valid UnitDto unitDto) {

        if (StringUtils.isEmpty(unitDto.getMillTypeCode())) {
            return ResultResp.NOTNULLPARAM();
        }
        // 验证主轴数据
        UnitDto.validateAndSetEmptyToNull(unitDto.getUnitBearingsDto());
        if (unitDto.getUnitBearingsDto().allPropertiesAreNull()) {
            unitDto.setUnitBearingsDto(null);
        }
        // 验证齿轮箱数据
        UnitDto.validateAndSetEmptyToNull(unitDto.getUnitGearDto());
        if (!unitDto.getUnitGearDto().allPropertiesGearboxBearingsListNull()) {
            unitDto.getUnitGearDto().setUnitGearboxBearingsDtoList(null);
        }
        if (!unitDto.getUnitGearDto().allPropertiesGearboxStructureListNull()) {
            unitDto.getUnitGearDto().setUnitGearboxStructureDtoList(null);
        }

        if (unitDto.getUnitGearDto().allPropertiesAreNull()) {
            unitDto.setUnitGearDto(null);
        }
        // 验证发电机数据
        UnitDto.validateAndSetEmptyToNull(unitDto.getUnitDynamoDto());
        if (unitDto.getUnitDynamoDto().allPropertiesAreNull()) {
            unitDto.setUnitDynamoDto(null);
        }

        if (unitDto.getUnitBearingsDto() == null &&
                unitDto.getUnitDynamoDto() == null &&
                unitDto.getUnitGearDto() == null) {
            return ResultResp.FAIL("所有数据均为空,不进行部件数据维护!");
        }

        UnitBearingsPO unitBearingsPO = unitService.verifyUnitBearingByMillTypeCode(unitDto.getMillTypeCode());
        if (unitBearingsPO != null && unitBearingsPO.getId().intValue() != unitDto.getUnitBearingsDto().getId().intValue()) {
            return ResultResp.FAIL("此机型编号已经绑定部件信息，不可重复绑定");
        }

        UnitDynamoPO unitDynamoPO = unitService.verifyUnitDynamoByMillTypeCode(unitDto.getMillTypeCode());
        if (unitDynamoPO != null && unitDynamoPO.getId().intValue() != unitDto.getUnitDynamoDto().getId().intValue()) {
            return ResultResp.FAIL("此机型编号已经绑定部件信息，不可重复绑定");
        }

        boolean flg = unitService.modificationUnit(unitDto, getUserId());
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @GetMapping(value = "/getUnitVo")
    @ApiOperation(value = "通过机型编号查询部件信息")
    public ResultResp<UnitVo> getUnitVo(@RequestParam String millTypeCode) {
        UnitVo vo = unitService.getUnitVo(millTypeCode);
        return ResultResp.SUCCESS(vo);
    }







}
