package com.manage.service.ipml;

import com.manage.dao.Power_MenuMapper;
import com.manage.dao.Power_Sys_DictMapper;
import com.manage.entity.*;
import com.manage.service.Power_MenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:hjl
 * @Date:Creatid in 1:23 2019/4/17
 * @Description:
 */
@Service
public class Power_MenuServiceImpl implements Power_MenuService{
    @Autowired
    private Power_MenuMapper powerMenuMapper;
    @Autowired
    private Power_Sys_DictMapper sysDictMapper;
    @Override
    public List<Power_Menu> queryAllPowerMenu(String sysFlag,Integer roleId) {
        return powerMenuMapper.queryAllPowerMenu(sysFlag,roleId);
    }

    /**
      * @ProjectName:    selectAll
      * @Description:    查询菜单树
      * @Param           Integer sysId:系统id,Integer isEffective:是否可用，此处为null,全查
      * @Return          List<Power_Menu>
      * @Author:         曾文和
      * @CreateDate:     2019/4/17 15:30
      * @UpdateUser:     更新者
      * @UpdateDate:     2019/4/17 15:30
      * @UpdateRemark:   更新说明
      * @Version:        1.0
     */
    @Override
    public List<Power_Menu> selectAll(Integer sysId,Integer isEffective,Integer roleId) {
        return powerMenuMapper.selectAll(null,null,roleId);
    }

    /**
     * @ProjectName:    addMenu
     * @Description:    保存菜单信息
     * @Param           Power_Menu menu
     * @Return
     * @Author:         曾文和
     * @CreateDate:     2019/4/17 15:30
     * @UpdateUser:     更新者
     * @UpdateDate:     2019/4/17 15:30
     * @UpdateRemark:   更新说明
     * @Version:        1.0
     */
    @Override
    public void addMenu(Power_Menu menu,String sysName){
        //添加功能时拼接url
        if(StringUtils.isNoneBlank(menu.getMethod())){
            menu.setMenuDesc("功能");
        }else{
            menu.setMenuDesc("菜单");

        }
        //设置系统id
        if(StringUtils.isNotBlank(sysName) && StringUtils.isNotBlank(menu.getSysFlag())){
            List<Power_Sys_Dict> dictVo = sysDictMapper.selectAllBySysFlagAndSysName(menu.getSysFlag(),sysName);
            if(null != dictVo && !dictVo.isEmpty()){
                menu.setSysId(dictVo.get(0).getDictId());
            }
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        menu.setCreater(user.getUserName());
        menu.setUpdater(user.getUserName());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        menu.setCreateDate(fmt.format(new Date()));
        menu.setUpdateDate(fmt.format(new Date()));
        powerMenuMapper.insert(menu);
    }

    /**
     * @ProjectName:    deleteMenuByMenuId
     * @Description:    根据菜单id删除菜单
     * @Param           Integer menuId：菜单id
     * @Return
     * @Author:         曾文和
     * @CreateDate:     2019/4/17 15:30
     * @UpdateUser:     更新者
     * @UpdateDate:     2019/4/17 15:30
     * @UpdateRemark:   更新说明
     * @Version:        1.0
     */
    @Override
    public void deleteMenuByMenuId(Integer menuId) {
        powerMenuMapper.deleteByPrimaryKey(menuId);
    }

    /**
     * @ProjectName:    updateMenu
     * @Description:    修改菜单信息
     * @Param           Power_Menu menu：菜单对象
     * @Return
     * @Author:         曾文和
     * @CreateDate:     2019/4/17 15:30
     * @UpdateUser:     更新者
     * @UpdateDate:     2019/4/17 15:30
     * @UpdateRemark:   更新说明
     * @Version:        1.0
     */
    @Override
    public void updateMenu(Power_Menu menu,String sysName) {
        //设置系统id
        if(StringUtils.isNotBlank(sysName) && StringUtils.isNotBlank(menu.getSysFlag())){
            List<Power_Sys_Dict> dictVo = sysDictMapper.selectAllBySysFlagAndSysName(menu.getSysFlag(),sysName);
            if(null != dictVo && !dictVo.isEmpty()){
                menu.setSysId(dictVo.get(0).getDictId());
            }
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        menu.setUpdater(user.getUserName());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        menu.setUpdateDate(fmt.format(new Date()));
        //子类系统id和系统标识全改一致
        powerMenuMapper.updateChildSysFlagAndSysId(menu.getMenuId(),menu.getSysId(),menu.getSysFlag());
        powerMenuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public List<Power_Menu> checkMenuNameBySysId(String sysFlag, String menuName) {
        return powerMenuMapper.checkMenuNameBySysId(sysFlag, menuName);
    }

    @Override
    public List<Power_Menu> checkMethodByParentId(Integer parentId, String menuName,String method) {
        return powerMenuMapper.checkMethodByParentId(parentId,menuName,method);
    }

    @Override
    public List<Power_Menu> selectUserAndRoleMenuListPower(Integer userId, String sysFlag) {
        return powerMenuMapper.selectUserAndRoleMenuListPower(userId,sysFlag);
    }
}
