package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.erp.security.util.SecurityUtil;
import com.zanxiang.game.module.base.pojo.enums.DeleteEnum;
import com.zanxiang.game.module.manage.pojo.params.GameAuthGroupAddUpdateParam;
import com.zanxiang.game.module.manage.pojo.params.GameAuthGroupListParam;
import com.zanxiang.game.module.manage.pojo.vo.GameAuthGroupVO;
import com.zanxiang.game.module.manage.service.IGameAuthGroupService;
import com.zanxiang.game.module.manage.service.IGameAuthRoleGroupService;
import com.zanxiang.game.module.mybatis.entity.GameAuthGroup;
import com.zanxiang.game.module.mybatis.entity.GameAuthRoleGroup;
import com.zanxiang.game.module.mybatis.mapper.GameAuthGroupMapper;
import com.zanxiang.module.util.bean.BeanUtil;
import com.zanxiang.module.util.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author : lingfeng
 * @time : 2024-05-20
 * @description : 授权分组
 */
@Slf4j
@Service
public class GameAuthGroupServiceImpl extends ServiceImpl<GameAuthGroupMapper, GameAuthGroup> implements IGameAuthGroupService {

    @Autowired
    private IGameAuthRoleGroupService gameAuthRoleGroupService;

    @Override
    public IPage<GameAuthGroupVO> listOfPage(GameAuthGroupListParam param) {
        return page(param.toPage(), new QueryWrapper<GameAuthGroup>().lambda()
                .like(Strings.isNotBlank(param.getGroupName()), GameAuthGroup::getGroupName, param.getGroupName())
                .eq(!SecurityUtil.isAdmin(), GameAuthGroup::getCreateBy, SecurityUtil.getUserId())
                .orderByDesc(GameAuthGroup::getCreateTime)
        ).convert(this::toVo);
    }

    private GameAuthGroupVO toVo(GameAuthGroup gameAuthGroup) {
        if (Objects.isNull(gameAuthGroup)) {
            return null;
        }
        return BeanUtil.copy(gameAuthGroup, GameAuthGroupVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addOrUpdate(GameAuthGroupAddUpdateParam param) {
        //分组名称不可重复判断
        if (super.count(new LambdaQueryWrapper<GameAuthGroup>()
                .eq(GameAuthGroup::getGroupName, param.getGroupName())
                .ne(param.getId() != null, GameAuthGroup::getId, param.getId())
        ) > 0) {
            throw new BaseException("参数错误, 分组名称不可重复");
        }
        //修改组名称
        if (param.getId() != null) {
            return super.update(new LambdaUpdateWrapper<GameAuthGroup>()
                    .set(GameAuthGroup::getGroupName, param.getGroupName())
                    .set(GameAuthGroup::getUpdateBy, SecurityUtil.getUserId())
                    .set(GameAuthGroup::getUpdateTime, LocalDateTime.now())
                    .eq(GameAuthGroup::getId, param.getId())
            );
        }
        //组添加
        return super.save(GameAuthGroup.builder()
                .groupName(param.getGroupName())
                .isDelete(DeleteEnum.NO.getCode())
                .createBy(SecurityUtil.getUserId())
                .createTime(LocalDateTime.now())
                .build());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        super.removeById(id);
        gameAuthRoleGroupService.update(new LambdaUpdateWrapper<GameAuthRoleGroup>()
                .set(GameAuthRoleGroup::getIsDelete, DeleteEnum.YES.getCode())
                .set(GameAuthRoleGroup::getUpdateBy, SecurityUtil.getUserId())
                .set(GameAuthRoleGroup::getUpdateTime, LocalDateTime.now())
                .eq(GameAuthRoleGroup::getGroupId, id)
        );
        return Boolean.TRUE;
    }
}
