package com.zd.system.service.impl;

import java.util.List;

import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.system.domain.SysPermitMenu;
import com.zd.system.service.ISysPermitMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.system.mapper.SysPermitMapper;
import com.zd.system.domain.SysPermit;
import com.zd.system.service.ISysPermitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 权限模板Service业务层处理
 *
 * @author liubo
 * @date 2022-03-21
 */
@Service
public class SysPermitServiceImpl implements ISysPermitService {
    @Autowired
    private SysPermitMapper sysPermitMapper;
    @Autowired
    private ISysPermitMenuService sysPermitMenuService;

    /**
     * 查询权限模板
     *
     * @param id 权限模板主键
     * @return 权限模板
     */
    @Override
    public SysPermit selectSysPermitById(Long id) {
        SysPermit permit = sysPermitMapper.selectSysPermitById(id);
        if(permit == null){
            throw new ServiceException("数据不存在，或者参数无效，请刷新重试！");
        }

        SysPermitMenu sysPermitMenu = new SysPermitMenu();
        sysPermitMenu.setPermitId(permit.getId());
        sysPermitMenu.setType(1);
        permit.setPermitMenus(sysPermitMenuService.selectSysPermitMenuList(sysPermitMenu));
        return permit;
    }

    @Override
    public List<SysPermitMenu> selectSysPermitByUserId(Long userId) {
        SysPermitMenu sysPermitMenu = new SysPermitMenu();
        sysPermitMenu.setUserId(userId);
        sysPermitMenu.setType(2);
        return sysPermitMenuService.selectSysPermitMenuList(sysPermitMenu);
    }

    /**
     * 查询权限模板列表
     *
     * @param sysPermit 权限模板
     * @return 权限模板
     */
    @Override
    public List<SysPermit> selectSysPermitList(SysPermit sysPermit) {
        return sysPermitMapper.selectSysPermitList(sysPermit);
    }

    /**
     * 新增权限模板
     *
     * @param sysPermit 权限模板
     * @return 结果
     */
    @Override
    @Transactional
    public SysPermit insertSysPermit(SysPermit sysPermit) {
        //设置其他公共字段
        SaveUtil.setCommonAttr(sysPermit);
        sysPermitMapper.insertSysPermit(sysPermit);

        sysPermitMenuService.insertSysPermitMenuList(sysPermit.getId(), null, sysPermit.getPermitMenus());

        return sysPermit;
    }

    /**
     * 修改权限模板
     *
     * @param sysPermit 权限模板
     * @return 结果
     */
    @Override
    @Transactional
    public SysPermit updateSysPermit(SysPermit sysPermit) {
        sysPermit.setUpdateTime(DateUtils.getNowDate());
        sysPermit.setUpdateBy(SecurityUtils.getUsername());

        sysPermitMapper.updateSysPermit(sysPermit);
        sysPermitMenuService.insertSysPermitMenuList(sysPermit.getId(), null, sysPermit.getPermitMenus());
        return sysPermit;
    }

    /**
     * 批量删除权限模板
     *
     * @param ids 需要删除的权限模板主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysPermitByIds(Long[] ids) {
        for (Long id : ids) {
            sysPermitMenuService.insertSysPermitMenuList(id, null, null);
        }
        return sysPermitMapper.deleteSysPermitByIds(ids);
    }

    /**
     * 删除权限模板信息
     *
     * @param id 权限模板主键
     * @return 结果
     */
    @Override
    public int deleteSysPermitById(Long id) {
        return sysPermitMapper.deleteSysPermitById(id);
    }

}
