package com.zt.plat.module.qms.resource.record.service.recordpermission;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zt.plat.module.qms.resource.record.controller.admin.recordpermission.vo.RecordPermissionPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordpermission.vo.RecordPermissionRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordpermission.vo.RecordPermissionSaveReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.record.dal.dataobject.recordpermission.RecordPermissionDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.resource.record.dal.mapper.RecordPermissionMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 记录权限 Service 实现类
 *
 * @author 后台管理
 */
@Slf4j
@Service
@Validated
public class RecordPermissionServiceImpl implements RecordPermissionService {

    @Resource
    private RecordPermissionMapper recordPermissionMapper;


    // 查询所有记录权限列表
    @Override
    public List<RecordPermissionDO> selectAllList(RecordPermissionDO recordPermissionDO) {
        LambdaQueryWrapper<RecordPermissionDO> wrapper = Wrappers.lambdaQuery();
        if (!ObjectUtils.isEmpty(recordPermissionDO.getSourceId()))
            wrapper.eq(RecordPermissionDO::getSourceId, recordPermissionDO.getSourceId());
        return recordPermissionMapper.selectList(wrapper);
    }

    // 查询管理员-- 单条数据
    @Override
    public List<RecordPermissionDO> selectRecordPermission(RecordPermissionDO recordPermissionDO) {
        LambdaQueryWrapper<RecordPermissionDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(RecordPermissionDO::getTargetId, recordPermissionDO.getTargetId());
        queryWrapper.eq(RecordPermissionDO::getTargetType, recordPermissionDO.getTargetType());
        queryWrapper.eq(RecordPermissionDO::getPermission, recordPermissionDO.getPermission());
        if (!ObjectUtils.isEmpty(recordPermissionDO.getSourceId()))
            queryWrapper.eq(RecordPermissionDO::getSourceId, recordPermissionDO.getSourceId());
        if (!ObjectUtils.isEmpty(recordPermissionDO.getSourceType()))
            queryWrapper.eq(RecordPermissionDO::getSourceType, recordPermissionDO.getSourceType());
        return recordPermissionMapper.selectList(queryWrapper);
    }

    @Override
    public boolean insertRecordPermissionBatch(List<RecordPermissionDO> permissionDOList) {
        return recordPermissionMapper.insertBatch(permissionDOList);
    }

    @Override
    public List<RecordPermissionDO> selectList(LambdaQueryWrapper<RecordPermissionDO> lambdaQueryWrapper) {
        return recordPermissionMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public RecordPermissionRespVO createRecordPermission(RecordPermissionSaveReqVO createReqVO) {
        // 插入
        RecordPermissionDO recordPermission = BeanUtils.toBean(createReqVO, RecordPermissionDO.class);
        recordPermissionMapper.insert(recordPermission);
        // 返回
        return BeanUtils.toBean(recordPermission, RecordPermissionRespVO.class);
    }

    @Override
    public void updateRecordPermission(RecordPermissionSaveReqVO updateReqVO) {
        // 校验存在
        validateRecordPermissionExists(updateReqVO.getId());
        // 更新
        RecordPermissionDO updateObj = BeanUtils.toBean(updateReqVO, RecordPermissionDO.class);
        recordPermissionMapper.updateById(updateObj);
    }

    @Override
    public void deleteRecordPermission(Long id) {
        // 校验存在
        validateRecordPermissionExists(id);
        // 删除
        recordPermissionMapper.deleteById(id);
    }

    @Override
    public void deleteRecordPermissionListByIds(List<Long> ids) {
        // 校验存在
        validateRecordPermissionExists(ids);
        // 删除
        recordPermissionMapper.deleteByIds(ids);
    }

    private void validateRecordPermissionExists(List<Long> ids) {
        List<RecordPermissionDO> list = recordPermissionMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(RECORD_PERMISSION_NOT_EXISTS);
        }
    }

    private void validateRecordPermissionExists(Long id) {
        if (recordPermissionMapper.selectById(id) == null) {
            throw exception(RECORD_PERMISSION_NOT_EXISTS);
        }
    }

    @Override
    public RecordPermissionDO getRecordPermission(Long id) {
        return recordPermissionMapper.selectById(id);
    }

    @Override
    public PageResult<RecordPermissionDO> getRecordPermissionPage(RecordPermissionPageReqVO pageReqVO) {
        return recordPermissionMapper.selectPage(pageReqVO);
    }

}