package com.crunii.micro.domain.repo.main;

import com.crunii.micro.domain.common.BaseRepo;
import com.crunii.micro.domain.entity.main.TsmOrg;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.math.BigInteger;
import java.util.List;

@Repository
public interface TsmOrgRepo extends BaseRepo<TsmOrg, Long> {


    @Query(value = "select * from tsm_org where org_id=:orgId", nativeQuery = true)
    TsmOrg findOrgById(@Param("orgId") String orgId);

    @Query(value = "select org_id from tsm_org where ori_org_code=:code", nativeQuery = true)
    Long findIdByCode(@Param("code") String code);

    @Query(value = "select org_code from tsm_org where org_id=:id", nativeQuery = true)
    String findCodeById(@Param("id") String id);

    TsmOrg findByOrgCode(String code);

    List<TsmOrg> findByOrgCodeAndState(String orgCode, int state);

    List<TsmOrg> findByOrgIdInAndState(List<Long> orgIdList, int state);

    List<TsmOrg> findByOrgIdIn(List<Long> orgIdList);
    List<TsmOrg> findByParentOrgIdInAndState(List<Long> parentOrgIdList, int state);

    List<TsmOrg> findByParentOrgIdAndState(Long parentOrgId, int state);

    @Transactional
    @Modifying
    @Query(value ="UPDATE tsm_org  SET state = 0 WHERE data_sources = '愉快政'", nativeQuery = true)
    void updateStateForDataSourcesYkz();

    TsmOrg findByOrgIdAndState(Long orgId, int i);


    @Query(value = "select * from tsm_org where zone_id=:zoneId and whether_jurisdiction=1", nativeQuery = true)
    List<TsmOrg> findOrgByZone(@Param("zoneId") Long zoneId);

    @Query(value = "select * from tsm_org where org_name=:orgName", nativeQuery = true)
    TsmOrg getOrgByOrgName(@Param("orgName")String orgName);

    @Query(value = "select * from tsm_org where parent_org_id=?1 order by sort_id", nativeQuery = true)
    List<TsmOrg> findByParentOrgId(Long parentOrgId);

    @Transactional
    @Modifying
    @Query(value ="UPDATE tsm_org  SET manage_people = ?1 WHERE org_id =?2", nativeQuery = true)
    void updateManagePeople(Integer managePeople,Long orgId);
    ;

}
