package com.bonus.system.service.impl;

import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.SysTemplate;
import com.bonus.system.api.domain.SysTemplateMenu;
import com.bonus.system.mapper.SysDeptMapper;
import com.bonus.system.mapper.SysTemplateMapper;
import com.bonus.system.mapper.SysTemplateMenuMapper;
import com.bonus.system.service.ISysTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限模板Service业务层处理
 *
 * @author bonus
 * @date 2025-01-05
 */
@Service
public class SysTemplateServiceImpl implements ISysTemplateService {

    @Autowired
    private SysTemplateMapper sysTemplateMapper;

    @Autowired
    private SysTemplateMenuMapper sysTemplateMenuMapper;

    @Autowired
    SysDeptMapper sysDeptMapper;

    /**
     * 查询权限模板
     *
     * @param id 权限模板ID
     * @return 权限模板
     */
    @Override
    public SysTemplate selectSysTemplateById(Long id) {
        return sysTemplateMapper.selectSysTemplateById(id);
    }

    /**
     * 查询权限模板列表
     *
     * @param sysTemplate 权限模板
     * @return 权限模板
     */
    @Override
    public List<SysTemplate> selectSysTemplateList(SysTemplate sysTemplate) {
        return sysTemplateMapper.selectSysTemplateList(sysTemplate);
    }

    /**
     * 新增权限模板
     *
     * @param sysTemplate 权限模板
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysTemplate(SysTemplate sysTemplate) {
        sysTemplate.setCreateBy(SecurityUtils.getUsername());
        List<Long> menuIds = sysTemplate.getMenuIds();
        if (CollectionUtils.isEmpty(menuIds)) {
            throw new ServiceException("权限不能为空");
        }
        sysTemplateMapper.insertSysTemplate(sysTemplate);
        resetSysTempMenus(sysTemplate, menuIds);
        return resetSysTempMenus(sysTemplate, menuIds);

    }

    /**
     * 修改权限模板
     *
     * @param sysTemplate 权限模板
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysTemplate(SysTemplate sysTemplate) {
        sysTemplate.setUpdateBy(SecurityUtils.getUsername());
        List<Long> menuIds = sysTemplate.getMenuIds();
        if (CollectionUtils.isEmpty(menuIds)) {
            throw new ServiceException("权限不能为空");
        }
        resetSysTempMenus(sysTemplate, menuIds);
        return sysTemplateMapper.updateSysTemplate(sysTemplate);
    }

    /**
     * 重置模板对应的菜单
     */
    private int resetSysTempMenus(SysTemplate sysTemplate, List<Long> menuIds) {
        sysTemplateMenuMapper.deleteByTempId(sysTemplate.getId());
        List<SysTemplateMenu> list = new ArrayList<>();
        SysTemplateMenu sysTempMenu = null;
        for (Long menuId : menuIds) {
            sysTempMenu = new SysTemplateMenu();
            sysTempMenu.setTempId(sysTemplate.getId());
            sysTempMenu.setMenuId(menuId);
            list.add(sysTempMenu);
        }
        return sysTemplateMenuMapper.batchInsert(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSysTemplateByIds(Long[] ids) {
        for (Long tempId : ids) {
            Integer count = sysDeptMapper.getDeptCountByMenuTemplateId(tempId);
            if (null != count && count > 0) {
                SysTemplate sysTemplate = sysTemplateMapper.selectSysTemplateById(tempId);
                throw new ServiceException(sysTemplate.getName() + "下存在公司，请重新分配模板后再尝试删除");
            }
            sysTemplateMenuMapper.deleteByTempId(tempId);
        }
        return sysTemplateMapper.deleteSysTemplateByIds(ids);
    }
}
