package com.bonus.core.customer.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.core.autth.config.LeNiuDataPermission;
import com.bonus.core.autth.enums.DataPermissionTypeEnum;
import com.bonus.core.customer.dto.CustOrgPageDTO;
import com.bonus.core.customer.dto.CustOrgTreeDTO;
import com.bonus.core.customer.model.CustOrg;
import com.bonus.core.customer.openapi.vo.OrgQueryVO;
import com.bonus.core.customer.po.QueryOrgsBasicInfoPO;
import com.bonus.core.customer.po.UpdateChildOrgPO;
import com.bonus.core.customer.vo.CustOrgVO;
import com.bonus.core.customer.vo.OrgBasicInfoVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CustOrgMapper extends BaseMapper<CustOrg> {
   void updateChildOrg(@Param("params") UpdateChildOrgPO updateChildOrgPO);

   List<CustOrgTreeDTO> getOrgLazyTree(@Param("superId") Long superId, @Param("effId") Long effId);

   List<CustOrgTreeDTO> listAllOrg();

   @LeNiuDataPermission(
      alias = "org",
      permissionType = DataPermissionTypeEnum.PERMISSION_ORG
   )
   Page<CustOrgVO> pageCustOrgByParams(Page<CustOrg> page, @Param("custOrg") CustOrgPageDTO content);

   Page<CustOrgVO> queryPageCustOrg(Page<CustOrg> page, @Param("custOrg") CustOrgPageDTO content, @Param("userId") Long userId);

   @Select({"SELECT a.org_id, a.org_num, a.org_name, a.org_full_name, a.org_level, b.org_num superNum, a.super_id, a.org_head, a.org_tel, a.remarks, a.if_del, a.uptime from cust_org a LEFT JOIN cust_org b ON a.super_id = b.org_id  ${ew.customSqlSegment} "})
   List<OrgQueryVO> queryOrgsForOpen(@Param("ew") QueryWrapper<CustOrg> queryWrapper);

   boolean superInDescendantNode(@Param("orgFullId") String orgFullId, @Param("superId") Long superId);

   List<CustOrgVO> queryCustOrgVOList(@Param("orgIdList") List<Long> orgIdList);

   List<Long> listDescendantOrgIdByOrgFullId(String orgFullId);

   List<Long> listChildOrgIdByOrgId(Long orgId);

   List<OrgBasicInfoVO> listOrgBasicInfoByParams(@Param("params") QueryOrgsBasicInfoPO params);

   @Select({"SELECT t.* FROM  cust_org t  WHERE t.if_del = 2 and t.super_id = #{superId} order by t.sort"})
   List<CustOrgVO> getCustOrgLazyTree(Long parentId);

   CustOrg getOrgByCustId(@Param("custId") Long custId);
}
