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

import com.github.microservice.auth.client.model.UserFaceUploadModel;
import com.github.microservice.auth.client.service.UserFaceService;
import com.github.microservice.net.ResultContent;
import com.google.common.collect.Lists;
import com.zhongshu.card.client.model.projectAbout.OrgUserFaceModel;
import com.zhongshu.card.client.model.projectAbout.OrgUserFaceParam;
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.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.base.SuperService;
import com.zhongshu.card.server.core.service.devices.permiss.DevicePermissEventService;
import com.zhongshu.card.server.core.util.BeanUtils;
import com.zhongshu.card.server.core.util.CommonUtil;
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.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目人脸管理
 *
 * @author TRX
 * @date 2024/10/12
 */
@Slf4j
@Service
public class OrgUserFaceService extends SuperService {

    @Autowired
    private OrgUserFaceDao orgUserFaceDao;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private DevicePermissEventService devicePermissEventService;

    @Autowired
    private UserCountDao userCountDao;

    /**
     * 人脸管理类
     */
    @Autowired
    private UserFaceService userFaceService;

    @Autowired
    private CommonService commonService;

    /**
     * 保存用户人脸信息
     *
     * @param param
     * @return
     */
    public ResultContent saveFace(OrgUserFaceParam param) {
        String projectOid = param.getProjectOid();
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = getCurrentProjectOid();
        }
        if (StringUtils.isEmpty(projectOid)) {
            return ResultContent.buildFail("oid不能为空");
        }
        String userId = param.getUserId();
        if (StringUtils.isEmpty(userId)) {
            userId = getCurrentUserId();
        }
        String updateFaceFileId = "";
        OrgUserFace orgUserFace = orgUserFaceDao.findTopByUserIdAndProjectOid(userId, projectOid);
        if (ObjectUtils.isEmpty(orgUserFace)) {
            orgUserFace = new OrgUserFace();
            initEntityNoCheckOid(orgUserFace);
        } else {
            initUpdateEntity(orgUserFace);
            updateFaceFileId = orgUserFace.getUpdateFaceFileId();
        }
        BeanUtils.copyProperties(param, orgUserFace, "id");
        orgUserFace.setProjectOid(projectOid);

        // 所属用户userId
        UserAccount userAccount = userCountDao.findTopByUserId(userId);
        orgUserFace.setUserId(userId);
        if (ObjectUtils.isNotEmpty(userAccount)) {
            orgUserFace.setUserAccount(userAccount);
        }
        String faceBase64 = commonService.getUrlFileBase64(param.getImg());
        // 维护管理权限中心用户的头像信息
        UserFaceUploadModel userFaceUploadModel = new UserFaceUploadModel();
        userFaceUploadModel.setUserId(userId);
        userFaceUploadModel.setFile(faceBase64);
        userFaceUploadModel.setUpdateFaceFileId(updateFaceFileId);
        com.github.microservice.auth.client.content.ResultContent<String> content = userFaceService.upload(userFaceUploadModel);
        if (content.isFailed()) {
            return ResultContent.buildFail(content.getMsg());
        }
        updateFaceFileId = content.getContent();

        orgUserFace.setUpdateFaceFileId(updateFaceFileId);
        orgUserFaceDao.save(orgUserFace);

        String groupId = CommonUtil.UUID();
        Map<String, Object> map = new HashMap<>();
        map.put("groupId", groupId);
        // 通知人脸信息发送变化
        devicePermissEventService.sendUserInfoChangeEvent(
                orgUserFace.getUserId(), orgUserFace.getProjectOid(), map);
        return ResultContent.buildSuccess();
    }

    public List<OrgUserFace> getUserFaces(String userId, String oid) {
        List<OrgUserFace> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(userId) && StringUtils.isNotEmpty(oid)) {
            list = orgUserFaceDao.findByUserIdAndProjectOid(userId, oid);
        }
        return list;
    }

    public ResultContent<OrgUserFaceModel> getUserFaceModel(String userId, String projectOid) {
        if (StringUtils.isEmpty(userId)) {
            // 得到当前用户的userId
            userId = getCurrentUserId();
        }
        if (StringUtils.isEmpty(projectOid)) {
            // 得到header里面的项目oid
            projectOid = getCurrentProjectOid();
        }
        if (StringUtils.isEmpty(projectOid)) {
            return ResultContent.buildFail("projectOid为空");
        }

        OrgUserFace entity = getUserFace(userId, projectOid);
        return ResultContent.buildSuccess(toModel(entity));
    }

    public OrgUserFace getUserFace(String userId, String oid) {
        if (StringUtils.isNotEmpty(userId) && StringUtils.isNotEmpty(oid)) {
            return orgUserFaceDao.findTopByUserIdAndProjectOid(userId, oid);
        }
        return null;
    }

    /**
     * 得到用户的人脸信息
     *
     * @param projectOid
     * @param userIds
     * @return
     */
    public List<OrgUserFace> getUserFaces(String projectOid, List<String> userIds) {
        if (ObjectUtils.isNotEmpty(userIds) && StringUtils.isNotEmpty(projectOid)) {
            return orgUserFaceDao.findByProjectOidAndUserIdIn(projectOid, userIds);
        }
        return Lists.newArrayList();
    }

    public OrgUserFaceModel toModel(OrgUserFace entity) {
        OrgUserFaceModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new OrgUserFaceModel();
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

}
