package com.persagy.proxy.object.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.persagy.dmp.common.utils.JsonHelper;
import com.persagy.proxy.adm.request.AdmCreateRequest;
import com.persagy.proxy.adm.request.AdmCreateResponse;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.utils.AdmContextUtil;
import com.persagy.proxy.object.model.AdmFloor;
import com.persagy.proxy.object.model.AdmISpace;
import com.persagy.proxy.object.service.IAdmFloorService;
import com.persagy.proxy.object.service.IAdmISpaceService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * Adm 模型空间 API
 * @author lvxy
 * @date 2021/8/16
 */

@RestController
@RequestMapping("/object/ispace")
public class AdmISpaceController {
    @Autowired
    private IAdmISpaceService service;
    @Autowired
    private IAdmFloorService floorService;

    /**
     * 统计
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/count")
    public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception {
        request.setName(AdmISpace.OBJ_TYPE);
        request.setOnlyCount(true);
        return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmISpace.class);
    }
    /**
     * 创建
     * @param AdmISpace
     * @return
     * @throws Exception
     */
    @PostMapping("/create")
    public AdmCreateResponse create(@RequestBody AdmCreateRequest<AdmISpace> AdmISpace) throws Exception {
        List<AdmISpace> vos = CollUtil.newArrayList(AdmISpace.getContent());
        ensureDataInfos(vos);
        vos = service.doInsert(AdmContextUtil.toDmpContext(), AdmISpace.class, vos);
        return AdmCreateResponse.success(vos);
    }

    /**
     * 根据ID删除
     * @param vos
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public AdmResponse delete( @RequestBody List<AdmISpace> vos) throws Exception {
        service.doDelete(AdmContextUtil.toDmpContext(), vos);
        return AdmResponse.success();
    }

    /**
     * 更新分区对象信息
     * @param AdmISpace
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public AdmResponse update(@RequestBody AdmCreateRequest<AdmISpace> AdmISpace) throws Exception {
        List<AdmISpace> vos = CollUtil.newArrayList(AdmISpace.getContent());
        ensureDataInfos(vos);
        vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmISpace.class, vos);
        return AdmResponse.success(vos);
    }

    /**
     * 补偿处理对象属性
     * @param vos
     */
    private void ensureDataInfos(List<AdmISpace> vos) {
        if(CollUtil.isEmpty(vos)) {
            return;
        }
        for(AdmISpace vo:vos) {
            if(StrUtil.isBlank(vo.getName())) {
                vo.setName(vo.getLocalName());
            }
            vo.setClassCode(AdmISpace.OBJ_TYPE);
        }
    }

    /**
     * 根据楼层id查询元空间数据
     * @param param:请求参数
     */
    @PostMapping("/query")
    public AdmResponse query(@RequestBody JSONObject param) throws Exception {
        AdmQueryCriteria request = JsonHelper.toSingleEntity(param, AdmQueryCriteria.class);
        request.setName(AdmISpace.OBJ_TYPE);
        // 如果楼层ID不为空
        String floorId = param.getString("floorId");
        if (StrUtil.isNotBlank(floorId)){
            AdmQueryCriteria floorCriteria = new AdmQueryCriteria();
            floorCriteria.setName(AdmFloor.OBJ_TYPE);
            floorCriteria.addFilters(" id = '" + floorId + "' ");
            AdmResponse floorRes = floorService.query(floorCriteria);
            if(CollUtil.isNotEmpty(floorRes.getContent())){
                // 得到楼层信息，用bimId查询
                AdmFloor floor = (AdmFloor) floorRes.getContent().get(0);
                if(floor!= null && StrUtil.isNotEmpty(floor.getBimIdPre())){
                    request.addFilters(" bimId startwith '" + floor.getBimIdPre() + "' ");
                }
            }
        }
        AdmResponse admResponse = service.doQuery(AdmContextUtil.toDmpContext(), request, AdmISpace.class);
        admResponse.setMessage(floorId);
        return admResponse;
    }

}
