package com.wb.permission.domain;

import com.wb.common.WbComUtil;
import com.wb.permission.pojo.WbGroupResource;
import com.wb.permission.pojo.WbUserRole;
import com.wb.util.SysUtil;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Description:
* Author: wangll
* Version: 1.0
* Create Date: 2020-03-09 10:09:44
*/
@Service
public class WbUserRoleBO {
    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    /**
    * Description: 新增和修改
    * @param wbUserRole
    * @return
    * Note: Nothing much.
    */
    public void save(WbUserRole wbUserRole){
        if (StringUtils.isEmpty(wbUserRole.getId())) {
            sqlSessionTemplate.insert("resources/permission.WbUserRoleMapper.insertSelective",wbUserRole);
        }else{
            sqlSessionTemplate.update("resources/permission.WbUserRoleMapper.updateByPrimaryKeySelective",wbUserRole);
        }
    }

    /**
    * Description: 新增
    * @param wbUserRole
    * @return
    * Note: Nothing much.
    */
    public void insert(WbUserRole wbUserRole){
       sqlSessionTemplate.insert("resources/permission.WbUserRoleMapper.insertSelective",wbUserRole);
    }

    /**
    * Description: 修改
    * @param wbUserRole
    * @return
    * Note: Nothing much.
    */
    public void update(WbUserRole wbUserRole){
       sqlSessionTemplate.update("resources/permission.WbUserRoleMapper.updateByPrimaryKeySelective",wbUserRole);
    }

    /**
    * Description: 删除
    * @param id
    * @return
    * Note: Nothing much.
    */
    public void delete(String id){
       sqlSessionTemplate.delete("resources/permission.WbUserRoleMapper.deleteByPrimaryKey",id);
    }

    /**
     * Description: 删除用户角色关系
     * @param tenancyId
     * @param userId
     * @return
     * Note: Nothing much.
     */
    public void deleteByUserId(String tenancyId,String userId){
        Map dataMap = new HashMap();
        if (!StringUtils.isEmpty(tenancyId)){
            dataMap.put("tenancyId",tenancyId);
        }
        if (!StringUtils.isEmpty(userId)){
            dataMap.put("userId",userId);
        }
        sqlSessionTemplate.delete("resources/permission.WbUserRoleMapper.deleteByUserId",dataMap);
    }

    /**
     * Description: 处理用户角色关系
     * @param request
     * @param userId
     * @return
     * Note: Nothing much.
     */
    public List<WbUserRole> proWbUserRole(HttpServletRequest request,String tenancyId,String userId){
        String roleIds = request.getParameter("roleIds");
        List<WbUserRole> wbUserRoleList= new ArrayList();
        if (StringUtils.isEmpty(roleIds)){
            return wbUserRoleList;
        }
        String[] roleArray= roleIds.split(",");
        for (int i=0;i<roleArray.length;i++) {
            WbUserRole wbUserRole = new WbUserRole();
            WbComUtil.setParameterValue(wbUserRole,request);
            wbUserRole.setId(SysUtil.getId());
            wbUserRole.setTenancyId(tenancyId);
            wbUserRole.setUserId(userId);
            wbUserRole.setRoleId(roleArray[i]);
            wbUserRoleList.add(wbUserRole);
        }
        return wbUserRoleList;
    }
}