package org.wcs.mapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.wcs.mapper.AppBaseUserGroupMapper;
import org.wcs.mapper.intf.AppBaseUserGroupService;
import org.wcs.model.po.app.AppBaseUserGroup;
import org.wcs.utils.AppStringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AppBaseUserGroupDao extends ServiceImpl<AppBaseUserGroupMapper, AppBaseUserGroup> implements AppBaseUserGroupService {


    @Override
    public List<AppBaseUserGroup> queryWithLevelMin(Integer groupLevel) {
        if(groupLevel == null) {
            return null;
        }
        LambdaQueryWrapper<AppBaseUserGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(AppBaseUserGroup::getGroupLevel, groupLevel);
        try {
            return super.list(queryWrapper);
        } catch (Exception e) {
            log.error("查询用户组表异常，查询小于用户组等级的数据", e);
            return null;
        }
    }

    @Override
    public int insert(AppBaseUserGroup appBaseUserGroup) {
        if(appBaseUserGroup == null) {
            return 0;
        }
        appBaseUserGroup.setCreateTime(LocalDateTime.now());
        appBaseUserGroup.setUpdateTime(LocalDateTime.now());
        try {
            return super.save(appBaseUserGroup) ? 1 : 0;
        } catch (Exception e) {
            log.error("插入用户组表异常", e);
            return 0;
        }
    }

    /**
     * 删除一个用户组 ---- 不抓取异常
     * @param userGroup 要删除的用户组
     * @return 删除结果
     */
    @Override
    public int deleteByUserGroupThrowException(String userGroup) {
        if(AppStringUtils.isEmpty(userGroup)) {
            throw new RuntimeException("删除用户组表异常，用户组不能为空");
        }
        return super.removeById(userGroup) ? 1 : 0;
    }

    /**
     * 更新用户组表
     * @param appBaseUserGroup 更新用户组条件
     * @return 更新结果
     */
    @Override
    public int update(AppBaseUserGroup appBaseUserGroup) {
        if(appBaseUserGroup == null || AppStringUtils.isEmpty(appBaseUserGroup.getUserGroup())) {
            return 0;
        }
        appBaseUserGroup.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<AppBaseUserGroup> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AppBaseUserGroup::getUserGroup, appBaseUserGroup.getUserGroup());
        updateWrapper.set(AppStringUtils.isNotEmpty(appBaseUserGroup.getGroupName()), AppBaseUserGroup::getGroupName, appBaseUserGroup.getGroupName())
                .set(appBaseUserGroup.getGroupLevel() != null,AppBaseUserGroup::getGroupLevel, appBaseUserGroup.getGroupLevel())
                .set(appBaseUserGroup.getGroupStatus() != null, AppBaseUserGroup::getGroupStatus, appBaseUserGroup.getGroupStatus())
                .set(AppBaseUserGroup::getUpdateTime, appBaseUserGroup.getUpdateTime());
        try {
            return super.update(updateWrapper) ? 1 : 0;
        } catch (Exception e) {
            log.error("更新用户组表异常", e);
            return 0;
        }

    }
}
