package pwc.taxtech.atms.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;
import pwc.taxtech.atms.dto.AreaOrganizationStatistics;
import pwc.taxtech.atms.dto.IndustryDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.OrganizationValidateDto;
import pwc.taxtech.atms.dto.dimension.DimensionOrgDtoDashboard;
import pwc.taxtech.atms.dto.dimension.OrgDashboardParams;
import pwc.taxtech.atms.dto.navtree.DevTreeDto;
import pwc.taxtech.atms.dto.navtree.NavTreeDto;
import pwc.taxtech.atms.dto.organization.DimensionRoleDto;
import pwc.taxtech.atms.dto.organization.OrgBasicDto;
import pwc.taxtech.atms.dto.organization.OrgDto;
import pwc.taxtech.atms.dto.organization.OrgGeneralInfoDto;
import pwc.taxtech.atms.dto.organization.OrganizationDto;
import pwc.taxtech.atms.dto.organization.UpdateOrgDimensionDto;
import pwc.taxtech.atms.service.OrganizationService;
import pwc.taxtech.atms.service.UserRoleService;
import pwc.taxtech.atms.service.UserService;

@RestController
@RequestMapping("/api/v1/org/")
public class OrganizationController {

    private static Logger logger = LoggerFactory.getLogger(OrganizationController.class);

    @Autowired
    OrganizationService organizationService;
    @Autowired
    UserRoleService userRoleService;
    @Autowired
    UserService userService;

    @ApiOperation(value = "根据使用的方式获取机构的列表")
    @RequestMapping(value = "getjson", method = RequestMethod.GET)
    public @ResponseBody
    List<NavTreeDto> getOrgListToJson(@RequestParam("useType") Integer useType) {
        return organizationService.getOrgListToJson(useType);
    }

    @ApiOperation(value = "获取根据机构筛选的属性和机构名称")
    @RequestMapping(value = "getOrganizationFilterList", method = RequestMethod.GET)
    public @ResponseBody
    List<OrganizationDto> getOrganizationFilterList() {
        return organizationService.getOrganizationFilterList();
    }

    @ApiOperation(value = "根据使用的方式获取机构的展示列表")
    @RequestMapping(value = "display", method = RequestMethod.GET)
    public @ResponseBody
    List<OrganizationDto> getOrgList(@RequestParam("useType") Integer useType) {
        return organizationService.getOrgList(useType);
    }

    @ApiOperation(value = "获取行业列表")
    @RequestMapping(value = "getProjectIndustrys", method = RequestMethod.GET)
    public @ResponseBody
    List<IndustryDto> getProjectIndustry() {
        logger.info("GET /api/v1/org/getProjectIndustrys");
        return organizationService.getProjectIndustryList();
    }

    @ApiOperation(value = "层级显示机构列表_列表显示")
    @RequestMapping(value = "getOrgListLevel", method = RequestMethod.GET)
    public @ResponseBody
    List<OrgBasicDto> getOrgListLevel() {
        logger.info("GET /api/v1/org/getOrgListLevel");
        return organizationService.getOrgListLevel();
    }

    @ApiOperation(value = "纳税人识别号唯一性验证")
    @RequestMapping(value = "taxPayerNumberUniqueValidate", method = RequestMethod.POST)
    public @ResponseBody
    Boolean taxPayerNumberUniqueValidate(@RequestBody OrganizationValidateDto validateDto) {
        logger.info("POST /api/v1/org/taxPayerNumberUniqueValidate");
        return organizationService.taxPayerNumberUniqueValidate(validateDto);
    }

    @ApiOperation(value = "获取机构展示面板(Dashboard)")
    @RequestMapping(value = "getOrgDashboard", method = RequestMethod.POST)
    public @ResponseBody
    List<DimensionOrgDtoDashboard> getOrgDashboardPost(
            @RequestBody(required = false) List<String> expandedOrgList,
            @RequestParam("dimensionID") String dimensionID, @RequestParam("parentOrgID") String parentOrgID) {
        logger.info("POST /api/v1/org/getOrgDashboard");
        OrgDashboardParams param = new OrgDashboardParams();
        param.setExpandedOrgList(expandedOrgList);
        return organizationService.getOrgDashboard(dimensionID, parentOrgID, param);
    }

    @ApiOperation(value = "获取单个机构展示面板(Dashboard)")
    @RequestMapping(value = "getOrgDashboardOnly", method = RequestMethod.GET)
    public @ResponseBody
    List<DimensionOrgDtoDashboard> getOrgDashboard(@RequestParam("dimensionID") String dimensionID,
                                                   @RequestParam("parentOrgID") String parentOrgID, @RequestParam("filterFlag") String filterFlag) {
        logger.info("POST /api/v1/org/getOrgDashboardOnly");
        OrgDashboardParams param = new OrgDashboardParams();
        param.setFilterFlag(filterFlag);
        return organizationService.getOrgDashboard(dimensionID, parentOrgID, param);
    }

    @ApiOperation(value = "显示覆盖区域统计数据")
    @RequestMapping(value = "getAreaStatistics", method = RequestMethod.GET)
    public AreaOrganizationStatistics getAreaStatistics() {
        logger.info("POST /api/v1/org/getAreaStatistics");
        return organizationService.getAreaStatistics();
    }

    @ApiOperation(value = "获取机构上层维度")
    @RequestMapping(value = "getOrgBuAreaIndustryUser", method = RequestMethod.GET)
    public @ResponseBody
    List<DimensionRoleDto> getOrgBuAreaIndustryUser(@RequestParam("userID") String userID) {
        logger.info("POST /api/v1/org/getOrgBuAreaIndustryUser");
        return userRoleService.getOrgBuAreaIndustryUser(userID);
    }

    @ApiOperation(value = "显示覆盖区域统计数据")
    @RequestMapping(value = "getOrgCustomDashbord", method = RequestMethod.GET)
    public OrgDto getOrgCustomDashbord(
            @RequestParam(value = "dimensionValueID", required = false) String dimensionValueID,
            @RequestParam(value = "parentDimensionID", required = false) String parentDimensionID,
            @RequestParam(value = "attributeID", required = false) String attributeID) {
        logger.info("POST /api/v1/org/getOrgCustomDashbord");
        return organizationService.getOrgCustomDashbord(dimensionValueID, parentDimensionID, attributeID);
    }

    @ApiOperation(value = "获取区域下的所有机构")
    @RequestMapping(value = "getOrganizationListByAreaID", method = RequestMethod.GET)
    public List<OrganizationDto> getOrganizationListByAreaID(
            @RequestParam(value = "areaID", required = true) String areaID,
            @RequestParam(value = "attributeID", required = true) String attributeID) {
        logger.info("GET /api/v1/org/getOrganizationListByAreaID");
        return organizationService.getOrganizationListByAreaID(areaID, attributeID);
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "禁用选中机构信息")
    @RequestMapping(value = "disableOrgs", method = RequestMethod.POST)
    public @ResponseBody
    OperationResultDto disableOrgs(@RequestBody List<String> orgIDs) {
        logger.info("POST /api/v1/org/disableOrgs");
        return organizationService.disableOrgs(orgIDs);
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "启用选中机构信息")
    @RequestMapping(value = "enableOrgs", method = RequestMethod.GET)
    public @ResponseBody
    OperationResultDto enableOrgs(@RequestParam("orgID") String orgID) {
        logger.info("POST /api/v1/org/enableOrgs");
        return organizationService.enableOrgs(orgID);
    }

    @ApiOperation(value = "通过orgId获取一个组织的信息")
    @RequestMapping(value = "displaySingle", method = RequestMethod.GET)
    public @ResponseBody
    OrganizationDto getSingleOrgByOrgID(@RequestParam("orgId") String orgId) {
        logger.info("POST /api/v1/org/displaySingle");
        return organizationService.getSingleOrgByOrgID(orgId);
    }

    @ApiOperation(value = "机构代码唯一性验证")
    @RequestMapping(value = "codeUniqueValidate", method = RequestMethod.POST)
    public @ResponseBody
    Boolean codeUniqueValidate(@RequestBody OrganizationValidateDto validateDto) {
        logger.info("POST /api/v1/org/codeUniqueValidate");
        return validateDto != null && organizationService.codeUniqueValidate(validateDto);
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation(value = "为当前的维度添加现有的机构,可以批量的添加", notes = "机构管理>卡片视角>点击机构+>关联现有机构>确定")
    @RequestMapping(value = "updateOrgToDimension", method = RequestMethod.POST)
    public @ResponseBody
    OperationResultDto updateOrgToDimension(@RequestBody UpdateOrgDimensionDto dto) {
        return organizationService.updateOrgToDimension(dto.getDimensionValueID(), dto.getDimensionID(),
                dto.getOrgDtoList());
    }

    @ApiOperation(value = "增加机构信息")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public @ResponseBody
    OperationResultDto<Object> addOrg(@RequestBody OrganizationDto orgDto) {
        logger.info("POST /api/v1/org/add");
        if (StringUtils.isBlank(orgDto.getClientCode()) && StringUtils.isNotBlank(orgDto.getCode())) {
            orgDto.setClientCode(orgDto.getCode());
        }
        return organizationService.addOrg(orgDto);
    }

    @ApiOperation(value = "更新机构信息")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public @ResponseBody
    OperationResultDto<Object> updateOrg(@RequestBody OrganizationDto orgDto) {
        logger.info("PUT /api/v1/org/update");
        if (StringUtils.isBlank(orgDto.getClientCode()) && StringUtils.isNotBlank(orgDto.getCode())) {
            orgDto.setClientCode(orgDto.getCode());
        }
        return organizationService.updateOrg(orgDto);
    }

    @ApiOperation(value = "删除用户维度权限设置")
    @RequestMapping(value = "deleteUserDimensionValue", method = RequestMethod.POST)
    public @ResponseBody
    Boolean deleteUserDimensionValue(@RequestBody DimensionRoleDto dto,
                                     @RequestParam("userID") String userID) {
        userService.deleteUserDimensionValue(dto, userID);
        return true;
    }

    @ApiOperation(value = "获取机构通用信息", notes = "机构管理>卡片视角>机构视图>点击机构灰色区域文字")
    @RequestMapping(value = "getGeneralInfo", method = RequestMethod.GET)
    public @ResponseBody
    OrgGeneralInfoDto getGeneralInfo(@RequestParam("orgID") String orgID) {
        logger.info("POST /api/v1/org/getGeneralInfo");
        return organizationService.getGeneralInfo(orgID);
    }

    @ResponseBody
    @ApiOperation(value = "获取机构树信息", notes = "科目对应>复制对应关系>弹出层")
    @RequestMapping(value = "getOrgIvhTreeList", method = RequestMethod.GET)
    public List<DevTreeDto> getOrgIvhTreeList(@RequestParam("useType") Integer useType,
                                              @RequestParam("orgSetID") String orgSetID) {
        return organizationService.getOrgIvhTreeList(useType, orgSetID);
    }

}