package com.huimv.admin.service.primary.impl;

import com.huimv.admin.domain.primary.SysGroupEntity;
import com.huimv.admin.repo.primary.SysGroupEntityRepository;
import com.huimv.admin.repo.primary.SysGroupMenuEntityRepository;
import com.huimv.admin.service.primary.IGroupService;
import com.huimv.admin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class GroupServiceImpl implements IGroupService {

    @Autowired
    private SysGroupEntityRepository sysGroupEntityRepository;
    @Autowired
    private SysGroupMenuEntityRepository sysGroupMenuEntityRepository;

    @Override
    public Result updateGroup(Integer groupId, String groupName, String remark) {
        try {
            SysGroupEntity group = this.sysGroupEntityRepository.findById(groupId).orElse(null);
            if (group == null) {
                return new Result(10002,"请选择数据",false);
            }
            group.setGroupName(groupName);
            group.setRemark(remark);
            sysGroupEntityRepository.save(group);
            return new Result(10000,"修改成功",true);
        }catch (Exception e){
            return new Result(10001,"修改失败",false);
        }
    }

    @Override
    @Transactional
    public Result removeGroup(Integer groupId) {
        try {
            SysGroupEntity group = this.sysGroupEntityRepository.findById(groupId).orElse(null);
            if (group == null) {
                return new Result(10002,"请选择数据",false);
            }
            // 删除权限组
            this.sysGroupEntityRepository.deleteById(groupId);
            // 删除权限组关联菜单
            sysGroupMenuEntityRepository.deleteByGroupId(groupId);
            return new Result(10000,"删除成功",true);
        }catch (Exception e){
            return new Result(10001,"删除失败",false);
        }
    }

    @Override
    public Page<SysGroupEntity> listGroup(int pageNum, int pageSize, String searchStr) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.Direction.DESC, "id");
        if (!StringUtils.isEmpty(searchStr)) {
            return sysGroupEntityRepository.findByGroupNameLike(searchStr, pageable);
        }
        return sysGroupEntityRepository.findAll(pageable);
    }

    @Override
    public Result addGroup(String groupName, String remark) {
        try {
            SysGroupEntity group = new SysGroupEntity();
            group.setGroupName(groupName);
            group.setRemark(remark);
            sysGroupEntityRepository.save(group);
            return new Result(10000,"添加成功",true);
        }catch (Exception e){
            return new Result(10001,"添加失败",false);
        }
    }
}
