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

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.projectAbout.orgFace.OrgUserFaceModel;
import com.zhongshu.card.client.model.projectAbout.orgFace.OrgUserFaceParam;
import com.zhongshu.card.client.openApi.model.face.OrgUserFaceOpenApiParam;
import com.zhongshu.card.client.openApi.model.face.OrgUserFaceOpenModel;
import com.zhongshu.card.client.openApi.model.face.UpdateFaceFileIdParam;
import com.zhongshu.card.client.openApi.params.UserOpenApiParam;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.dao.projectAbout.OrgUserFaceDao;
import com.zhongshu.card.server.core.domain.org.OrganizationUser;
import com.zhongshu.card.server.core.domain.org.UserAccount;
import com.zhongshu.card.server.core.domain.projectAbout.OrgUserFace;
import com.zhongshu.card.server.core.service.base.CommonService;
import com.zhongshu.card.server.core.service.projectAbout.OrgUserFaceService;
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.stereotype.Service;

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

/**
 * 用户人脸信息 openAPI
 * @author TRX
 * @date 2025/3/11
 */
@Slf4j
@Service
public class OrgUserFaceOpenService {

    @Autowired
    private OrgUserFaceService orgUserFaceService;

    @Autowired
    private UserCountDao userCountDao;

    @Autowired
    private CommonService commonService;

    @Autowired
    private OrgUserFaceDao orgUserFaceDao;

    public ResultContent<OrgUserFaceModel> saveFace(OrgUserFaceOpenApiParam param) {
        if (StringUtils.isEmpty(param.getUserId())) {
            return ResultContent.buildFail("userId 不能为空");
        }
        UserAccount userAccount = userCountDao.findTopByUserId(param.getUserId());
        if (ObjectUtils.isEmpty(userAccount)) {
            return ResultContent.buildFail("用户不存在");
        }
        // 检查项目信息
        ResultContent<String> resultContent = commonService.checkProjectCanUserByCode(param.getProjectCode());
        if (resultContent.isFailed()) {
            return ResultContent.buildFail(resultContent.getMsg());
        }
        String projectOid = resultContent.getContent();

        OrgUserFaceParam orgUserFaceParam = new OrgUserFaceParam();
        BeanUtils.copyProperties(param, orgUserFaceParam);
        orgUserFaceParam.setProjectOid(projectOid);
        orgUserFaceParam.setIsCanUsed(Boolean.TRUE);
        orgUserFaceParam.setIsManager(Boolean.TRUE);
        return orgUserFaceService.saveFace(orgUserFaceParam);
    }

    public ResultContent<List<OrgUserFaceOpenModel>> faceListQuery(UserOpenApiParam param) {
        if (StringUtils.isEmpty(param.getUserId())) {
            return ResultContent.buildFail("userId不能为空");
        }

        // 检查项目信息
        ResultContent<String> resultContent = commonService.checkProjectCanUserByCode(param.getProjectCode());
        if (resultContent.isFailed()) {
            return ResultContent.buildFail(resultContent.getMsg());
        }
        String projectOid = resultContent.getContent();
        List<OrgUserFace> list = orgUserFaceDao.findByUserIdAndProjectOidOrderByCreateTimeDesc(param.getUserId(), projectOid);
        List<OrgUserFaceOpenModel> models = new ArrayList<>();
        if (ObjectUtils.isNotEmpty(list)) {
            models = list.stream().map(this::toModel).collect(Collectors.toList());
        }
        return ResultContent.buildSuccess(models);
    }

    public ResultContent<OrgUserFaceOpenModel> detailByFaceId(UpdateFaceFileIdParam param) {
        if (StringUtils.isEmpty(param.getUpdateFaceFileId())) {
            return ResultContent.buildFail("updateFaceFileId不能为空");
        }
        OrgUserFace entity = orgUserFaceDao.findTopByUpdateFaceFileId(param.getUpdateFaceFileId());
        return ResultContent.buildSuccess(toModel(entity));
    }

    public ResultContent<OrgUserFaceOpenModel> deleteByFaceId(UpdateFaceFileIdParam param) {
        if (StringUtils.isEmpty(param.getUpdateFaceFileId())) {
            return ResultContent.buildFail("updateFaceFileId不能为空");
        }
        OrgUserFace entity = orgUserFaceDao.findTopByUpdateFaceFileId(param.getUpdateFaceFileId());
        return orgUserFaceService.deleteFace(entity.getId(), false);
    }

    private OrgUserFaceOpenModel toModel(OrgUserFace entity) {
        OrgUserFaceOpenModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new OrgUserFaceOpenModel();
            BeanUtils.copyProperties(entity, model);
            UserAccount userAccount = entity.getUserAccount();
            if (userAccount != null) {
                model.setUserId(userAccount.getUserId());
                model.setUserName(userAccount.getName());
                model.setPhone(userAccount.getPhone());
                model.setSex(userAccount.getSex());
            }
            OrganizationUser organizationUser = entity.getOrganizationUser();
            if (organizationUser != null) {
                model.setCode(organizationUser.getCode());
            }
        }
        return model;
    }

}
