package com.wb.permission.domain;

import com.wb.common.WbComUtil;
import com.wb.permission.pojo.WbGroupResource;
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-05 23:41:40
*/
@Service
public class WbGroupResourceBO {
    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

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

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

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

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

    /**
     * Description:处理数据
     * @param request
     * @return
     * Note: Nothing much.
     */
    public List<WbGroupResource> proWbGroup(HttpServletRequest request){
        String groupCode = request.getParameter("groupCode");
        String relationGroupCodes = request.getParameter("relationGroupCodes");
        List<WbGroupResource> wbGroupResourceList = new ArrayList<>();
        String[] groupArray= relationGroupCodes.split(",");
        for (int i=0;i<groupArray.length;i++) {
            WbGroupResource wbGroupResource = new WbGroupResource();
            wbGroupResource.setGroupCode(groupCode);
            wbGroupResource.setRelationGroupCode(groupArray[i]);
            WbComUtil.setParameterValue(wbGroupResource,request);
            wbGroupResourceList.add(wbGroupResource);
        }
        return wbGroupResourceList;
    }

    /**
     * Description: 删除数据通过租户，组代码
     * @param tenancyId
     * @param groupCode
     * @param relationGroupCode
     * @return
     * Note: Nothing much.
     */
    public void delWbGroupResourceByGroupCode(String tenancyId,String groupCode,String relationGroupCode){
        Map dataMap = new HashMap();
        if (!StringUtils.isEmpty(tenancyId)){
            dataMap.put("tenancyId",tenancyId);
        }
        if (!StringUtils.isEmpty(groupCode)){
            dataMap.put("groupCode",groupCode);
        }
        if (!StringUtils.isEmpty(relationGroupCode)){
            dataMap.put("relationGroupCode",relationGroupCode);
        }
        sqlSessionTemplate.delete("resources/permission.WbGroupResourceMapper.delWbGroupResourceByGroupCode",dataMap);
    }

}