package com.cusc.nirvana.user.rnr.fp.controller;

import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.eiam.client.OrganizationClient;
import com.cusc.nirvana.user.eiam.client.UserOrganClient;
import com.cusc.nirvana.user.eiam.dto.OrganizationDTO;
import com.cusc.nirvana.user.eiam.dto.UserOrganDTO;
import com.cusc.nirvana.user.rnr.fp.common.ResponseCode;
import com.cusc.nirvana.user.rnr.fp.constants.OrganBizzTypeEnum;
import com.cusc.nirvana.user.util.CuscStringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * Description: 组织控制层
 * <br />
 * CreateDate 2022-05-27 16:52:10
 *
 * @author yuyi
 **/
@RestController
@RequestMapping("/organ")
public class OrganController {

    @Resource
    private UserOrganClient userOrganClient;

    @Resource
    private OrganizationClient organizationClient;

    /**
     * Description: 根据用户id查询组织id
     * <br />
     * CreateDate 2022-05-27 17:23:37
     *
     * @author yuyi
     **/
    @RequestMapping("/getOrganIdByUserId")
    public Response<String> getOrganIdByUserId(@RequestBody UserOrganDTO bean) {
        return getOrganIdByUserIdPrivate(bean);
    }

    /**
     * Description: 根据用户id查询车企子组织信息
     * <br />
     * CreateDate 2022-05-27 17:23:37
     *
     * @author yuyi
     **/
    @RequestMapping("/getCarSubOrganByUserId")
    public Response<OrganizationDTO> getCarSubOrganByUserId(@RequestBody UserOrganDTO bean) {
        if (CuscStringUtils.isEmpty(bean.getUserId()) || CuscStringUtils.isEmpty(bean.getTenantNo())) {
            return Response.createError(ResponseCode.PARAMETER_NULL.getMsg(), ResponseCode.PARAMETER_NULL.getCode());
        }
        Response<String> organIdResp = getOrganIdByUserIdPrivate(bean);
        if (!organIdResp.isSuccess()) {
            return Response.createError(organIdResp.getMsg(), organIdResp.getCode());
        }
        String organId = organIdResp.getData();
        if (CuscStringUtils.isEmpty(organId)) {
            return Response.createSuccess();
        }
        //根据组织id查询组织信息
        OrganizationDTO organ = new OrganizationDTO();
        organ.setUuid(organId);
        organ.setTenantNo(bean.getTenantNo());
        return getCarSubOrganByOrgIdPrivate(organ);
    }

    /**
     * Description: 根据组织id查询车企子组织信息
     * <br />
     * CreateDate 2022-05-27 17:23:37
     *
     * @author yuyi
     **/
    @RequestMapping("/getCarSubOrganByOrganId")
    public Response<OrganizationDTO> getCarSubOrganByOrganId(@RequestBody OrganizationDTO bean) {
        if (CuscStringUtils.isEmpty(bean.getUuid()) || CuscStringUtils.isEmpty(bean.getTenantNo())) {
            return Response.createError(ResponseCode.PARAMETER_NULL.getMsg(), ResponseCode.PARAMETER_NULL.getCode());
        }
        return getCarSubOrganByOrgIdPrivate(bean);
    }

    /**
     * Description: 根据组织查询编码获取上一级的车企组织id
     * <br />
     * CreateDate 2022-05-27 17:23:37
     *
     * @author yuyi
     **/
    @RequestMapping("/getCarSubOrganByQueryCode")
    public Response<OrganizationDTO> getCarSubOrganByQueryCode(@RequestBody OrganizationDTO bean) {
        if (CuscStringUtils.isEmpty(bean.getQueryCode()) || CuscStringUtils.isEmpty(bean.getTenantNo())) {
            return Response.createError(ResponseCode.PARAMETER_NULL.getMsg(), ResponseCode.PARAMETER_NULL.getCode());
        }
        return getCarSubOrganByQueryCodePrivate(bean);
    }

    /**
     * Description: 根据用户id查询车企顶级组织
     * <br />
     * CreateDate 2022-05-27 17:23:37
     *
     * @author yuyi
     **/
    @RequestMapping("/getCarOrgan")
    public Response<OrganizationDTO> getCarOrgan(@RequestBody UserOrganDTO bean) {
        if (CuscStringUtils.isEmpty(bean.getUserId()) || CuscStringUtils.isEmpty(bean.getTenantNo())) {
            return Response.createError(ResponseCode.PARAMETER_NULL.getMsg(), ResponseCode.PARAMETER_NULL.getCode());
        }
        Response<String> organIdResp = getOrganIdByUserIdPrivate(bean);
        if (!organIdResp.isSuccess()) {
            return Response.createError(organIdResp.getMsg(), organIdResp.getCode());
        }
        String organId = organIdResp.getData();
        //根据组织id查询组织信息
        OrganizationDTO organ = new OrganizationDTO();
        organ.setUuid(organId);
        organ.setTenantNo(bean.getTenantNo());
        Response<OrganizationDTO> organResp = organizationClient.getByUuid(organ);
        if (!organResp.isSuccess()) {
            return Response.createError(organResp.getMsg(), organResp.getCode());
        }
        //获取最顶级的组织id
        String parentOrganId = organResp.getData().getQueryCode().split("-")[1];
        organ = new OrganizationDTO();
        organ.setUuid(parentOrganId);
        organ.setTenantNo(bean.getTenantNo());
        organResp = organizationClient.getByUuid(organ);
        if (!organResp.isSuccess()) {
            return Response.createError(organResp.getMsg(), organResp.getCode());
        }
        return Response.createSuccess(organResp.getData());
    }

    //-----------------私有方法区域-----------------------

    /**
     * Description: 根据用户id获取组织id
     * <br />
     * CreateDate 2022-05-27 19:15:25
     *
     * @author yuyi
     **/
    private Response<String> getOrganIdByUserIdPrivate(UserOrganDTO bean) {
        Response<List<UserOrganDTO>> listResponse = userOrganClient.queryByList(bean);
        if (!listResponse.isSuccess()) {
            return Response.createError(listResponse.getMsg(), listResponse.getCode());
        }
        Response ret = Response.createSuccess();
        if (!CollectionUtils.isEmpty(listResponse.getData())) {
            ret.setData(listResponse.getData().get(0).getOrganId());
        }
        return ret;
    }

    /**
     * Description: 根据组织id获取上一级的车企组织id
     * <br />
     * CreateDate 2022-05-27 18:59:35
     *
     * @author yuyi
     **/
    private Response<OrganizationDTO> getCarSubOrganByOrgIdPrivate(OrganizationDTO bean) {
        //根据组织id查询组织信息
        OrganizationDTO organ = new OrganizationDTO();
        organ.setUuid(bean.getUuid());
        organ.setTenantNo(bean.getTenantNo());
        Response<OrganizationDTO> organResp = organizationClient.getByUuid(organ);
        if (!organResp.isSuccess()) {
            return Response.createError(organResp.getMsg(), organResp.getCode());
        }
        organ = organResp.getData();
        return getCarSubOrganByQueryCodePrivate(organ);
    }

    /**
     * Description: 根据组织查询编码获取上一级的车企组织id
     * <br />
     * CreateDate 2022-05-27 18:59:35
     *
     * @author yuyi
     **/
    private Response<OrganizationDTO> getCarSubOrganByQueryCodePrivate(OrganizationDTO bean) {
        //根据当前组织的查询编码查询组织信息
        List<String> uuidList = Arrays.asList(bean.getQueryCode().split("-"));
        OrganizationDTO organSub = new OrganizationDTO();
        organSub.setTenantNo(bean.getTenantNo());
        organSub.setUuidList(uuidList);
        Response<List<OrganizationDTO>> orgListResp = organizationClient.queryByList(organSub);
        if (!orgListResp.isSuccess()) {
            return Response.createError(orgListResp.getMsg(), orgListResp.getCode());
        }
        if (CollectionUtils.isEmpty(orgListResp.getData())) {
            return Response.createSuccess();
        }

        for (OrganizationDTO tmp : orgListResp.getData()) {
            if (OrganBizzTypeEnum.CAR_FACTORY.getCode() == tmp.getBizType().intValue()
                    || OrganBizzTypeEnum.CAR_SUB_ORGAN.getCode() == tmp.getBizType().intValue()) {
                return Response.createSuccess(tmp);
            }
        }
        return Response.createSuccess();
    }
}
