import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.manage.service.dao.mapper.WxEnterpriseMapper;
import com.gic.haoban.manage.service.dao.mapper.role.TabHaobanRoleMapper;
import com.gic.haoban.manage.service.dao.mapper.role.TabHaobanRoleMenuMapper;
import com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise;
import com.gic.haoban.manage.service.entity.role.TabHaobanRole;
import com.gic.haoban.manage.service.entity.role.TabHaobanRoleMenu;
import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"})
public class HaobanRoleTest0410 {
    
    @Autowired
    private TabHaobanRoleMenuMapper  haobanRoleMenuMapper ;
    @Autowired
    private TabHaobanRoleMapper haobanRoleMapper ;
    @Autowired
    private WxEnterpriseMapper  wxEnterpriseMapper ;


    @Test
    public void test()  {
        List<TabHaobanWxEnterprise>  list = this.wxEnterpriseMapper.listAll() ;
        for(int i=0;i<list.size();i++) {
            this.add(list.get(i).getWxEnterpriseId());
        }
    }

    private List<String> existList(String wxEnterpriseId , Long roleId , List<String> ms) {
        List<String> list = this.haobanRoleMenuMapper.getMenuInMenuCodes(wxEnterpriseId, roleId, ms) ;
        if(null == list) {
            list = new ArrayList<>() ;
        }
        return list ;
    }

    private void add(String wxEnterpriseId) {
        List<TabHaobanRoleMenu> addList = new ArrayList<>() ;
        List<TabHaobanRole>  list =  this.haobanRoleMapper.getListByWxEnterpriseId(wxEnterpriseId,null) ;
        for(TabHaobanRole role : list) {
            if(role.getClerkType()==0) {
                List<String> ms = Arrays.asList("goalSetting","performanceDiagnosis") ;
                List<String> existMenus = this.existList(wxEnterpriseId, role.getRoleId(), ms) ;
                for(String s : ms) {
                    TabHaobanRoleMenu m = new TabHaobanRoleMenu();
                    m.setRoleMenuId(UniqueIdUtils.uniqueLong());
                    m.setMenuCode(s);
                    m.setStatusFlag(1);
                    m.setCreateTime(new Date());
                    m.setUpdateTime(new Date());
                    m.setWxEnterpriseId(wxEnterpriseId);
                    m.setRoleId(role.getRoleId());
                    if(!existMenus.contains(s)) {
                        addList.add(m);
                    }
                }
            } else if(role.getClerkType()==1) {
                List<String> ms = Arrays.asList("goalSetting","performanceDiagnosis") ;
                List<String> existMenus = this.existList(wxEnterpriseId, role.getRoleId(), ms) ;
                for(String s : ms) {
                    TabHaobanRoleMenu m = new TabHaobanRoleMenu() ;
                    m.setRoleMenuId(UniqueIdUtils.uniqueLong());
                    m.setMenuCode(s);
                    m.setStatusFlag(1);
                    m.setCreateTime(new Date());
                    m.setUpdateTime(new Date());
                    m.setWxEnterpriseId(wxEnterpriseId);
                    m.setRoleId(role.getRoleId());
                    if(!existMenus.contains(s)) {
                        addList.add(m);
                    }
                }
            }else if(role.getClerkType()==2) {
                List<String> ms = Arrays.asList("performanceDiagnosis") ;
                List<String> existMenus = this.existList(wxEnterpriseId, role.getRoleId(), ms) ;
                for(String s : ms) {
                    TabHaobanRoleMenu m = new TabHaobanRoleMenu() ;
                    m.setRoleMenuId(UniqueIdUtils.uniqueLong());
                    m.setMenuCode(s);
                    m.setStatusFlag(1);
                    m.setCreateTime(new Date());
                    m.setUpdateTime(new Date());
                    m.setWxEnterpriseId(wxEnterpriseId);
                    m.setRoleId(role.getRoleId());
                    if(!existMenus.contains(s)) {
                        addList.add(m);
                    }
                }
            }
        }
        if(CollectionUtils.isNotEmpty(addList)) {
            System.out.println("*********************"+addList.size());
            this.haobanRoleMenuMapper.insertBatch(addList);
        }
    }
}
