package com.zhongshu.card.server.core.service.openAPI;

import com.github.microservice.components.data.base.util.PageEntityUtil;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.org.OrganizationUserModel;
import com.zhongshu.card.client.model.org.OrganizationUserSearch;
import com.zhongshu.card.client.openApi.model.OrganizationUserOpenApiModel;
import com.zhongshu.card.client.openApi.params.OrganizationUserOpenApiSearch;
import com.zhongshu.card.server.core.dao.org.OrganizationUserDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.org.OrganizationUser;
import com.zhongshu.card.server.core.domain.org.UserAccount;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * 机构相关的 openAPI
 *
 * @author TRX
 * @date 2024/11/12
 */
@Slf4j
@Service
public class OrganizationUserOpenApiService extends SuperService {

    @Autowired
    private OrganizationUserDao organizationUserDao;

    /**
     * 项目用户列表
     *
     * @param apiSearch
     * @return
     */
    public ResultContent<Page<OrganizationUserOpenApiModel>> projectUserPage(OrganizationUserOpenApiSearch apiSearch) {
        OrganizationUserSearch param = new OrganizationUserSearch();
        if (StringUtils.isEmpty(apiSearch.getProjectCode())) {
            return ResultContent.buildFail("projectCode不能为空");
        }
        String projectOid = getOidByCode(apiSearch.getProjectCode());
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = "unKnow";
        }
        BeanUtils.copyProperties(apiSearch, param);
        param.setProjectOid(projectOid);

        Pageable pageable = PageRequest.of(apiSearch.getNumber(), apiSearch.getSize());
        Page<OrganizationUser> page = organizationUserDao.page(pageable, param);
        return ResultContent.buildSuccess(PageEntityUtil.concurrent2PageModel(page, this::toModel));
    }

    /**
     * 用户详情
     *
     * @param id
     * @return
     */
    public ResultContent<OrganizationUserOpenApiModel> getUserDetailById(String id) {
        OrganizationUser organizationUser = organizationUserDao.findTopById(id);
        if (ObjectUtils.isEmpty(organizationUser)) {
            return ResultContent.buildFail("用户存在");
        }
        return ResultContent.buildSuccess(toModel(organizationUser));
    }

    public OrganizationUserOpenApiModel toModel(OrganizationUser entity) {
        OrganizationUserOpenApiModel model = new OrganizationUserOpenApiModel();
        if (ObjectUtils.isNotEmpty(entity.getId())) {
            BeanUtils.copyProperties(entity, model);

            Organization organization = entity.getOrganization();
            UserAccount user = entity.getUser();
            if (ObjectUtils.isNotEmpty(user)) {
                model.setUserId(user.getUserId());
                model.setPhone(user.getPhone());
                model.setName(user.getName());
                model.setProfilePic(user.getProfilePic());
                model.setSex(user.getSex());
            }
            if (ObjectUtils.isNotEmpty(organization)) {
                model.setOrgName(organization.getName());
                model.setAuthType(organization.getAuthType());
                model.setOrgOid(organization.getOid());
                model.setLogo(organization.getLogo());
            }
        }
        return model;
    }
}
