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.SignupDTO;
import com.reyun.saas.mob.openapi.dto.UserOutDTO;
import com.reyun.saas.mob.openapi.service.CollaborativeProjectService;
import com.reyun.saas.mob.openapi.service.OpenApiUserService;
import com.reyun.saas.mob.user.domain.User;
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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: hanzepeng
 * @Date: 2022/02/09/13:36
 * @Description: openApi的用户控制层
 */
@Slf4j
@Api(value = "/user", tags = "OpenApi 用户")
@OpenapiCpRestController("user")
public class OpenApiUserController {

    @Autowired
    private OpenApiUserService openApiUserService;

    @Autowired
    private CollaborativeProjectService projectService;

    @Log(value = 1, type = 9, description = "合作项目注册用户接口", insertDB = true)
    @PostMapping("signup")
    @ApiOperation(value = "OpenApi注册用户", produces = "application/json")
    public RestResult signup(@RequestBody SignupDTO signupDTO,
                             @RequestHeader(value = "token",required = false) String token,
                             HttpServletRequest request) {
        try {
            //根据token查询合作项目
            CollaborativeProject project = projectService.findProjectByToken(token);
            signupDTO.checkParam();//校验参数

            User user = openApiUserService.signup(signupDTO, project, request);

            log.info("{}通过openapi创建用户 新用户oid:{} param:{}", project.getProjectName(), user.getOid(), signupDTO);
            return ResultGenerator.genSuccessResult(new HashMap<String, Object>() {{
                put("oid", user.getOid());
            }});
        } catch (CustomException e) {
            return new RestResult(e.getResultCode());
        } catch (Exception e) {
            log.error("openapi创建用户失败! token:{} param:{}", token, signupDTO, e);
            return new RestResult(ResultCode.UNKNOWN_ERROR);
        }
    }

    @Log(value = 4, type = 9, description = "合作项目查询用户列表接口", insertDB = false)
    @GetMapping("users")
    @ApiOperation(value = "OpenApi用户列表", produces = "application/json")
    public RestResult users(@RequestHeader(value = "token",required = false) String token) {
        try {
            //根据token查询合作项目
            CollaborativeProject project = projectService.findProjectByToken(token);

            List<UserOutDTO> userOutDTOS = null;
            List<User> users = openApiUserService.findUsers(project);
            if (!CollectionUtils.isEmpty(users)) {
                userOutDTOS = users.stream().map(user -> {
                    UserOutDTO temp = new UserOutDTO();
                    temp.setUsername(user.getUsername());
                    temp.setOid(user.getOid());
                    temp.setCreateTime(user.getCreateTime());
                    return temp;
                }).collect(Collectors.toList());
            }
            return ResultGenerator.genSuccessResult(userOutDTOS);
        } catch (CustomException e) {
            return new RestResult(e.getResultCode());
        } catch (Exception e) {
            log.error("openapi查询用户列表失败! token:{} ", token, e);
            return new RestResult(ResultCode.UNKNOWN_ERROR);
        }

    }


}
