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


import com.energy.manage.common.base.Page;
import com.energy.manage.common.enums.IdPrefixEnum;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.domain.dto.windenginegroup.*;
import com.energy.manage.service.controller.base.BaseServiceController;
import com.energy.manage.service.domain.dto.windenginegroup.WindEngineGroupCodeDto;
import com.energy.manage.service.domain.dto.windenginegroup.WindEngineGroupCreateDto;
import com.energy.manage.service.domain.dto.windenginegroup.WindEngineGroupPageDto;
import com.energy.manage.service.domain.dto.windenginegroup.WindEngineGroupStateDto;
import com.energy.manage.service.domain.vo.cockpit.WindFieldGroupTowerVo;
import com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupPageVo;
import com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupVo;
import com.energy.manage.service.service.windenginegroup.WindEngineGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RestController
@CrossOrigin
@Api(value = "WindEngineGroupController", tags = "风机机组信息管理")
@RequestMapping(value = "/api/wind/engine/group")
public class WindEngineGroupController extends BaseServiceController {

    @Autowired
    private WindEngineGroupService windEngineGroupService;


    @UserLoginToken
    @PostMapping(value = "/createWindEngineGroup")
    @ApiOperation(value = "创建风机机组")
    public ResultResp createWindEngineGroup(@RequestBody @Valid WindEngineGroupCreateDto windEngineGroupCreateDto) {
        if (!windEngineGroupCreateDto.getFieldCode().contains(IdPrefixEnum.WIND_FIELD_NUMBER.getCode())) {
            return ResultResp.FAIL("未绑定风场信息！");
        }
        int count = windEngineGroupService.getCountWindEngineGroupByCode(windEngineGroupCreateDto.getEngineName(),windEngineGroupCreateDto.getFieldCode());
        if (count > 0) {
            return ResultResp.FAIL("风机名称已存在！");
        }
        windEngineGroupCreateDto.setCreateBy(getUserId());
        boolean flg = windEngineGroupService.createWindEngineGroup(windEngineGroupCreateDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/windEngineGroupImportData")
    @ApiOperation(value = "导入风机信息")
    public ResultResp windEngineGroupImportData(@RequestPart MultipartFile file, @RequestParam("fieldCode") String fieldCode) {
        if (!fieldCode.contains(IdPrefixEnum.WIND_FIELD_NUMBER.getCode())) {
            return ResultResp.FAIL("未绑定风场信息！");
        }
        return windEngineGroupService.windEngineGroupImportData(file, fieldCode, getUserId());
    }


    @UserLoginToken
    @PostMapping(value = "/windEngineGroupAndIdImportData")
    @ApiOperation(value = "导入风机数据(大唐使用，包含远景内部风机id属性，并对风机ID进行验证)，此功能由前端通过打包命令进行控制使用")
    public ResultResp windEngineGroupAndIdImportData(@RequestPart MultipartFile file, @RequestParam("fieldCode") String fieldCode) {
        if (!fieldCode.contains(IdPrefixEnum.WIND_FIELD_NUMBER.getCode())) {
            return ResultResp.FAIL("未绑定风场信息！");
        }
        return windEngineGroupService.windEngineGroupAndIdImportData(file, fieldCode, getUserId());
    }

    @Deprecated
    @UserLoginToken
    @PostMapping(value = "/afreshWindEngineGroupImportData")
    @ApiOperation(value = "重新上传风机信息")
    public ResultResp afreshWindEngineGroupImportData(@RequestPart MultipartFile file, @RequestParam("fieldCode") String fieldCode) {
//        boolean flg = windEngineGroupService.afreshWindEngineGroupImportData(file,fieldCode);
        return ResultResp.FAIL("不可使用");
    }


    @UserLoginToken
    @PostMapping(value = "/windEngineGrouPage")
    @ApiOperation(value = "风机分页查询")
    public ResultResp<Page<WindEngineGroupPageVo>> windEngineGrouPage(@RequestBody WindEngineGroupPageDto windEngineGroupPageDto) {
        Page<WindEngineGroupPageVo> page = windEngineGroupService.windEngineGrouPage(windEngineGroupPageDto);
        return ResultResp.SUCCESS(page);
    }

    @UserLoginToken
    @PostMapping(value = "/getWindEngineGroupListByFieldCode")
    @ApiOperation(value = "通过风场code查询风机集合查询")
    public ResultResp<List<WindEngineGroupPageVo>> getWindEngineGroupListByFieldCode(@RequestBody WindEngineGroupPageDto windEngineGroupPageDto) {
        List<WindEngineGroupPageVo> list = windEngineGroupService.getWindEngineGroupListByFieldCode(windEngineGroupPageDto);
        return ResultResp.SUCCESS(list);
    }

    @UserLoginToken
    @PostMapping(value = "/getWindEngineGroupRatedListByFieldCodePage")
    @ApiOperation(value = "通过风场code查询风机风速-分析展示集合查询")
    public ResultResp<Page<WindEngineGroupPageVo>> getWindEngineGroupRatedListByFieldCodePage(@RequestBody WindEngineGroupPageDto windEngineGroupPageDto) {
        Page<WindEngineGroupPageVo> page = windEngineGroupService.getWindEngineGroupRatedListByFieldCodePage(windEngineGroupPageDto);
        return ResultResp.SUCCESS(page);
    }


    @UserLoginToken
    @PostMapping(value = "/getWindEngineGroupByFieldCode")
    @ApiOperation(value = "通过风场code查询风机集合(jsc)")
    public ResultResp<WindFieldGroupTowerVo> getWindEngineGroupByFieldCode(@RequestBody WindEngineGroupFieldCodeDto windEngineGroupFieldCodeDto) {
        WindFieldGroupTowerVo vo = windEngineGroupService.getWindEngineGroupByFieldCode(windEngineGroupFieldCodeDto.getFieldCode(), windEngineGroupFieldCodeDto.getBatchCode());
        return ResultResp.SUCCESS(vo);
    }

    @UserLoginToken
    @PostMapping(value = "/getWindEngineGroup")
    @ApiOperation(value = "查询风机对象")
    public ResultResp<WindEngineGroupVo> getWindEngineGroup(@RequestBody WindEngineGroupCodeDto windEngineGroupCodeDto) {
        WindEngineGroupVo vo = windEngineGroupService.getWindEngineGroup(windEngineGroupCodeDto);
        return ResultResp.SUCCESS(vo);
    }


    @UserLoginToken
    @PostMapping(value = "/delWindEngineGroup")
    @ApiOperation(value = "删除风场下风机")
    public ResultResp delFieldWindEngineGroup(@RequestBody WindEngineGroupCodeDto windEngineGroupCodeDto) {
        boolean flg = windEngineGroupService.delWindEngineGroup(windEngineGroupCodeDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/updateStateWindEngineGroup")
    @ApiOperation(value = "更改状态")
    public ResultResp updateStateWindEngineGroup(@RequestBody WindEngineGroupStateDto windEngineGroupStateDto) {
        boolean flg = windEngineGroupService.updateStateWindEngineGroup(windEngineGroupStateDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "/updateWindEngineGroup")
    @ApiOperation(value = "更改风机数据")
    public ResultResp updateWindEngineGroup(@RequestBody WindEngineGroupUpdateDto dto) {
        boolean flg = windEngineGroupService.updateWindEngineGroup(dto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/bathUpdateWindEngineGroup")
    @ApiOperation(value = "批量更改风机数据")
    public ResultResp bathUpdateWindEngineGroup(@RequestBody WindEngineGroupBathUpdateDto windEngineGroupBathUpdateDto) {

        if (CollectionUtils.isEmpty(windEngineGroupBathUpdateDto.getWindEngineGroupUpdateDtoList())) {
            return ResultResp.FAIL("参数不能为空！");
        }


        Set<String> enginNames = windEngineGroupBathUpdateDto.getWindEngineGroupUpdateDtoList().stream().map(item -> item.getEngineName()).collect(Collectors.toSet());
        if(enginNames.size()<windEngineGroupBathUpdateDto.getWindEngineGroupUpdateDtoList().size()){
            return ResultResp.FAIL("风机名称不能重复！");
        }
        boolean flg = windEngineGroupService.updateWindEngineGroupList(windEngineGroupBathUpdateDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @ApiOperation("根据角色获取风机")
    @UserLoginToken
    @GetMapping("/getWindEngineGroupByUser")
    public ResultResp getWindEngineGroupByUser() {
        return success(windEngineGroupService.getWindEngineGroupByRoleId(getRoleId()));
    }

}
