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


import com.energy.manage.common.enums.TypeRelationEnum;
import com.energy.manage.common.po.company.WindCompanyPO;
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.company.*;
import com.energy.manage.service.domain.vo.company.WindCompanyPageVo;
import com.energy.manage.service.domain.vo.company.WindCompanyVo;
import com.energy.manage.service.domain.vo.windfield.WindFieldVo;
import com.energy.manage.service.service.company.WindCompanyService;
import com.energy.manage.service.service.system.SysOrganizationAuthService;
import com.energy.manage.service.service.windfield.WindFieldService;
import com.energy.manage.service.service.windrelation.WindRelationService;
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;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
@Api(value = "WindCompanyController", tags = "企业管理接口")
@RequestMapping(value = "/api/wind/company")
public class WindCompanyController extends BaseServiceController {

    @Autowired
    private WindCompanyService windCompanyService;

    @Autowired
    private WindFieldService windFieldService;

    @Autowired
    private WindRelationService windRelationService;


    @UserLoginToken
    @PostMapping(value = "/addCompany")
    @ApiOperation(value = "创建企业")
    public ResultResp addCompany(@RequestBody @Valid WindCompanyDto dto) {
        // 验证企业名称是否存在
        WindCompanyPO po = windCompanyService.getWindCompanyByCompanyName(dto);
        if (po != null) {
            return ResultResp.FAIL("企业名称已存在！");
        }
        if (StringUtils.isEmpty(dto.getParentCode()) || StringUtils.isEmpty(dto.getCompanyName())) {
            return ResultResp.NOTNULLPARAM();
        }
        dto.setCreateBy(getUserId());
        boolean flg = windCompanyService.addCompany(dto,getRoleId(),getUserId());
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/delCompany")
    @ApiOperation(value = "删除企业")
    public ResultResp delCompany(@RequestBody @Valid WindCompanyDelDto dto) {

        List<WindFieldVo> list = windFieldService.getWindFieldVos(dto.getCompanyCode());
        if (list.size() > 0) {
            return ResultResp.FAIL("项目公司有绑定风场不可删除！");
        }
        int number = windRelationService.getCountParentCode(dto.getCompanyCode());
        if(number>0){
            return ResultResp.FAIL("该项目公司下有子公司不可删除！");
        }

        boolean flg = windCompanyService.delCompany(dto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "/updateStateCompany")
    @ApiOperation(value = "更新企业状态")
    public ResultResp updateStateCompany(@RequestBody @Valid WindCompanyStateDto dto) {
        boolean flg = windCompanyService.updateStateCompany(dto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "/getWindCompany")
    @ApiOperation(value = "查询企业对象")
    public ResultResp<WindCompanyVo> getWindCompany(@RequestBody @Valid WindCompanyDelDto dto) {
        WindCompanyVo vo = windCompanyService.getWindCompany(dto);
        return ResultResp.SUCCESS(vo);
    }

    @UserLoginToken
    @PostMapping(value = "/updateCompany")
    @ApiOperation(value = "更新企业")
    public ResultResp updateCompany(@RequestBody @Valid WindCompanyUpdateDto dto) {
        boolean flg = windCompanyService.updateCompany(dto);
        return flg ? ResultResp.SUCCESS() : ResultResp.FAIL();
    }


    @UserLoginToken
    @PostMapping(value = "/getAllWindCompany")
    @ApiOperation(value = "查询企业树")
    public ResultResp<List<Map<String, Object>>> getAllWindCompany(@RequestBody WindCompanyPageDto windCompanyPageDto) {
        List<Map<String, Object>> list = windCompanyService.getAllWindCompany(windCompanyPageDto.getState(), windCompanyPageDto.getCompanyName(),getRoleId(),getUserId());
        return ResultResp.SUCCESS(list);
    }


}
