package com.bonus.face.service.impl;

import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.face.domain.FaceGroupsEntity;
import com.bonus.face.mapper.FaceGroupsMapper;
import com.bonus.face.service.FaceGroupsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service业务层处理
 *
 * @author bonus
 * @date 2024-12-06
 */
@Service
public class FaceGroupsServiceImpl implements FaceGroupsService {
    @Resource
    private FaceGroupsMapper faceGroupsMapper;

    /**
     * 查询
     *
     * @param groupId 主键
     * @return
     */
    @Override
    public AjaxResult selectFaceGroupsByGroupId(Long groupId) {
        try {
            FaceGroupsEntity faceGroups = faceGroupsMapper.selectFaceGroupsByGroupId(groupId);
            if (ObjectUtils.isEmpty(faceGroups)) {
                return AjaxResult.error();
            } else {
                return AjaxResult.success(faceGroups);
            }
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 查询列表
     *
     * @param faceGroups
     * @return
     */
    @Override
    public List<FaceGroupsEntity> selectFaceGroupsList(FaceGroupsEntity faceGroups) {
        return faceGroupsMapper.selectFaceGroupsList(faceGroups);
    }

    /**
     * 新增
     *
     * @param faceGroups
     * @return 结果
     */
    @Override
    public AjaxResult insertFaceGroups(FaceGroupsEntity faceGroups) {
        try {
            int groupNameUnique = faceGroupsMapper.isGroupNameUnique(faceGroups.getGroupName(), faceGroups.getGroupId());
            if (groupNameUnique > 0) {
                return AjaxResult.error("分组名称已存在");
            }
            faceGroups.setGroupCode("GROUP-" + StringUtils.randomUUID().substring(0, 8));
            int rows = faceGroupsMapper.insertFaceGroups(faceGroups);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 修改
     *
     * @param faceGroups
     * @return 结果
     */
    @Override
    public AjaxResult updateFaceGroups(FaceGroupsEntity faceGroups) {
        try {
            int groupNameUnique = faceGroupsMapper.isGroupNameUnique(faceGroups.getGroupName(), faceGroups.getGroupId());
            if (groupNameUnique > 0) {
                return AjaxResult.error("分组名称已存在");
            }
            int rows = faceGroupsMapper.updateFaceGroups(faceGroups);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 批量删除
     *
     * @param groupIds 需要删除的主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteFaceGroupsByGroupIds(Long[] groupIds) {
        try {
            for (Long groupId : groupIds) {
                FaceGroupsEntity faceGroupsEntity = faceGroupsMapper.selectFaceGroupsByGroupId(groupId);
                if (ObjectUtils.isNotEmpty(faceGroupsEntity)) {
                    int i = faceGroupsMapper.deleteFaceDataByGroupCode(faceGroupsEntity.getGroupCode());
                }
            }
            int rows = faceGroupsMapper.deleteFaceGroupsByGroupIds(groupIds);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }
}
