Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / ButtonsServiceImpl.java
package com.newfiber.api.pc.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.commons.CustomException;
import com.newfiber.api.core.commons.ResultCode;
import com.newfiber.api.core.exception.RRException;
import com.newfiber.api.pc.dao.ButtonsRepository;
import com.newfiber.api.pc.dao.MenuButtonsRepository;
import com.newfiber.api.pc.dto.ButtonDTO;
import com.newfiber.api.pc.model.zhz.Buttons;
import com.newfiber.api.pc.model.zhz.MenuButtons;
import com.newfiber.api.pc.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:zzh
 * @CreateDate:2020/11/19 15:36
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ButtonsServiceImpl extends ServiceImpl<ButtonsRepository,Buttons> implements BaseService<Buttons>{

    @Autowired
    private MenuButtonsRepository menuButtonsRepository;

    /**
     * 新增按钮
     * @param buttonDTO
     */
    public void addButtons(ButtonDTO buttonDTO, HttpServletRequest request) {
        String userNo = request.getHeader("userNo");
        if(StringUtils.isEmpty(buttonDTO.getMenuNo())){
            throw new RRException("菜单编号不能为空",602);
        }
        Buttons buttons = new Buttons();
        //存入按钮表以及菜单按钮关联表
        String code = IdUtil.simpleUUID();
        buttons.setButtonNo(code);
        buttons.setButtonEvent(buttonDTO.getButtonEvent());
        buttons.setButtonGrade(buttonDTO.getButtonGrade());
        buttons.setButtonGradeZn(buttonDTO.getButtonGradeZn());
        buttons.setButtonName(buttonDTO.getButtonName());
        buttons.setOrderSort(buttonDTO.getOrderSort());
        buttons.setButtonStyle(buttonDTO.getButtonStyle());
        buttons.setCreateTime(new Date());
        buttons.setRemark(buttonDTO.getRemark());
        boolean insert = this.insert(buttons);
        if(!insert){
            throw new RRException("新增按钮失败",504);
        }
        //将数据插入菜单按钮关联表
        MenuButtons menuButtons = new MenuButtons();
        menuButtons.setButtonNo(code);
        menuButtons.setCreateTime(new Date());
        menuButtons.setMenuNo(buttonDTO.getMenuNo());
        //插入用户编号
        menuButtons.setCreateUserNo(userNo);
        menuButtons.setState(1);
        menuButtons.setStateZn("正在使用");
        Integer meunButtons = menuButtonsRepository.insert(menuButtons);
        if(meunButtons <= 0){
            throw new RRException("关联表数据新增失败",504);
        }
    }

    /**
     * 修改按钮
     * @param buttonDTO
     */
    public void updateButtons(ButtonDTO buttonDTO) {
        Buttons buttons = this.selectById(buttonDTO.getSerial());
        if(StringUtils.isEmpty(buttons)){
            throw new RRException("没有该条按钮主键对应的数据",602);
        }
        buttons.setButtonEvent(buttonDTO.getButtonEvent());
        buttons.setButtonGrade(buttonDTO.getButtonGrade());
        buttons.setButtonGradeZn(buttonDTO.getButtonGradeZn());
        buttons.setButtonName(buttonDTO.getButtonName());
        buttons.setOrderSort(buttonDTO.getOrderSort());
        buttons.setButtonStyle(buttonDTO.getButtonStyle());
        buttons.setUpdateTime(new Date());
        buttons.setRemark(buttonDTO.getRemark());
        boolean byId = this.updateById(buttons);
        if(!byId){
            throw new RRException("修改按钮失败",504);
        }

    }

    /**
     * 删除按钮-同时也删除关联表数据
     * @param serial
     */
    public void deleteButtons(Integer serial) {
        if(StringUtils.isEmpty(serial)){
            throw new RRException("主键不能为空",602);
        }
        Buttons buttons = this.selectById(serial);
        //执行删除操作
        boolean b = this.deleteById(serial);
        EntityWrapper<MenuButtons> wrapper = new EntityWrapper<>();
        wrapper.eq("button_no",buttons.getButtonNo());
        Integer delete = menuButtonsRepository.delete(wrapper);
        if(delete <= 0){
            throw new RRException("删除关联表数据失败",504);
        }
    }


    public List<Buttons> queryButtonsByMenuId(String menuNo) {
        if(StringUtils.isEmpty(menuNo)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        List<String> menuButtons = menuButtonsRepository.selectListByMenuNo(menuNo);
        if(menuButtons.size() <= 0){
            return null;
        }
        EntityWrapper<Buttons> buttonsEntityWrapper = new EntityWrapper<>();
        buttonsEntityWrapper.in("button_no",menuButtons);
        return this.selectList(buttonsEntityWrapper);
    }
}