package com.yunfeiyun.agmp.fms.util;

import com.yunfeiyun.agmp.common.core.domain.model.IBaseRole;
import com.yunfeiyun.agmp.common.framework.web.service.ISysRoleService;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import com.yunfeiyun.agmp.fms.domain.FmsBlock;
import com.yunfeiyun.agmp.fms.domain.FmsLand;
import com.yunfeiyun.agmp.fms.service.IFmsBlockService;
import com.yunfeiyun.agmp.fms.service.IFmsLandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class DataAuthUtil
{

    private static final String[] NO_DATA_FILTER_ROLE = new String[]{"admin","fms_super_admin","fms_admin"};

    @Autowired
    private IFmsLandService fmsLandService;
    @Autowired
    private IFmsBlockService fmsBlockService;
    @Autowired
    private ISysRoleService sysRoleService;


    /**
     * 查詢当前登录用户是否需要进行数据权限处理
     * @return
     */
    public boolean isValidate() {
        if ("admin".equals(SecurityUtils.getLoginUser().getUser().getUserLoginname())) {
            return false;
        }
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if(null == requestAttributes){
            return false;
        }
        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
        String dataFilter = httpServletRequest.getHeader("data-filter");
        if(StringUtils.isNotBlank(dataFilter)&&"false".equals(dataFilter)){
            return false;
        }
        String userId = SecurityUtils.getUserId();
        List<IBaseRole> iBaseRoles = sysRoleService.selectRolesByUserId(userId);
        List<String> dataNoFilterRoles = Arrays.asList(NO_DATA_FILTER_ROLE);
        for (IBaseRole iBaseRole : iBaseRoles) {
            if (iBaseRole.isFlag()&&dataNoFilterRoles.contains(iBaseRole.getRoleKey())) {
                return false;
            }
        }
        return true;
    }


    /**
     * 查询分配的基地列表
     */
    public List<String> landList()
    {
        String userId = SecurityUtils.getUserId();
        FmsLand fmsLand = new FmsLand();
        fmsLand.setLandManager(userId);
        List<FmsLand> fmsLands = fmsLandService.selectFmsLandList(fmsLand);
        List<String> list = new ArrayList<>();
        for (FmsLand land : fmsLands) {
            list.add(land.getLandId());
        }
        return list;
    }

    /**
     * 查询基地列表(包含所分配的基地以及所分配地块所属的基地)
     */
    public List<String> landListOfBlock()
    {
        String userId = SecurityUtils.getUserId();
        FmsLand fmsLand = new FmsLand();
        fmsLand.setLandManager(userId);
        List<FmsLand> fmsLands = fmsLandService.selectFmsLandList(fmsLand);
        List<String> list = new ArrayList<>();
        for (FmsLand land : fmsLands) {
            list.add(land.getLandId());
        }

        FmsBlock fmsBlock = new FmsBlock();
        fmsBlock.setBlockManager(userId);
        List<FmsBlock> blocks = fmsBlockService.selectFmsBlockList(fmsBlock);
        for (FmsBlock block : blocks) {
            list.add(block.getLandId());
        }
        return list.stream().distinct().collect(Collectors.toList());
    }

    /**
     * 查询分配的地块列表
     */
    public List<String> blockList()
    {
        String userId = SecurityUtils.getUserId();

        FmsBlock fmsBlock = new FmsBlock();
        fmsBlock.setBlockManager(userId);
        List<FmsBlock> blocks = fmsBlockService.selectFmsBlockList(fmsBlock);
        List<String> list = new ArrayList<>();
        for (FmsBlock block : blocks) {
            list.add(block.getBlockId());
        }

        FmsLand fmsLand = new FmsLand();
        fmsLand.setLandManager(userId);
        List<FmsLand> fmsLands = fmsLandService.selectFmsLandList(fmsLand);
        for (FmsLand land : fmsLands) {
            FmsBlock block = new FmsBlock();
            block.setLandId(land.getLandId());
            List<FmsBlock> blocks1 = fmsBlockService.selectFmsBlockList(block);
            for (FmsBlock block1 : blocks1) {
                list.add(block1.getBlockId());
            }
        }
        return list.stream().distinct().collect(Collectors.toList());
    }

    public boolean checkRole(String roleKey){
        String userId = SecurityUtils.getUserId();
        List<IBaseRole> iBaseRoles = sysRoleService.selectRolesByUserId(userId);
        for (IBaseRole iBaseRole : iBaseRoles) {
            if (iBaseRole.isFlag()&&roleKey.equals(iBaseRole.getRoleKey())) {
                return true;
            }
        }
        return false;
    }
}
