package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppBaseGroupOperationPermissionMapper;
import org.wcs.mapper.intf.AppBaseGroupOperationPermissionService;
import org.wcs.model.bo.db.DataBaseActionIntResult;
import org.wcs.model.po.app.AppBaseGroupOperationPermission;
import org.wcs.model.po.app.AppBaseMenuPermission;
import org.wcs.model.po.app.JoinOperationPermission;

import java.util.List;

/**
 * 应用基础组操作权限表操作接口实现类
 */
@Service
public class AppBaseGroupOperationPermissionDao extends ServiceImpl<AppBaseGroupOperationPermissionMapper, AppBaseGroupOperationPermission> implements AppBaseGroupOperationPermissionService {

    /**
     * 根据组ID查询权限
     * @param groupId 组ID
     * @return 权限列表
     */
    @Override
    public List<JoinOperationPermission> queryByGroupId(String groupId) {
        MPJLambdaWrapper<AppBaseGroupOperationPermission> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(AppBaseGroupOperationPermission.class);
        queryWrapper.select(AppBaseMenuPermission::getPermissionId).select(AppBaseMenuPermission::getPermissionName);
        queryWrapper.select(AppBaseMenuPermission::getMenuId);
        queryWrapper.leftJoin(AppBaseMenuPermission.class, q -> q.eq(AppBaseGroupOperationPermission::getPermissionId, AppBaseMenuPermission::getPermissionId));
        queryWrapper.eq(AppBaseGroupOperationPermission::getUserGroup, groupId);
        try {
            return this.baseMapper.selectJoinList(JoinOperationPermission.class, queryWrapper);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据组ID删除权限
     * @param groupId 组ID
     * @return 删除结果
     */
    @Override
    public DataBaseActionIntResult deleteByGroupId(String groupId) {
        if(groupId == null) {
            return new DataBaseActionIntResult(0, new Exception("组ID不能为空"));
        }
        try {
            LambdaQueryWrapper<AppBaseGroupOperationPermission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AppBaseGroupOperationPermission::getUserGroup, groupId);
            int delete = baseMapper.delete(queryWrapper);
            return new DataBaseActionIntResult(delete);
        } catch (Exception e) {
            return new DataBaseActionIntResult(0, e);
        }
    }

    /**
     * 批量插入权限
     * @param appBaseGroupOperationPermissionList 权限列表
     * @return 插入结果
     */
    @Override
    public DataBaseActionIntResult insert(List<AppBaseGroupOperationPermission> appBaseGroupOperationPermissionList) {
        if(appBaseGroupOperationPermissionList == null || appBaseGroupOperationPermissionList.isEmpty()) {
            return new DataBaseActionIntResult(0, new Exception("权限列表不能为空"));
        }
        try {
            boolean insert = super.saveBatch(appBaseGroupOperationPermissionList);
            if(!insert) {
                return new DataBaseActionIntResult(0);
            }
            return new DataBaseActionIntResult(appBaseGroupOperationPermissionList.size());
        } catch (Exception e) {
            return new DataBaseActionIntResult(0, e);
        }
    }
}
