package com.persagy.proxy.object.controller;

import cn.hutool.core.collection.CollUtil;
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.AdmProperty;
import com.persagy.proxy.object.service.IAdmPropertyService;
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 javax.ws.rs.QueryParam;
import java.util.List;

/**
 * Adm 资产 API
 * @author lvxy
 * @date 2021/8/16
 */

@RestController
@RequestMapping("/object/property")
public class AdmPropertyController {
    @Autowired
    private IAdmPropertyService service;

    /**
     * 创建
     * @param createRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/create")
    public AdmCreateResponse create(@RequestBody AdmCreateRequest<AdmProperty> createRequest) throws Exception {
        List<AdmProperty> vos = CollUtil.newArrayList(createRequest.getContent());
        vos = service.doInsert(AdmContextUtil.toDmpContext(), AdmProperty.class, vos);
        return AdmCreateResponse.success(vos);
    }

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


    /**
     * 更新
     * @param createRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public AdmResponse update(@RequestBody AdmCreateRequest<AdmProperty> createRequest, @QueryParam("projectId") String projectId) throws Exception {
        List<AdmProperty> vos = CollUtil.newArrayList(createRequest.getContent());
        vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmProperty.class, vos);
        return AdmResponse.success(vos);
    }


    /**
     * 查询
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/query")
    public AdmResponse query(@RequestBody AdmQueryCriteria request) throws Exception {
        request.setName(AdmProperty.OBJ_TYPE);
        return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmProperty.class);
    }


    /**
     * 统计 已经关联岗位的资产数量（"Filters": " not EquipId isNull"），未关联岗位的资产数量（"Filters": " EquipId isNull"）
     * 
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/count")
    public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception {
        request.setName(AdmProperty.OBJ_TYPE);
        request.setOnlyCount(true);
        return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmProperty.class);
    }
}
