package com.persagy.dmp.rwd.simple.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ParamCheckUtil;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.define.entity.ObjectInfoDefine;
import com.persagy.dmp.rwd.define.service.IObjectInfoService;
import lombok.extern.slf4j.Slf4j;
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 java.util.List;
import java.util.Map;

/**
 * 采集信息查询
 * @author Charlie Yu
 * @date 2021-11-17
 */
@Slf4j
@RestController
@RequestMapping("/api/report/collect")
public class CollectInfoController {

    private IObjectInfoService infoService;

    /**
     * 根据类型查询动态信息点、静态信息点交付比例
     * @param objTypes 对象类型
     * @return
     */
    @PostMapping("/queryCollectReport")
    public CommonResult<Map<String, Long>> queryCollectReport(@RequestBody(required = false) List<String> objTypes) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE, CommonConstant.QUERY_PROJECTID);
        QueryWrapper<ObjectInfoDefine> wrapper = new QueryWrapper<>();
        wrapper.in(CollUtil.isNotEmpty(objTypes), "dt_object.obj_type", objTypes.toArray(new String[0]));
        Map<String, Long> rs = infoService.queryCollectReport(AppContext.getContext().getProjectId(), wrapper);
        return ResultHelper.single(rs);
    }

    /**
     * 查询采集信息
     * @param param {"page": long 页数, "size": long 条数, "objType": "对象基础类型"}
     * @return
     */
    @PostMapping("/queryCollectInfos")
    public CommonResult<List<Map<String, Object>>> queryCollectInfos(@RequestBody Map<String, Object> param) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE, CommonConstant.QUERY_PROJECTID);
        // 获取入参，不传就默认500
        Long page = MapUtil.getLong(param, "page", 1L);
        Long size = MapUtil.getLong(param, "size", 500L);
        String objType = MapUtil.getStr(param, "objType");
        // 分页查询条件
        Page<Map<String, Object>> pageable = new Page(page, size);
        QueryWrapper<ObjectInfoDefine> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(objType), "dt_object.obj_type", objType);
        pageable = infoService.queryCollectInfos(pageable, AppContext.getContext().getProjectId(), wrapper);
        return ResultHelper.multi(pageable.getRecords(), pageable.getTotal());
    }

    /**
     * 查询信息点
     * @param param {"hasValue":"boolean 是否有值“, "classCode":"String 对象类型"}
     * @return
     */
    @PostMapping("/queryInfosByObject")
    public CommonResult<List<ObjectInfoDefine>> queryInfosByObject(@RequestBody Map<String, Object> param) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE, CommonConstant.QUERY_PROJECTID);
        // 获取入参
        boolean hasValue = MapUtil.getBool(param, "hasValue", false);
        String classCode = MapUtil.getStr(param, "classCode");
        QueryWrapper<ObjectInfoDefine> wrapper = new QueryWrapper<>();
        wrapper.exists(hasValue, "(select 1 from dt_object where dt_define_info.class_code = dt_object.class_code " +
                "and json_extract(dt_object.infos, concat('$.', dt_define_info.code)) is not null)");
        List<ObjectInfoDefine> vos = infoService.queryByClassCodes(AppContext.getContext().getProjectId(), CollUtil.newArrayList(classCode), wrapper);
        return ResultHelper.multi(vos);
    }
}
