package com.ebts.web.controller.system;

import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.utils.MapExcelUtil;
import com.ebts.system.entity.RealUniQuery;
import com.ebts.system.entity.RelColumnInfo;
import com.ebts.system.entity.vo.RealInfo;
import com.ebts.system.service.RealQueryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 18209
 * @Date 2021/2/22 18:02
 * @Version 1.0
 */
@RestController
@RequestMapping("/data")
public class RealQueryController extends BaseController {

    @Resource
    private RealQueryService realQueryService;

    /**
     * 用户导出
     *
     * @param realUniQuery
     * @return
     */
    @PutMapping("/real/export")
    public Result<Map<String, Object>> exportReal(@Validated @RequestBody RealUniQuery realUniQuery) {
        Result<List<Map<String, Object>>> result = realQueryService.RealData(realUniQuery, 1);
        List<RelColumnInfo> infoList = realQueryService.getInfoList(realUniQuery.getId());
        if (result.isSuccess()) {
            List<Map<String,String>> infoMap = infoList.stream().map(info->{
                Map<String,String> map = new HashMap<>();
                map.put("label",info.getLabel());
                map.put("prop",info.getProp());
                return map;
            }).collect(Collectors.toList());
            return new MapExcelUtil().exportExcel(result.getData(), result.getMsg(),infoMap);
        } else {
            return Result.error(result.getMsg());
        }
    }

    /**
     * 获取查询基本信息
     *
     * @param id
     * @return
     */
    @GetMapping("/real/{id}")
    public Result<RealInfo> RealInfo(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.error("id不能为空!");
        }
        return realQueryService.RealInfo(id);
    }


    /**
     * 获取查询数据
     *
     * @param realUniQuery
     * @return
     */
    @PutMapping("/real")
    public Serializable RealData(@Validated @RequestBody RealUniQuery realUniQuery) {
        Result<List<Map<String, Object>>> result = realQueryService.RealData(realUniQuery, 2);
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }
}
