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.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.utils.AdmContextUtil;
import com.persagy.proxy.object.model.AdmProject;
import com.persagy.proxy.object.service.IAdmProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目管理
 * @author Charlie Yu
 * @date 2021-08-16
 */
@RestController
@RequestMapping("/object/project")
public class AdmProjectController {

    @Autowired
    private IAdmProjectService service;

    /**
     * 查询项目信息
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/query")
    public AdmResponse query(@RequestParam(value = "projectId",required = false) String projectId,
                             @RequestBody AdmQueryCriteria request) throws Exception {
        request.setName(AdmProject.OBJ_TYPE);
        return service.doQuery(AdmContextUtil.toDmpContext(), request);
    }

    /**
     * 查询项目信息-全部或指定id
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/query/obj")
    public AdmResponse queryObj(@RequestBody AdmQueryCriteria request) throws Exception {
        return query(null, request);
    }


    /**
     * 更新项目信息
     * @param createRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public AdmResponse update(@RequestBody AdmCreateRequest<AdmProject> createRequest) throws Exception {
        List<AdmProject> vos = CollUtil.newArrayList(createRequest.getContent());
        vos = service.doUpdate(AdmContextUtil.toDmpContext(), vos);
        return AdmResponse.success(vos);
    }
}
