package cn.superdesk.uniorder.dubbo.consumer._permission;

import cn.superdesk.uniorder.common.enums.login.SysLoginTypeEnum;
import cn.superdesk.uniorder.common.pagination.AnBaseQueryCriteria;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.setting.UnifiedService;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionOrg;
import cn.superdesk.uniorder.dto.vo._function.select.SelectOptionVirt;
import cn.superdesk.uniorder.dubbo.facade._permission.DubboPermissionCustomerService;
import cn.superdesk.uniorder.common.security.JwtUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;


/**
 * @Description: 数据权限类？
 * @Author: linjinyu
 * @CreateDate: 9/4/20 3:23 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 9/4/20 3:23 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class PermissionDubboCustomerService {

	@Reference(version = "1.0.0", timeout = 30000, retries = -1)
	private DubboPermissionCustomerService dubboPermissionCustomerService;


	public List<SelectOptionOrg> convertSelectPermOrg(List<Long> outOrgIds) {
		return dubboPermissionCustomerService.convertSelectPermOrg(outOrgIds);
	}

	public List<SelectOptionVirt> convertSelectPermVirt(List<Long> outVirtIds) {
		return dubboPermissionCustomerService.convertSelectPermVirt(outVirtIds);
	}

	public String getPermOutOrgIds(String token) {
		return dubboPermissionCustomerService.getPermOutOrgIds(token);
	}

	public String getPermOutVirtIds(String token) {
		return dubboPermissionCustomerService.getPermOutVirtIds(token);
	}

	public List<SelectOptionOrg> getSelectPermOrgByToken(String token) {
		return dubboPermissionCustomerService.getSelectPermOrgByToken(token);
	}

	public List<SelectOptionVirt> getSelectPermVirtByToken(String token) {
		return dubboPermissionCustomerService.getSelectPermVirtByToken(token);
	}

	public List<UnifiedService> getAsOrderType(String token) {
		return dubboPermissionCustomerService.getAsOrderType(token);
	}

	public String getPermOutSaleId(String token) {
		return dubboPermissionCustomerService.getPermOutSaleId(token);
	}

	public String getPermOutSaleName(String token) {
		return dubboPermissionCustomerService.getPermOutSaleName(token);
	}

	/**
	 * 附加 许可 企业
	 */
	public Boolean addOnPerm(String token, Map<String, Object> params) {

		// 默认 通过
		Boolean isPass = true;

		// mark by linjinyu at 20210312
//		Map<String, Object> tokenVo = JwtUtil.getTokenVo(token);
		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

//		log.info("附加 许可 企业 : {}", JsonUtils.toJson(loginSysUser));

		// tokenVo.get("outLogin").toString().equals("1")
		if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.服务之窗.getKey()) == 0) {

			// mark by linjinyu at 20210312
//			String[] virtIds = tokenVo.get("virtIds").toString().split(",");

			String[] virtIds = loginSysUser.getVirtIds().split(",");

			if (virtIds.length > 0) {

				params.put("outVirtIds", virtIds);
				String[] services = {"LNSS"};
				params.put("outServices", services);

			} else {
				isPass = false;
			}

		}
		// tokenVo.get("outLogin").toString().equals("0")
		else if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.统一订单.getKey()) == 0) {

			String uniPermOutVirtIdsStr = getPermOutVirtIds(token);

			if (StringUtils.isNotBlank(uniPermOutVirtIdsStr)) {
				String[] virtIds = uniPermOutVirtIdsStr.split(",");
				params.put("outVirtIds", virtIds);
			}
		}

		return isPass;
	}

	/**
	 * 附加 许可 企业
	 */
	public Boolean addOnPerm(String token, AnBaseQueryCriteria criteria) {

		// 默认 通过
		Boolean isPass = true;

		LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

		if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.服务之窗.getKey()) == 0) {

			String[] virtIds = loginSysUser.getVirtIds().split(",");

			if (virtIds.length > 0) {
				String[] services = {"LNSS"};

				criteria.setOutVirtIds(virtIds);
				criteria.setOutServices(services);

			} else {
				isPass = false;
			}

		}
		// tokenVo.get("outLogin").toString().equals("0")
		else if (loginSysUser.getOutLogin().compareTo(SysLoginTypeEnum.统一订单.getKey()) == 0) {

			String uniPermOutVirtIdsStr = getPermOutVirtIds(token);

			if (StringUtils.isNotBlank(uniPermOutVirtIdsStr)) {
				String[] virtIds = uniPermOutVirtIdsStr.split(",");
//				params.put("outVirtIds", virtIds);
				criteria.setOutVirtIds(virtIds);
			}
		}

		return isPass;

	}
}
