package com.zhgd.xmgl.modules.basicdata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhgd.jeecg.common.execption.OpenAlertException;
import com.zhgd.xmgl.modules.basicdata.entity.BaseAction;
import com.zhgd.xmgl.modules.basicdata.mapper.BaseActionMapper;
import com.zhgd.xmgl.modules.basicdata.service.IBaseActionService;
import com.zhgd.xmgl.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Description: 系统资源-功能操作
 * @author： pds
 * @date：   2020-10-27
 * @version： V1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseActionServiceImpl extends ServiceImpl<BaseActionMapper, BaseAction> implements IBaseActionService {
    @Autowired
    private BaseActionMapper baseActionMapper;
//    @Autowired
//    private IBaseAuthorityService baseAuthorityService;

    @Override
    public List<BaseAction> findListByMenuId(String menuId) {
        QueryWrapper<BaseAction> queryWrapper = new QueryWrapper();
        queryWrapper.lambda()
                .eq(BaseAction::getMenuId, menuId);
        List<BaseAction> list = baseActionMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Set<String> getMenuCode(String menuId) {
        return baseActionMapper.getMenuCode(menuId);
    }

    @Override
    public void addBaseAction(BaseAction aciton) {
        if (aciton.getPriority() == null) {
            aciton.setPriority(0);
        }
        if (aciton.getStatus() == null) {
            aciton.setStatus(1);
        }
        aciton.setCreateTime(new Date());
        aciton.setUpdateTime(aciton.getCreateTime());
        baseActionMapper.insert(aciton);
        // 同步权限表里的信息
//        baseAuthorityService.saveOrUpdateAuthority(aciton.getActionId(), "action");
    }

    @Override
    public void editBaseAction(BaseAction aciton) {
        BaseAction saved = baseActionMapper.selectById(aciton.getActionId());
        if (saved == null) {
            throw new OpenAlertException(MessageUtil.get("notFindErr"));
        }
        if (aciton.getPriority() == null) {
            aciton.setPriority(0);
        }
        aciton.setUpdateTime(new Date());
        baseActionMapper.updateById(aciton);
//        // 同步权限表里的信息
//        baseAuthorityService.saveOrUpdateAuthority(aciton.getActionId(),"action");
    }

    @Override
    public void deleteBaseAction(String actionId) {
//        baseAuthorityService.removeAuthority(Long.valueOf(actionId), "action");
        baseActionMapper.deleteById(actionId);
    }
}
