package net.northking.cctp.project.api.pub;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.project.api.project.service.CpProjectApiService;
import net.northking.cctp.project.db.entity.CpProject;
import net.northking.cctp.project.dto.project.ProjectSimpleInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

@Api(tags = "系统信息（pub）")
@RequestMapping("/pub")
@RestController
public class ProjectPubCtrl {

    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(ProjectPubCtrl.class);

    @Autowired
    private CpProjectApiService apiService;

    @GetMapping(value = "/project/query/info")
    @ApiOperation(value = "系统的基础信息", notes = "处理过程中，projectId进行了url解码（utf8）")
    public ResultWrapper<ProjectSimpleInfo> queryProjectInfo(@RequestParam("projectId") String projectId) throws UnsupportedEncodingException {
        ResultWrapper<ProjectSimpleInfo> wrapper = new ResultWrapper<>();
        if (StringUtils.isNotBlank(projectId)) {
            wrapper.setMessage("缺少系统信息");
        }
        logger.debug("系统信息查询：{}", projectId);
        CpProject project = new CpProject();
        project.setId(URLDecoder.decode(projectId, StandardCharsets.UTF_8.name()));
        ProjectSimpleInfo info = apiService.getProjectInfo(project);
        wrapper.success(info);
        return wrapper;
    }

}
