package com.softwarebr.nunu.web.admin;

import com.brframework.commoncms.annatotion.AdminMenu;
import com.brframework.commoncms.core.Icons;
import com.brframework.commonweb.json.JSONResult;
import com.google.common.collect.Lists;
import com.softwarebr.nunu.entity.res.Project;
import com.softwarebr.nunu.json.admin.visual.*;
import com.softwarebr.nunu.service.go.PolicyService;
import com.softwarebr.nunu.service.hall.InvestService;
import com.softwarebr.nunu.service.res.ProjectService;
import com.softwarebr.nunu.service.train.ActivityLogService;
import com.softwarebr.nunu.service.user.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lilin
 * @date 2020-02-26 11:02:46
 */

@RestController
@Api(tags = "可视化数据")
@AdminMenu(menuName = "可视化数据", icon = Icons.TABLE)
@RequestMapping("/admin/access")
public class AdminVisualizationController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private InvestService investService;

    @Autowired
    private ActivityLogService activityLogService;

    @Autowired
    private PolicyService policyService;


    @ApiOperation(value = "数据可视化", notes = "数据可视化", produces = "text/plain")
    @AdminMenu(menuName = "数据可视化", order = 10, url = "chartData")
    @PreAuthorize("hasRole('system-visualization-query')")
    public void visualizationRoute() {
    }

    @ApiOperation(value = "用户注册", notes = "用户注册", produces = "text/plain")
    @GetMapping("/v1/member/register/total")
    public JSONResult<MemberRegisterResult> getMemberRegisterTotalV1(VisualizationQueryParam param) {
        MemberRegisterResult registerResult = memberService.getMemberRegisterTotal(
                param.getMinDateTime(), param.getMaxDateTime(), param.getDays());
        return JSONResult.ok(registerResult);
    }

    @ApiOperation(value = "项目需求资源", notes = "项目需求资源", produces = "text/plain")
    @GetMapping("/v1/res/project/total")
    public JSONResult<List<VisualProjectResult>> getProjectTotalV1() {
        List<VisualProjectResult> projectResults = Lists.newArrayList();
        for (int i = Project.TYPE_TECHNOLOGICAL_DEVELOPMENT; i <= Project.TYPE_RESULTS_TRANSFER_COOPERATION; i++) {
            VisualProjectResult result = new VisualProjectResult();
            result.setTypeTotal(projectService.countProjectByType(i));
            result.setType(i);
            result.setTypeTitle(projectService.typeMap().get(i));
            projectResults.add(result);
        }
        return JSONResult.ok(projectResults);
    }

    @ApiOperation(value = "项目入驻", notes = "项目入驻", produces = "text/plain")
    @GetMapping("/v1/invest/project/total")
    public JSONResult<InvestProjectResult> getInvestProjectTotalV1(VisualizationQueryParam param) {
        InvestProjectResult investProject = investService.getInvestProjectTotal(param.getMinDateTime(), param.getMaxDateTime(), param.getDays());
        return JSONResult.ok(investProject);
    }

    @ApiOperation(value = "活动报名", notes = "活动报名", produces = "text/plain")
    @GetMapping("/v1/activity/apply/total")
    public JSONResult<List<VisualActivityResult>> getActivityApplyTotalV1(VisualizationQueryParam param) {
        List<VisualActivityResult> activityResults = activityLogService.getActivityApplyTotal(param.getMinDateTime(), param.getMaxDateTime());
        return JSONResult.ok(activityResults);
    }


    @ApiOperation(value = "访问人数", notes = "访问人数", produces = "text/plain")
    @GetMapping("/v1/member/visitors/total")
    public JSONResult<VisitorsResult> getMemberVisitorsTotalV1(VisualizationQueryParam param) {
        VisitorsResult memberVisitors = memberService.getMemberVisitorsTotal(param.getMinDateTime(), param.getMaxDateTime(), param.getDays());
        return JSONResult.ok(memberVisitors);
    }


    @ApiOperation(value = "政策收集", notes = "政策收集", produces = "text/plain")
    @GetMapping("/v1/policy/collection/total")
    public JSONResult<List<PolicyCollectionResult>> getPolicyCollectionTotalV1(VisualizationQueryParam param) {
        List<PolicyCollectionResult> policyCollectionResults = policyService.getPolicyCollectionTotal(param.getMinDateTime(), param.getMaxDateTime(), param.getDays());
        return JSONResult.ok(policyCollectionResults);
    }

}
