package com.microservices.zone.common.controller;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.domain.GenericsAjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.system.api.domain.SysUser;
import com.microservices.zone.common.domain.vo.ZoneVisitsInputVo;
import com.microservices.zone.common.service.IZoneVisitsService;
import com.microservices.zone.dataset.domain.vo.ZoneDataSetProjectVo;
import com.microservices.zone.dataset.service.IZoneDataSetService;
import com.microservices.zone.detail.domain.vo.ZoneBaseDataVo;
import com.microservices.zone.detail.domain.vo.ZoneDetailDataVo;
import com.microservices.zone.detail.service.IZoneDetailService;
import com.microservices.zone.member.domain.vo.ZoneMemberDataVo;
import com.microservices.zone.member.domain.vo.ZoneMemberOpenSearchVo;
import com.microservices.zone.member.domain.vo.ZoneMemberOverviewVo;
import com.microservices.zone.member.service.IZoneMemberService;
import com.microservices.zone.partners.domain.vo.ZonePartnersOverviewVo;
import com.microservices.zone.partners.service.IZonePartnersService;
import com.microservices.zone.project.domain.ZoneProjectType;
import com.microservices.zone.project.domain.vo.ZoneProjectDataVo;
import com.microservices.zone.project.domain.vo.ZoneProjectOpenSearchVo;
import com.microservices.zone.project.domain.vo.ZoneProjectSearchVo;
import com.microservices.zone.project.domain.vo.ZoneStatisticsVo;
import com.microservices.zone.project.service.IZoneProjectService;
import com.microservices.zone.project.service.IZoneProjectTypeService;
import com.microservices.zone.resource.domain.ZoneResource;
import com.microservices.zone.resource.domain.ZoneResourceDomain;
import com.microservices.zone.resource.domain.ZoneResourceType;
import com.microservices.zone.resource.domain.vo.ZoneResourceDataVo;
import com.microservices.zone.resource.domain.vo.ZoneResourceOpenSearchVo;
import com.microservices.zone.resource.service.IZoneResourceDomainService;
import com.microservices.zone.resource.service.IZoneResourceService;
import com.microservices.zone.resource.service.IZoneResourceTypeService;
import com.microservices.zone.specialProject.domain.ZoneSpecialProject;
import com.microservices.zone.specialProject.domain.ZoneSpecialProjectRelevancy;
import com.microservices.zone.specialProject.domain.ZoneSpecialProjectReport;
import com.microservices.zone.specialProject.domain.ZoneSpecialProjectType;
import com.microservices.zone.specialProject.domain.vo.CommonTypeVo;
import com.microservices.zone.specialProject.domain.vo.ZoneSpecialProjectRelevancySearchVo;
import com.microservices.zone.specialProject.domain.vo.ZoneSpecialProjectReportSearchVo;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectRelevancyService;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectReportService;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectService;
import com.microservices.zone.specialProject.service.IZoneSpecialProjectTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 特色专区Controller
 *
 * @author otto
 * @date 2023-03-28
 */
@Api(tags = "特色专区公开接口")
@RestController
@RequestMapping("/open")
public class OpenController extends BaseController {
    @Autowired
    private IZoneMemberService zoneMemberService;
    @Autowired
    private IZonePartnersService zonePartnersService;
    @Autowired
    private IZoneProjectService zoneProjectService;
    @Autowired
    private IZoneProjectTypeService zoneProjectTypeService;
    @Autowired
    private IZoneDetailService zoneDetailService;
    @Autowired
    private IZoneResourceService zoneResourceService;
    @Autowired
    private IZoneDataSetService zoneDataSetService;
    @Autowired
    private IZoneResourceDomainService zoneResourceDomainService;
    @Autowired
    private IZoneResourceTypeService zoneResourceTypeService;
    @Autowired
    private IZoneSpecialProjectTypeService zoneSpecialProjectTypeService;
    @Autowired
    private IZoneSpecialProjectService zoneSpecialProjectService;
    @Autowired
    private IZoneSpecialProjectRelevancyService zoneSpecialProjectRelevancyService;
    @Autowired
    private IZoneSpecialProjectReportService zoneSpecialProjectReportService;
    @Autowired
    private IZoneVisitsService zoneVisitsService;

    /**
     * 新增特色专区用户浏览
     */
    @Log(title = "特色专区用户浏览", businessType = BusinessType.INSERT)
    @PostMapping("/zoneVisits")
    @ApiOperation(value = "新增特色专区用户浏览")
    public AjaxResult add(@RequestBody @Validated ZoneVisitsInputVo zoneVisitsInputVo) {
        return toAjax(zoneVisitsService.insertZoneVisits(zoneVisitsInputVo.toZoneVisits()));
    }


    /**
     * 获取特色专区详情详细信息
     */
    @GetMapping(value = "/{zoneId}")
    @ApiOperation(value = "获取特色专区详细信息")
    public GenericsAjaxResult<ZoneDetailDataVo> getZoneInfo(@PathVariable("zoneId") Long zoneId) {
        return genericsSuccess(zoneDetailService.selectZoneDetailDataById(zoneId));
    }

    /**
     * 获取特色专区列表
     */
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取特色专区列表")
    public GenericsAjaxResult<List<ZoneBaseDataVo>> getZoneList() {
        List<ZoneBaseDataVo> zoneBaseDataVoList = zoneDetailService.selectPublishedZoneBaseList();
        return genericsSuccess(zoneBaseDataVoList);
    }

    /**
     * 通过专区名称获取特色专区详细信息
     */
    @GetMapping(value = "/zoneName/{zoneName}")
    @ApiOperation(value = "通过专区名称获取特色专区详细信息")
    public GenericsAjaxResult<ZoneDetailDataVo> getZoneInfoByName(
            @PathVariable("zoneName") String zoneName) {
        return genericsSuccess(zoneDetailService.selectZoneDetailDataByName(zoneName));
    }

    /**
     * 通过专区标识获取特色专区详细信息
     */
    @GetMapping(value = "/zoneKey/{zoneKey}")
    @ApiOperation(value = "通过专区标识获取特色专区详细信息")
    public GenericsAjaxResult<ZoneDetailDataVo> getZoneInfoByKey(
            @PathVariable("zoneKey") String zoneKey) {
        return genericsSuccess(zoneDetailService.selectZoneDetailDataByKey(zoneKey));
    }

    /**
     * 通过邀请Token获取用户信息
     */
    @GetMapping("/getUserInfoByInvitationToken/{invitationToken}")
    @ApiOperation("通过邀请Token获取用户信息")
    public GenericsAjaxResult<SysUser> getUserInfoByInvitationToken(@PathVariable("invitationToken") String invitationToken) {
        return genericsSuccess(zoneMemberService.getUserInfoByInvitationToken(invitationToken));
    }


    /**
     * 确认邀请操作
     */
    @Log(title = "特色专区会员确认邀请操作", businessType = BusinessType.UPDATE)
    @PutMapping("/{zoneId}/confirmInvitation/{invitationToken}/{isConfirm}")
    @ApiOperation("确认邀请操作")
    public AjaxResult confirmInvitation(@PathVariable("zoneId") Long zoneId,
                                        @PathVariable("invitationToken") String invitationToken,
                                        @PathVariable("isConfirm") Boolean isConfirm) {
        return toAjax(zoneMemberService.confirmInvitation(zoneId, invitationToken, isConfirm));
    }


    /**
     * 查询特色专区项目聚合列表
     */
    @GetMapping("/{zoneId}/project/list")
    @ApiOperation("查询项目聚合列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer")
    })
    public GenericsTableDataInfo<ZoneProjectDataVo> projectList(@ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId,
                                                                ZoneProjectOpenSearchVo zoneProjectOpenSearchVo) {
        ZoneProjectSearchVo zoneProjectSearchVo = zoneProjectOpenSearchVo.toZoneProjectSearchVo(zoneId);
        return zoneProjectService.selectZoneProjectList(zoneProjectSearchVo, true);
    }

    /**
     * 查询特色专区项目聚合列表
     */
    @GetMapping("/{zoneId}/projectByScore/list")
    @ApiOperation("查询项目聚合列表（活跃度排名）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer")
    })
    public GenericsTableDataInfo<JSONObject> projectByScoreList(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId,
            ZoneProjectOpenSearchVo zoneProjectOpenSearchVo) {
        ZoneProjectSearchVo zoneProjectSearchVo = zoneProjectOpenSearchVo.toZoneProjectSearchVo(zoneId);
        return getAllGenericsDataTableToPage(
                zoneProjectService.selectZoneProjectByScoreList(zoneProjectSearchVo, true)
        );
    }

    /**
     * 查询特色专区项目聚合列表
     */
    @GetMapping("/{zoneId}/statistics")
    @ApiOperation("查询专区统计数据")
    public GenericsAjaxResult<ZoneStatisticsVo> zoneStatistics(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId) {
        return genericsSuccess(zoneDetailService.getZoneStatistics(zoneId));
    }

    /**
     * 查询特色专区项目分类列表
     */
    @GetMapping("/{zoneId}/projectType/list")
    @ApiOperation("查询特色专区项目分类列表")
    public GenericsTableDataInfo<ZoneProjectType> projectTypeList(@ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId) {
        ZoneProjectType zoneProjectTypeSearch = new ZoneProjectType();
        zoneProjectTypeSearch.setZoneId(zoneId);
        List<ZoneProjectType> list = zoneProjectTypeService.selectZoneProjectTypeList(zoneProjectTypeSearch);
        return getGenericsDataTable(list);
    }

    /**
     * 获取特色专区会员总览列表
     */
    @GetMapping("/{zoneId}/member/overviewList")
    @ApiOperation("获取特色专区会员总览列表")
    public GenericsTableDataInfo<ZoneMemberOverviewVo> memberOverviewList(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId
            , ZoneMemberOpenSearchVo zoneMemberOpenSearchVo) {
        List<ZoneMemberOverviewVo> list = zoneMemberService.selectZoneMemberOverviewList(zoneId, zoneMemberOpenSearchVo, true);
        return getGenericsDataTable(list);
    }

    /**
     * 获取首页特色专区会员列表
     */
    @GetMapping("/{zoneId}/member/homePageList")
    @ApiOperation("获取首页特色专区会员列表")
    public GenericsTableDataInfo<ZoneMemberDataVo> homePageMemberList(@ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId) {
        List<ZoneMemberDataVo> list = zoneMemberService.selectZoneMemberHomePageMemberList(zoneId,true);
        return getGenericsDataTable(list);
    }

    /**
     * 检查当前用户是否有该专区的权限
     */
    @GetMapping("/zone/{zoneId}/checkZoneRole")
    @ApiOperation("检查当前用户是否有该专区的权限")
    public AjaxResult checkZoneRole(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId
            , HttpServletRequest request) {
        String zoneAdminUrl = zoneDetailService.checkZoneRole(zoneId, request);
        if (StringUtils.isEmpty(zoneAdminUrl)) {
            return AjaxResult.success("用户未拥有该专区权限");
        }
        return AjaxResult.success("用户拥有该专区权限", zoneAdminUrl);
    }

    /**
     * 获取特色专区合作伙伴总览列表
     */
    @GetMapping("/{zoneId}/partners/list")
    @ApiOperation("获取特色专区合作伙伴总览列表")
    public GenericsTableDataInfo<ZonePartnersOverviewVo> partnersList(@ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId) {
        List<ZonePartnersOverviewVo> list = zonePartnersService.selectZonePartnersOverviewList(zoneId);
        return getGenericsDataTable(list);
    }

    /**
     * 查询特色专区资源聚合列表
     */
    @GetMapping("/{zoneId}/resource/list")
    @ApiOperation("查询特色专区资源聚合列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer")
    })
    public GenericsTableDataInfo<ZoneResourceDataVo> resourceList(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId
            , ZoneResourceOpenSearchVo zoneResourceSearchVo) {
        ZoneResource zoneResource = zoneResourceSearchVo.toZoneResource();
        zoneResource.setZoneId(zoneId);
        startPage();
        return zoneResourceService.selectZoneResourceList(zoneResource);
    }

    /**
     * 查询特色专区资源领域列表
     */
    @GetMapping("/{zoneId}/resourceDomain/list")
    @ApiOperation("查询特色专区资源领域列表")
    public GenericsTableDataInfo<ZoneResourceDomain> resourceList(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId,
            @ApiParam(name = "name", value = "资源领域名称") String name) {
        ZoneResourceDomain zoneResourceDomain = new ZoneResourceDomain();
        zoneResourceDomain.setName(name);
        zoneResourceDomain.setZoneId(zoneId);
        startPage();
        List<ZoneResourceDomain> list = zoneResourceDomainService.selectZoneResourceDomainList(zoneResourceDomain);
        return getGenericsDataTable(list);
    }

    /**
     * 查询特色专区资源类别列表
     */
    @GetMapping("/{zoneId}/resourceType/list")
    @ApiOperation("查询特色专区资源类别列表")
    public GenericsTableDataInfo<ZoneResourceType> typeList(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId,
            @ApiParam(name = "name", value = "资源类别名称") String name) {
        ZoneResourceType zoneResourceType = new ZoneResourceType();
        zoneResourceType.setName(name);
        zoneResourceType.setZoneId(zoneId);
        startPage();
        List<ZoneResourceType> list = zoneResourceTypeService.selectZoneResourceTypeList(zoneResourceType, true);
        return getGenericsDataTable(list);
    }

    /**
     * 获取特色专区资源聚合详细信息
     */
    @GetMapping("/resource/detail/{resourceId}")
    @ApiOperation("获取特色专区资源聚合详细信息")
    public GenericsAjaxResult<ZoneResourceDataVo> getInfo(@ApiParam(name = "resourceId", value = "资源Id", required = true) @PathVariable("resourceId") Long resourceId
            , HttpServletRequest request) {
        return genericsSuccess(zoneResourceService.selectOpenZoneResourceDataById(resourceId, request));
    }

    /**
     * 获取特色专区数据集项目列表
     */
    @GetMapping("/{zoneId}/dataSet/projectList")
    @ApiOperation("获取特色专区数据集项目列表")
    public GenericsTableDataInfo<ZoneDataSetProjectVo> getDataSetProjectList(@ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId
            , @ApiParam(name = "search", value = "名称模糊搜索") @RequestParam(value = "search", required = false) String search) {
        return getGenericsDataTable(zoneDataSetService.getDataSetProjectList(zoneId, search));
    }

    /**
     * 获取特色专区聚合项目对应数据集详情
     */
    @GetMapping("/dataSet/{projectId}")
    @ApiOperation("获取特色专区聚合项目对应数据集详情")
    public GenericsAjaxResult<JSONObject> getDataSetDetail(@ApiParam(name = "projectId", value = "项目聚合Id", required = true) @PathVariable("projectId") Long projectId
            , @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        return genericsSuccess(zoneDataSetService.getDataSetDetail(projectId, page, limit));
    }

    /**
     * 获取特色专区专项项目分类列表
     */
    @GetMapping("/{zoneId}/specialProjectType/list")
    @ApiOperation("获取特色专区专项项目分类列表")
    public GenericsTableDataInfo<ZoneSpecialProjectType> selectZoneSpecialProjectTypeList(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId) {
        ZoneSpecialProjectType zoneSpecialProjectType = new ZoneSpecialProjectType();
        zoneSpecialProjectType.setZoneId(zoneId);
        startPage();
        List<ZoneSpecialProjectType> list = zoneSpecialProjectTypeService.selectZoneSpecialProjectTypeList(zoneSpecialProjectType, true);
        return getGenericsDataTable(list);
    }

    /**
     * 获取特色专区专项项目列表
     */
    @GetMapping("/{zoneId}/specialProjectType/{specialProjectTypeId}/specialProjectList")
    @ApiOperation("获取特色专区专项项目列表")
    public GenericsTableDataInfo<ZoneSpecialProject> selectZoneSpecialProjectList(
            @ApiParam(name = "zoneId", value = "专区Id", required = true) @PathVariable("zoneId") Long zoneId,
            @ApiParam(name = "specialProjectTypeId", value = "专项项目分类Id", required = true) @PathVariable("specialProjectTypeId") Long specialProjectTypeId) {
        ZoneSpecialProject zoneSpecialProject = new ZoneSpecialProject();
        zoneSpecialProject.setZoneId(zoneId);
        zoneSpecialProject.setTypeId(specialProjectTypeId);
        startPage();
        List<ZoneSpecialProject> list = zoneSpecialProjectService.selectZoneSpecialProjectList(zoneSpecialProject);
        return getGenericsDataTable(list);
    }

    /**
     * 获取特色专区专项项目关联关系列表
     */
    @GetMapping("/specialProject/{specialProjectId}/specialProjectRelevancyList")
    @ApiOperation("获取特色专区专项项目关联关系列表")
    public GenericsTableDataInfo<ZoneSpecialProjectRelevancy> selectZoneSpecialProjectRelevancyList(
            @ApiParam(name = "specialProjectId", value = "专项项目Id", required = true) @PathVariable("specialProjectId") Long specialProjectId,
            @Validated ZoneSpecialProjectRelevancySearchVo zoneSpecialProjectRelevancySearchVo) {
        zoneSpecialProjectRelevancySearchVo.setSpecialProjectId(specialProjectId);
        startPage();
        List<ZoneSpecialProjectRelevancy> list = zoneSpecialProjectRelevancyService.selectZoneSpecialProjectRelevancyList(zoneSpecialProjectRelevancySearchVo.toZoneSpecialProjectRelevancySearchVo());
        return getGenericsDataTable(list);
    }

    @GetMapping("/ZoneSpecialProjectRelevancyType/list")
    @ApiOperation(value = "获取专项项目关联类型列表")
    public List<CommonTypeVo> zoneSpecialProjectRelevancyTypeList() {
        return zoneSpecialProjectRelevancyService.selectZoneSpecialProjectRelevancyTypeList();
    }

    /**
     * 查询专项项目会议报告列表
     */
    @GetMapping("/ZoneSpecialProjectReport/list")
    @ApiOperation("查询专项项目会议报告列表")
    public GenericsTableDataInfo<ZoneSpecialProjectReport> list(@Validated ZoneSpecialProjectReportSearchVo zoneSpecialProjectReportSearchVo) {
        startPage();
        List<ZoneSpecialProjectReport> list = zoneSpecialProjectReportService.selectZoneSpecialProjectReportList(zoneSpecialProjectReportSearchVo.toZoneSpecialProjectReport());
        return getGenericsDataTable(list);
    }
}
