package com.ssm.manage.service.impl;

import com.ssm.manage.dao.*;
import com.ssm.manage.dto.RlPowerDto;
import com.ssm.manage.entity.RlMenuPower;
import com.ssm.manage.entity.RlPower;
import com.ssm.manage.entity.RlRolePower;
import com.ssm.manage.entity.RlRoleStaff;
import com.ssm.manage.mapper.RlPowerMapper;
import com.ssm.manage.service.RlPowerService;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ssm.core.impl.BaseDaoImpl;

import java.io.Serializable;
import java.util.List;

import tk.mybatis.mapper.entity.Example;

/**
 * 权限(RlPower)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 15:34:05
 */
@Service("rlPowerService")
public class RlPowerServiceImpl extends BaseServiceImpl<RlPower> implements RlPowerService {
    @Autowired
    private RlPowerDao rlPowerDao;
    @Autowired
    private RlRoleStaffDao rlRoleStaffDao;

    @Autowired
    private RlRolePowerDao rlRolePowerDao;

    @Autowired
    private RlMenuPowerDao rlMenuPowerDao;

    @Autowired
    private RlPowerMapper rlPowerMapper;

    @Override
    public BaseDao<RlPower> getDao() {
        return rlPowerDao;
    }

    @Override
    public PageInfo<RlPower> selectPage(RlPowerDto dto) {
        Example example = new Example(RlPower.class);
        Example.Criteria criteria = example.createCriteria();
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<RlPower> list = this.rlPowerDao.selectByExample(example);
        PageInfo<RlPower> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public int add(RlPowerDto rlPower) {
        int i = rlPowerMapper.insertPower(rlPower);
        //保存菜单和上下级关系
        RlMenuPower rlMenuPower = rlPower.getRlMenuPower();
        rlMenuPower.setId(rlPower.getPowerId());
        rlMenuPower.setPowerName(rlPower.getPowerName());
        rlMenuPowerDao.insert(rlMenuPower);
        //插入当前角色的id
        RlRoleStaff rlRoleStaff = new RlRoleStaff();
        rlRoleStaff.setStaffId(rlPower.getStaffId());
        List<RlRoleStaff> roleStaffs = rlRoleStaffDao.select(rlRoleStaff);
        for (RlRoleStaff roleStaff : roleStaffs) {
            RlRolePower rlRolePower = new RlRolePower();
            rlRolePower.setPowerId(rlPower.getPowerId());
            rlRolePower.setRoleId(roleStaff.getRoleId());
            rlRolePowerDao.insert(rlRolePower);
        }
        return i;
    }
}
