package com.reyun.saas.web.controller.openapi;

import com.reyun.saas.common.exception.CustomException;
import com.reyun.saas.common.framework.OpenapiCpRestController;
import com.reyun.saas.common.restResult.RestResult;
import com.reyun.saas.common.restResult.ResultCode;
import com.reyun.saas.common.restResult.ResultGenerator;
import com.reyun.saas.mob.openapi.domain.CollaborativeProject;
import com.reyun.saas.mob.openapi.dto.AppInputDTO;
import com.reyun.saas.mob.openapi.dto.AppOutDTO;
import com.reyun.saas.mob.openapi.service.CollaborativeProjectService;
import com.reyun.saas.mob.openapi.service.OpenApiAppService;
import com.reyun.saas.mob.user.domain.App;
import com.reyun.saas.web.aop.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: hanzepeng
 * @Date: 2022/02/09/14:27
 * @Description: openApi的app控制层
 */
@Slf4j
@Api(value = "/app", tags = "OpenApi App")
@OpenapiCpRestController("app")
public class OpenApiAppController {

    @Autowired
    private OpenApiAppService apiAppService;

    @Autowired
    private CollaborativeProjectService projectService;

    @Log(value = 1, type = 9, description = "合作项目创建App接口", insertDB = true)
    @PostMapping("create")
    @ApiOperation(value = "OpenApi创建app", produces = "application/json")
    public RestResult create(@RequestBody AppInputDTO appInputDTO,
                             @RequestHeader(value = "token",required = false) String token) {
        try {
            //根据token查询合作项目
            CollaborativeProject project = projectService.findProjectByToken(token);
            //校验参数
            appInputDTO.checkParam();
            App app = apiAppService.create(appInputDTO, project);
            String appkey = app.getAppkey();
            log.info("{}通过openapi创建APP appkey:{} param:{}", project.getProjectName(), appkey, appInputDTO);
            return ResultGenerator.genSuccessResult(new HashMap<String, Object>() {{
                put("appkey", appkey);
            }});
        } catch (CustomException e) {
            return new RestResult(e.getResultCode());
        } catch (Exception e) {
            log.error("openapi创建app失败! token:{} param:{}", token, appInputDTO, e);
            return new RestResult(ResultCode.UNKNOWN_ERROR);
        }
    }

    @Log(value = 4, type = 9, description = "合作项目查询App接口", insertDB = false)
    @GetMapping("apps")
    @ApiOperation(value = "OpenApi查询APP列表", produces = "application/json")
    public RestResult apps(@RequestHeader(value = "token",required = false) String token,
                           @RequestParam(value = "oid", required = false) String oid) {
        try {
            if (StringUtils.isEmpty(oid)) {
                return ResultGenerator.genResult(ResultCode.REQUEST_PARAM_ERROR);
            }
            //根据token查询合作项目
            CollaborativeProject project = projectService.findProjectByToken(token);
            List<AppOutDTO> result = null;
            List<App> apps = apiAppService.findAppsByOid(oid);
            if (!CollectionUtils.isEmpty(apps)) {
                result = apps.stream().map(app -> {
                    AppOutDTO temp = new AppOutDTO();
                    temp.setAppkey(app.getAppkey());
                    temp.setAppNmae(app.getAppName());
                    temp.setBid(app.getBid());
                    temp.setCreateTime(app.getCreateTime());
                    return temp;
                }).collect(Collectors.toList());
            }
            return ResultGenerator.genSuccessResult(result);
        } catch (CustomException e) {
            return new RestResult(e.getResultCode());
        } catch (Exception e) {
            log.error("openapi查询app列表失败! token:{} oid:{}", token, oid, e);
            return new RestResult(ResultCode.UNKNOWN_ERROR);
        }
    }


}
