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


import com.energy.manage.common.base.Page;
import com.energy.manage.common.enums.TypeRelationEnum;
import com.energy.manage.common.po.anemometertower.AnemometerTowerPO;
import com.energy.manage.common.po.windfield.WindFieldPO;
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.windfield.*;
import com.energy.manage.service.domain.vo.anemometertower.AnemometerTowerByFieldVo;
import com.energy.manage.service.domain.vo.company.WindCompanyPageVo;
import com.energy.manage.service.domain.vo.windfield.WindFieldNameVo;
import com.energy.manage.service.domain.vo.windfield.WindFieldPageVo;
import com.energy.manage.service.domain.vo.windfield.WindFieldVo;
import com.energy.manage.service.domain.vo.windrelation.WindRelationVo;
import com.energy.manage.service.service.anemometertower.AnemometerTowerService;
import com.energy.manage.service.service.system.SysOrganizationAuthService;
import com.energy.manage.service.service.windenginegroup.WindEngineGroupService;
import com.energy.manage.service.service.windfield.WindFieldResourceService;
import com.energy.manage.service.service.windfield.WindFieldService;
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.Map;

@Slf4j
@RestController
@CrossOrigin
@Api(value = "WindFieldController", tags = "风场管理接口")
@RequestMapping(value = "/api/wind/field")
public class WindFieldController extends BaseServiceController {

    @Autowired
    private WindFieldService windFieldService;

    @Autowired
    private WindEngineGroupService windEngineGroupService;

    @Autowired
    private WindFieldResourceService windFieldResourceService;

    @Autowired
    private SysOrganizationAuthService sysOrganizationAuthService;

    @Autowired
    private AnemometerTowerService anemometerTowerService;



    @UserLoginToken
    @PostMapping(value = "/createWindField")
    @ApiOperation(value = "创建风场信息")
    public ResultResp createWindField(@RequestBody @Valid WindFieldDto windFieldDto) {
        // 校验风场名称是否存在
        WindFieldPO po = windFieldService.getWindFieldPOByFieldName(windFieldDto.getFieldName());
        if (po != null) {
            return ResultResp.FAIL("风场名称已经存在！");
        }
        windFieldDto.setCreateBy(getUserId());
        boolean flg = windFieldService.createWindField(windFieldDto,getRoleId(),getUserId());
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/windFieldImportData")
    @ApiOperation(value = "导入风场信息")
    public ResultResp windFieldImportData(@RequestPart MultipartFile file,
                                          @RequestParam(required = true) String companyCode) {

        WindFieldImportDataDto windFieldImportDataDto = new WindFieldImportDataDto();
        windFieldImportDataDto.setCompanyCode(companyCode);
        windFieldImportDataDto.setCreateBy(getUserId());
        return windFieldService.windFieldImportData(file, windFieldImportDataDto,getRoleId(),getUserId());
    }

    @Deprecated
    @UserLoginToken
    @PostMapping(value = "/afreshWindFieldImportData")
    @ApiOperation(value = "重新导入风场信息")
    public ResultResp afreshWindFieldImportData(@RequestPart MultipartFile file,
                                                @RequestParam(required = true) String companyCode) {

        return ResultResp.FAIL("此接口已作废！");
    }

    @UserLoginToken
    @PostMapping(value = "/WindFieldListPage")
    @ApiOperation(value = "分页查询风场信息")
    public ResultResp<Page<WindFieldPageVo>> WindFieldListPage(@RequestBody WindFieldPageDto windFieldPageDto) {
        windFieldPageDto.setUserId(getUserId());
        windFieldPageDto.setRoleId(getRoleId());
        Page<WindFieldPageVo> page = windFieldService.WindFieldListPage(windFieldPageDto);
        return ResultResp.SUCCESS(page);
    }


    @UserLoginToken
    @PostMapping(value = "/delWindFieldById")
    @ApiOperation(value = "删除风场")
    public ResultResp delWindFieldById(@RequestBody @Valid WindFieldCodeDto windFieldCodeDto) {
        if (windEngineGroupService.getWindEngineGroupCountByField(windFieldCodeDto.getFieldCode()) > 0) {
            return ResultResp.FAIL("风场下有绑定风机，不可删除！");
        }
        List<AnemometerTowerByFieldVo>  list = anemometerTowerService.getAnemometerTowerByField(windFieldCodeDto.getFieldCode());
        if(!CollectionUtils.isEmpty(list)){
            return ResultResp.FAIL("风场有绑定测风塔不可删除！");
        }
        boolean flg = windFieldService.delWindFieldById(windFieldCodeDto.getFieldCode());
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "/getWindFieldVos")
    @ApiOperation(value = "通过企业编号查询关联风场")
    public ResultResp<List<WindFieldVo>> getWindFieldVos(@RequestParam(required = true) String companyCode) {

        List<WindFieldVo> list = windFieldService.getWindFieldVos(companyCode);
        return ResultResp.SUCCESS(list);
    }

    @UserLoginToken
    @PostMapping(value = "/updateWindFieldStateById")
    @ApiOperation(value = "修改状态")
    public ResultResp updateWindFieldStateById(@RequestBody @Valid WindFieldUpdateStateDto windFieldUpdateStateDto) {

        boolean flg = windFieldService.updateWindFieldStateById(windFieldUpdateStateDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "/getWindFieldVo")
    @ApiOperation(value = "查询风场对象")
    public ResultResp<WindFieldVo> getWindFieldVo(@RequestBody @Valid WindFieldCodeDto windFieldCodeDto) {
        WindFieldVo vo = windFieldService.getWindFieldVo(windFieldCodeDto);
        return ResultResp.SUCCESS(vo);
    }

    @UserLoginToken
    @PostMapping(value = "/WindFieldDto")
    @ApiOperation(value = "更新风场信息")
    public ResultResp updateWindField(@RequestBody @Valid WindFieldUpdateDto windFieldUpdateDto) {
        boolean flg = windFieldService.updateWindField(windFieldUpdateDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "/getWindFieldTree")
    @ApiOperation(value = "查询风场树")
    public ResultResp<List<Map<String, Object>>> getWindFieldTree() {
        List<Map<String, Object>> list = windFieldService.getWindFieldTree();
        return ResultResp.SUCCESS(list);
    }


    @UserLoginToken
    @PostMapping(value = "/saveWindFieldResource")
    @ApiOperation(value = "保存上传风场资源文件")
    public ResultResp saveWindFieldResource(@RequestBody @Valid WindFieldResourceCreateDto windFieldResourceCreateDto) {
        windFieldResourceCreateDto.setCreateBy(getUserId());
        boolean flg = windFieldResourceService.saveWindFieldResource(windFieldResourceCreateDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/delWindFieldResource")
    @ApiOperation(value = "删除风场资源文件")
    public ResultResp delWindFieldResource(@RequestBody @Valid WindFieldResourceDelDto windFieldResourceDelDto) {
        boolean flg = windFieldResourceService.delWindFieldResource(windFieldResourceDelDto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/getWindFieldNames")
    @ApiOperation(value = "查询风场集合")
    public ResultResp<List<WindFieldNameVo>> getWindFieldNames() {
        List<WindFieldNameVo> list = windFieldService.getWindFieldNames(getUserId(), getRoleId());
        return ResultResp.SUCCESS(list);
    }


}
