package com.gic.haoban.manage.web.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gic.clerk.api.service.ClerkMoveService;
import com.gic.clerk.api.service.ClerkService;
import com.gic.enterprise.api.dto.EnterpriseSettingDTO;
import com.gic.enterprise.api.service.EnterpriseService;
import com.gic.haoban.base.api.common.pojo.dto.WebLoginDTO;
import com.gic.haoban.common.utils.AuthWebRequestUtil;

/**
 * 
* @ClassName: CreatorUtils
* @Description: 商户后端管理员权限
* @author xugh
* @date 2019年10月14日 下午12:48:29
*
 */
@Component
public class CreatorUtils {

    private static Logger logger = LogManager.getLogger(CreatorUtils.class);

    public static String tip = "非创建人不允许此操作";
    private static EnterpriseService enterpriseService;
    private static ClerkService clerkService;
    private static ClerkMoveService clerkMoveService;

    @Autowired
    public void setEnterpriseService(EnterpriseService enterpriseService) {
        CreatorUtils.enterpriseService = enterpriseService;
    }

    @Autowired
    public void setClerkService(ClerkService clerkService) {
        CreatorUtils.clerkService = clerkService;
    }

    @Autowired
    public void setClerkMoveService(ClerkMoveService clerkMoveService) {
        CreatorUtils.clerkMoveService = clerkMoveService;
    }

    public static int getDepartmentRights() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();

        Integer departmemntRights = loginUser.getDepartAuth();
        if (departmemntRights == null) {
            departmemntRights = 3;
        }
        return departmemntRights;
    }

    /**
     * 
    * @Title: getShowClickList
    * @Description: 获取权限范围的管理员ID集合
    * @author xugh
    * @return
    * @throws
     */
    public static List<String> getCreatorIdList(int showSelfFlag) {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String clerkId = loginUser.getClerkId();
        List<String> userList = getList(clerkId);
        if (showSelfFlag == 1) {
            List<String> creatorIdList = new ArrayList<>();
            creatorIdList.add(clerkId);
            if (CollectionUtils.isNotEmpty(userList)) {
                creatorIdList.addAll(userList);
            }
            return creatorIdList;
        }
        EnterpriseSettingDTO enterpriseSetting = enterpriseService
                .getEnterpriseSettingByEnterpriseId(loginUser.getEnterpriseId());
        if (!(enterpriseSetting.getEnableAccessControl() != null && enterpriseSetting.getEnableAccessControl() == 1)) {
            logger.info("权限控制-未启用");
            return null;
        }
        // 判断是否超级管理员
        if (loginUser.getSuperAdmin() == 1) {
            logger.info("权限控制-超级管理员");
            return null;
        }
        // 部门权限范围 1可见本人 2可见部分 3可见所有
        Integer departmemntRights = loginUser.getDepartAuth();
        if (departmemntRights == null) {
            departmemntRights = 3;
        }
        logger.info("权限控制-可见{}", departmemntRights);
        if (departmemntRights == 3) {
            logger.info("权限控制-可见所有");
            return null;
        }
        List<String> creatorIdList = new ArrayList<>();
        if (departmemntRights == 1) {
            creatorIdList.add(clerkId);
            if (CollectionUtils.isNotEmpty(userList)) {
                creatorIdList.addAll(userList);
            }
        }
        if (departmemntRights == 2) {
            creatorIdList.add(clerkId);
            if (CollectionUtils.isNotEmpty(userList)) {
                creatorIdList.addAll(userList);
            }
            List<String> departClerIdList = clerkService.listClerkIdsInSameDeptByClerkId(clerkId);
            if (CollectionUtils.isNotEmpty(departClerIdList)) {
                creatorIdList.addAll(departClerIdList);
                List<String> list = clerkMoveService.clerkList(departClerIdList);
                if (CollectionUtils.isNotEmpty(list)) {
                    creatorIdList.addAll(list);
                }
            }
        }
        logger.info("权限控制,返回创建人creatorIdList={}", creatorIdList);
        return creatorIdList;
    }

    public static boolean isAdmin() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        if (loginUser.getSuperAdmin() == 1) {
            return true;
        }
        return false;
    }

    public static boolean showSelfParam() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        String enterpriseId = loginUser.getEnterpriseId();
        EnterpriseSettingDTO enterpriseSetting = enterpriseService.getEnterpriseSettingByEnterpriseId(enterpriseId);
        if (!(enterpriseSetting.getEnableAccessControl() != null && enterpriseSetting.getEnableAccessControl() == 1)) {
            return false;
        }
        Integer departmemntRights = loginUser.getDepartAuth();
        if (departmemntRights == null) {
            departmemntRights = 3;
        }
        if (departmemntRights == 1) {
            return false;
        }
        return true;
    }

    public static boolean openFlag() {
        WebLoginDTO loginUser = AuthWebRequestUtil.getLoginUser();
        EnterpriseSettingDTO enterpriseSetting = enterpriseService
                .getEnterpriseSettingByEnterpriseId(loginUser.getEnterpriseId());
        if (!(enterpriseSetting.getEnableAccessControl() != null && enterpriseSetting.getEnableAccessControl() == 1)) {
            return false;
        }
        return true;
    }

    public static List<String> getList(String userId) {
        List<String> authorizationClerkIdList = clerkMoveService.clerkList(userId);
        logger.info("查询关联id={},{}", userId, authorizationClerkIdList);
        return authorizationClerkIdList;
    }

}
