package com.bonus.face.service.impl;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.face.domain.FaceDataEntity;
import com.bonus.face.mapper.FaceDataMapper;
import com.bonus.face.service.FaceDataService;
import com.bonus.face.utils.FaceUtils;
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 FaceDataServiceImpl implements FaceDataService {
    @Resource
    private FaceDataMapper faceDataMapper;

    @Resource
    private FaceUtils faceUtils;

    /**
     * 查询
     *
     * @param faceId 主键
     * @return
     */
    @Override
    public AjaxResult selectFaceDataByFaceId(Long faceId) {
        try {
            FaceDataEntity faceData = faceDataMapper.selectFaceDataByFaceId(faceId);
            if (ObjectUtils.isEmpty(faceData)) {
                return AjaxResult.error();
            } else {
                return AjaxResult.success(faceData);
            }
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 查询列表
     *
     * @param faceData
     * @return
     */
    @Override
    public List<FaceDataEntity> selectFaceDataList(FaceDataEntity faceData) {
        return faceDataMapper.selectFaceDataList(faceData);
    }

    /**
     * 新增
     *
     * @param faceData
     * @return 结果
     */
    @Override
    public AjaxResult insertFaceData(FaceDataEntity faceData) {
        try { // 文件类型验证
            int groupAndIdCardUnique = faceDataMapper.isGroupAndIdCardUnique(faceData.getGroupCode(), faceData.getIdCard(), faceData.getFaceId());
            if (groupAndIdCardUnique > 0) {
                return AjaxResult.error("身份证信息已存在");
            }
            if (faceData.getFile() == null || faceData.getFile().isEmpty()) {
                return AjaxResult.error("请选择图片");
            }

            // 校验文件类型
            if (!faceData.getFile().getContentType().startsWith("image/")) {
                return AjaxResult.error("只能上传图片文件");
            }

            // 校验文件大小
            if (faceData.getFile().getSize() > 5 * 1024 * 1024) {
                return AjaxResult.error("文件大小不能超过 5MB");
            }

            return faceUtils.addFace(faceData);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 修改
     *
     * @param faceData
     * @return 结果
     */
    @Override
    public AjaxResult updateFaceData(FaceDataEntity faceData) {
        try {
            int groupAndIdCardUnique = faceDataMapper.isGroupAndIdCardUnique(faceData.getGroupCode(), faceData.getIdCard(), faceData.getFaceId());
            if (groupAndIdCardUnique > 0) {
                return AjaxResult.error("身份证信息已存在");
            }
            int rows = faceDataMapper.updateFaceData(faceData);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    /**
     * 批量删除
     *
     * @param faceIds 需要删除的主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteFaceDataByFaceIds(Long[] faceIds) {
        try {
            int rows = faceDataMapper.deleteFaceDataByFaceIds(faceIds);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }
}
