package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.thmz.UserMenuRelation;
import cn.hnthyy.thmz.mapper.thmz.UserMenuRelationMapper;
import cn.hnthyy.thmz.service.thmz.UserMenuRelationService;
import cn.hnthyy.thmz.vo.UserMenuRelationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class UserMenuRelationServiceImpl implements UserMenuRelationService {
    @SuppressWarnings("all")
    @Autowired
    private UserMenuRelationMapper userMenuRelationMapper;

    @Override
    public UserMenuRelation queryById(Long id) {
        return userMenuRelationMapper.selectById(id);
    }

    @Override
    public List<UserMenuRelation> queryByUserIds(List<Long> userIds) {
        return userMenuRelationMapper.selectByUserIds(userIds);
    }

    @Override
    public List<UserMenuRelation> queryByUserId(Long userId) {
        return userMenuRelationMapper.selectByUserId(userId);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int saveMenuRelation(UserMenuRelationVo userMenuRelationVo, Long createId) throws MzException {
        if (StringUtils.isBlank(userMenuRelationVo.getUserIds())) {
            throw new MzException("保存用户与菜单关系失败,用户集参数为空");
        }
        List<String> userIds = Arrays.asList(userMenuRelationVo.getUserIds().split(","));
        List<String> menuIds =null;
        if(userMenuRelationVo.getMenuIds()!=null){
            menuIds = Arrays.asList(userMenuRelationVo.getMenuIds().split(","));
        }
        Date now = new Date();
        for (String userId : userIds) {
            if (StringUtils.isBlank(userId)) {
                continue;
            }
            Long userIdL = Long.valueOf(userId);
            userMenuRelationMapper.deleteByUserId(userIdL);
            if (menuIds==null|| menuIds.size()==0) {
                continue;
            }
            for (String menuId : menuIds) {
                if (StringUtils.isBlank(menuId)) {
                    continue;
                }
                Long menuIdL = Long.valueOf(menuId);
                UserMenuRelation newUserMenuRelation = new UserMenuRelation();
                newUserMenuRelation.setUserId(userIdL);
                newUserMenuRelation.setMenuId(menuIdL);
                newUserMenuRelation.setCreateTime(now);
                newUserMenuRelation.setCreateId(createId);
                userMenuRelationMapper.insertUserMenuRelation(newUserMenuRelation);
            }
        }
        return 1;
    }

    @Override
    public int removeById(Long id) {
        return userMenuRelationMapper.deleteById(id);
    }

    @Override
    public int removeByUserId(Long userId) {
        return userMenuRelationMapper.deleteByUserId(userId);
    }

    @Override
    public int removeByMenuId(Long menuId) {
        return userMenuRelationMapper.deleteByMenuId(menuId);
    }
}
