package com.sifang.modules.sys.extention.permission.handlers;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.setting.dao.MemberRoleDao;
import com.sifang.modules.site.siteInfo.dao.SiteEmployeeDao;
import com.sifang.modules.sunshine.SystemSetting.dao.TraderDao;
import com.sifang.modules.sunshine.SystemSetting.dao.TraderEmployeDao;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.utils.SpringContextHolder;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.extention.permission.PermissionHandler;
import com.sifang.modules.sys.extention.permission.PermissionVo;
import com.sifang.modules.sys.util.CrmUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;


/**
 * 保留平台使用人员自定义的权限的菜单
 */
public class KeepMemberRoleMenusHandler implements PermissionHandler{

    @Override
    public void handler(PermissionVo vo) {
        vo.getData().clear();
        MemberRoleDao memberRoleDao = SpringContextHolder.getBean(MemberRoleDao.class);
        UserDao userDao=SpringContextHolder.getBean(UserDao.class);
        User user=userDao.getById(vo.getUserId());
        if(User.USER_TYPE_PTYG.equals(user.getUserType())){
            // 平台员工
            List<Record> roles= memberRoleDao.getMemberRoleMenus( vo.getUserId());
            for(Record rd : roles){
                String permission = rd.getStr("permission");
                putPermission(permission, vo);
            }
        }else if(User.USER_TYPE_FWSRY.equals(user.getUserType())){
            //服务商人员
            SiteEmployeeDao siteEmployeeDao=SpringContextHolder.getBean(SiteEmployeeDao.class);
           Record records=siteEmployeeDao.getRole(user.getId());
           Record record=memberRoleDao.getSitMemberRoleMenus(records.getStr("site_id"),records.getInt("role"));
           if(record !=null){
               String permissions = record.getStr("permission");
               putPermission(permissions, vo);
           }
        }else if (User.USER_TYPE_KJ.equals(user.getUserType())) {
            String roleId="4";
            Record traderRecord= CrmUtils.getUserTrader(user.getUserType(),user.getId());
            String traderType=traderRecord.getStr("type");
            if("2".equals(traderType)){
                roleId="10";
            }
            TraderDao traderDao = SpringContextHolder.getBean(TraderDao.class);
            List<String> permissions = traderDao.findRolePermission(roleId);
            for (String permission : permissions) {
                putPermission(permission, vo);
            }
        }else if (User.USER_TYPE_ADVISOR.equals(user.getUserType())) {
            TraderEmployeDao traderEmployeDao=SpringContextHolder.getBean(TraderEmployeDao.class);
            String str=traderEmployeDao.getTraderEmpPermission(user.getId());
            if(StringUtils.isNotBlank(str)){
                List<String> permissions= Arrays.asList(str.split(","));
                for (String permission : permissions) {
                    putPermission(permission, vo);
                }
            }
        }else if(User.USER_TYPE_GYS.equals(user.getUserType())){  //供应商
            TraderDao traderDao = SpringContextHolder.getBean(TraderDao.class);
            List<String> permissions = traderDao.findRolePermission(User.USER_TYPE_GYS);
            for (String permission : permissions) {
                putPermission(permission, vo);
            }
        }
    }

    private void putPermission(String permission, PermissionVo vo){
        if (StrKit.notBlank(permission)) {
            String[] perArr = permission.split(",");
            for (String per : perArr) {
                if (!vo.getData().containsKey(per)) {
                    vo.getData().put(per, "1");
                }
            }
        }
    }
}
