package com.purui.prims.headless.server.rest;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.common.pojo.enums.SensitiveLevelEnum;
import com.purui.prims.headless.api.pojo.request.DimensionReq;
import com.purui.prims.headless.api.pojo.request.DimensionValueReq;
import com.purui.prims.headless.api.pojo.request.MetaBatchReq;
import com.purui.prims.headless.api.pojo.request.PageDimensionReq;
import com.purui.prims.headless.api.pojo.response.DimensionResp;
import com.purui.prims.headless.api.pojo.response.SemanticQueryResp;
import com.purui.prims.headless.server.pojo.DimensionFilter;
import com.purui.prims.headless.server.pojo.MetaFilter;
import com.purui.prims.headless.server.service.DimensionService;
import com.purui.prims.headless.server.service.SemanticLayerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


@RestController
@RequestMapping("/api/semantic/dimension")
@Api(tags = "维度管理")
public class DimensionController {

    @Autowired
    private DimensionService dimensionService;

    @Autowired
    private SemanticLayerService queryService;


    /**
     * 创建维度
     *
     * @param dimensionReq
     */
    @ApiOperation(value = "创建维度")
    @PostMapping("/createDimension")
    public DimensionResp createDimension(@RequestBody DimensionReq dimensionReq,
                                         HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        return dimensionService.createDimension(dimensionReq, user);
    }

    @ApiOperation(value = "删除维度")
    @PostMapping("/updateDimension")
    public Boolean updateDimension(@RequestBody DimensionReq dimensionReq,
                                   HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        dimensionService.updateDimension(dimensionReq, user);
        return true;
    }

    @PostMapping("/batchUpdateStatus")
    public Boolean batchUpdateStatus(@RequestBody MetaBatchReq metaBatchReq,
                                     HttpServletRequest request,
                                     HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        dimensionService.batchUpdateStatus(metaBatchReq, user);
        return true;
    }

    @PostMapping("/batchUpdateSensitiveLevel")
    public Boolean batchUpdateSensitiveLevel(@RequestBody MetaBatchReq metaBatchReq,
                                             HttpServletRequest request,
                                             HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        dimensionService.batchUpdateSensitiveLevel(metaBatchReq, user);
        return true;
    }

    @ApiOperation(value = "获取维度列表")
    @GetMapping("/getDimensionList/{modelId}")
    public List<DimensionResp> getDimension(@PathVariable("modelId") Long modelId) {
        DimensionFilter dimensionFilter = new DimensionFilter();
        dimensionFilter.setModelIds(Lists.newArrayList(modelId));
        return dimensionService.getDimensions(dimensionFilter);
    }

    @ApiOperation(value = "获取主题域中所有数据集中的维度")
    @GetMapping("/getDimensionInModelCluster/{modelId}")
    public List<DimensionResp> getDimensionInModelCluster(@PathVariable("modelId") Long modelId) {
        return dimensionService.getDimensionInModelCluster(modelId);
    }

    @ApiOperation(value = "根据维度名称获取维度详情")
    @GetMapping("/{modelId}/{dimensionName}")
    public DimensionResp getDimensionDescByNameAndId(@PathVariable("modelId") Long modelId,
                                                     @PathVariable("dimensionName") String dimensionBizName) {
        return dimensionService.getDimension(dimensionBizName, modelId);
    }

    @ApiOperation(value = "查询维度")
    @PostMapping("/queryDimension")
    public PageInfo<DimensionResp> queryDimension(@RequestBody PageDimensionReq pageDimensionReq) {
        return dimensionService.queryDimension(pageDimensionReq);
    }

    @ApiOperation(value = "查询维度值")
    @GetMapping("/queryDimValue")
    public SemanticQueryResp queryDimValue(@RequestBody DimensionValueReq dimensionValueReq,
                                           HttpServletRequest request,
                                           HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        return queryService.queryDimensionValue(dimensionValueReq, user);
    }

    @ApiOperation(value = "删除维度")
    @DeleteMapping("deleteDimension/{id}")
    public Boolean deleteDimension(@PathVariable("id") Long id,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        User user = UserHolder.findUser(request, response);
        dimensionService.deleteDimension(id, user);
        return true;
    }

    @GetMapping("/getAllHighSensitiveDimension")
    public List<DimensionResp> getAllHighSensitiveDimension() {
        MetaFilter metaFilter = new MetaFilter();
        metaFilter.setSensitiveLevel(SensitiveLevelEnum.HIGH.getCode());
        return dimensionService.getDimensions(metaFilter);
    }

}
