package jj.tech.paolu.biz.webadmin.service;

import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

import java.util.List;

import org.mybatis.dynamic.sql.select.render.DefaultSelectStatementProvider;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jj.tech.paolu.biz.webadmin.dao.TreeMapper;
import jj.tech.paolu.biz.webadmin.vo.OrgTree;
import jj.tech.paolu.config.enums.UserType;
import jj.tech.paolu.repository.mybatis.dao.SysAdminMapper;
import jj.tech.paolu.repository.mybatis.dao.SysOrgMapper;
import jj.tech.paolu.repository.mybatis.dao.support.SysAdminDynamicSqlSupport;
import jj.tech.paolu.repository.mybatis.entity.SysAdmin;
import jj.tech.paolu.repository.mybatis.entity.SysOrg;

@Service
public class OrgService {
	@Autowired SysOrgMapper sysOrgMapper;
	@Autowired TreeMapper treeMapper;

	@Autowired
	SysAdminMapper sysAdminMapper;


	public SysAdmin getOrganizationAdmin(String orgId){
		SysAdmin bean =
				sysAdminMapper.selectOne(s->s
						.where(SysAdminDynamicSqlSupport.org_id, isEqualTo(orgId))
						.and(SysAdminDynamicSqlSupport.type, isEqualTo(UserType.ORG_ADMIN.getCode()))
						.limit(1)
				).orElse(null);
		return bean;

	}

	public SysOrg getCompanyOfUser(String userId){
		SysAdmin user = sysAdminMapper.selectByPrimaryKey(userId).get();
		if(user==null){
			throw new RuntimeException("根据userId:"+userId+"无法找到用户");
		}
		return getCompanyOfUser(user);
	}

	public SysOrg getCompanyOfUser(SysAdmin user){
		return getCompany(user.getOrg_id());
	}

	/**
	 * 查询某一个部门所属的公司
	 * @param orgId
	 * @return
	 */
	public SysOrg getCompany(String orgId){
		List<SysOrg> fathers = allFarter(orgId);
		if(fathers.size()>0){
			return fathers.getLast();
		}
		else{
			throw new RuntimeException("根据orgId:"+orgId+"找不到部门组织信息");
		}
	}

	/**
	 * 返回org_id的所有父以上的节点，包括自身
	 * @param org_id
	 * @return
	 */
	public List<SysOrg> allFarter(String org_id) {
		String select_sql =
		"""
			WITH RECURSIVE temp_org AS
			(
			   SELECT *
			    FROM sys_org sun
			    WHERE id = '%s'
			  UNION
			  SELECT p.* FROM temp_org 
			  INNER JOIN sys_org AS p 
			  ON temp_org.pid = p.id
			) SELECT * FROM temp_org
		""";
		String sql = String.format(select_sql, org_id);

		SelectStatementProvider provider =  DefaultSelectStatementProvider
			.withSelectStatement(sql)
			.build();

		//所有父组织
		List<SysOrg>  orgs = sysOrgMapper.selectMany(provider);
		return orgs;
	}
	
	
	public List<OrgTree> allFarterTree(String org_id) {
		String select_sql =
		"""
			WITH RECURSIVE temp_org AS
			(
			   SELECT *
			    FROM sys_org sun
			    WHERE id = '%s'
			  UNION
			  SELECT p.* FROM temp_org 
			  INNER JOIN sys_org AS p 
			  ON temp_org.pid = p.id
			) SELECT * FROM temp_org
		""";
		String sql = String.format(select_sql, org_id);

		SelectStatementProvider provider =  DefaultSelectStatementProvider
			.withSelectStatement(sql)
			.build();

		//所有父组织
		List<OrgTree> list = treeMapper.selectManyOrg(provider);
		return list;
	}
	
	
	public List<SysOrg> allSon(String org_id) {
		String select_sql =
		"""
			WITH RECURSIVE temp_org AS
			(
			   SELECT *
			    FROM sys_org sun
			    WHERE id = '%s'
			  UNION
			  SELECT p.* FROM temp_org 
			  INNER JOIN sys_org AS p 
			  ON temp_org.id = p.pid
			) SELECT * FROM temp_org
		""";
		String sql = String.format(select_sql, org_id);

		SelectStatementProvider provider =  DefaultSelectStatementProvider
			.withSelectStatement(sql)
			.build();

		//所有父组织
		List<SysOrg>  orgs = sysOrgMapper.selectMany(provider);
		return orgs;
	}
	
	public List<OrgTree> allSonTree(String org_id) {
		String select_sql =
		"""
			WITH RECURSIVE temp_org AS
			(
			   SELECT *
			    FROM sys_org sun
			    WHERE id = '%s'
			  UNION
			  SELECT p.* FROM temp_org 
			  INNER JOIN sys_org AS p 
			  ON temp_org.id = p.pid
			) SELECT * FROM temp_org
		""";
		String sql = String.format(select_sql, org_id);

		SelectStatementProvider provider =  DefaultSelectStatementProvider
			.withSelectStatement(sql)
			.build();

		//所有父组织
		List<OrgTree> list = treeMapper.selectManyOrg(provider);
		return list;
	}

}
