<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~      Copyright (c) 2018-2025, hnqz All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: hnqz
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qunzhixinxi.hnqz.daemon.quartz.mapper.SysDeptMapper">
	<resultMap id="sysDeptMap" type="com.qunzhixinxi.hnqz.daemon.quartz.entity.SysDept">
		<id property="deptId" column="dept_id"/>
		<result property="name" column="name"/>
		<result property="sort" column="sort"/>
		<result property="createTime" column="create_time"/>
		<result property="updateTime" column="update_time"/>
		<result property="delFlag" column="del_flag"/>
		<result property="parentId" column="parent_id"/>
		<result property="tenantId" column="tenant_id"/>
		<result property="deptCode" column="dept_code"/>
		<result property="level" column="level"/>
		<result property="level1Name" column="level1_name"/>
		<result property="level1Id" column="level1_id"/>
		<result property="level2Name" column="level2_name"/>
		<result property="level2Id" column="level2_id"/>
		<result property="level3Name" column="level3_name"/>
		<result property="level3Id" column="level3_id"/>
		<result property="appId" column="app_id"/>
		<result property="appSecret" column="app_secret"/>
		<result property="rsaPublicKey" column="rsa_public_key"/>
		<result property="queryUrl" column="query_url"/>
		<result property="subjectName" column="subject_name"/>
		<result property="subjectType" column="subject_type"/>
		<result property="taxCode" column="tax_code"/>
	</resultMap>

	<select id="selectDistinctDeptId" resultMap="sysDeptMap">
		select
		DISTINCT dept_id,
		level
		from
		sys_dept
		where
		dept_id = #{query.deptId,jdbcType=INTEGER}
		order by level
		limit 1
	</select>

	<select id="selectDeptList" resultMap="sysDeptMap">
		select
		dept_id,
		sort,
		name
		from
		sys_dept
	</select>

	<select id="selectDeptByIds" resultMap="sysDeptMap">
  		select
  		*
   		from
   		sys_dept
   		where
   		dept_id = #{query.deptId,jdbcType=INTEGER}
	</select>

	<select id="selectDeptByName" resultMap="sysDeptMap">
  		select
  		dept_id,name
   		from
   		sys_dept
   		where
   		dept_id = #{query.deptId,jdbcType=INTEGER}
	</select>

	<select id="selectDept" resultMap="sysDeptMap">
  		select
  		*
   		from
   		sys_dept
   		where
   		dept_id = #{deptId,jdbcType=INTEGER}
	</select>

	<select id="selectListByDept" resultMap="sysDeptMap">
		select
		dept_id,
		name
		from
		sys_dept
		where del_flag = 0
	</select>

	<select id="selectDeptById" resultType="java.util.HashMap">
  		SELECT
		d.dept_id as deptId,d.name,d.level,
		CASE WHEN a.zj_number IS NULL THEN e.entcode ELSE a.zj_number END entcode
		FROM
		sys_dept d
		LEFT JOIN wm_da_agent a ON d.dept_id = a.dept_id
		LEFT JOIN wm_da_drug_ent e ON d.dept_id = e.dept_id
		WHERE
		d.dept_id = #{query.deptId,jdbcType=INTEGER}
	</select>

	<select id="getAllParentDept" resultMap="sysDeptMap">
		select DISTINCT
		dept_id,
		`name`,
		`level`
		from sys_dept
		where dept_id IN
		(select d.dept_id  from sys_dept d
		where d.dept_id=#{query.deptId}
		union
		select r.up_id from sys_dept d
		left join wm_relation r on d.dept_id=r.down_id
		where d.dept_id=#{query.deptId}
		union
		select r2.up_id from sys_dept d
		left join wm_relation r on d.dept_id=r.down_id
		left join wm_relation r2 on r.up_id=r2.down_id
		where d.dept_id=#{query.deptId}
				)
		and del_flag=0
		AND dept_id NOT in(1)
	</select>

	<select id="selectByName" resultMap="sysDeptMap">
		select
		*
		from
		sys_dept
		where
		name = #{name,jdbcType=VARCHAR}
	</select>
	<select id="selectEntCodeByLevelId" resultType="string">
		<choose>
			<when test="level = 1">
				SELECT `entcode` FROM `wm_da_drug_ent`
			</when>
			<otherwise>
				SELECT `zj_number` FROM `wm_da_agent`
			</otherwise>
		</choose>
		WHERE `id` = #{levelId}
	</select>

	<update id="updateByPrimaryKeySelective" parameterType="com.qunzhixinxi.hnqz.admin.api.entity.SysDept" >
		update sys_dept
		<set >
			<if test="delFlag != null" >
				del_flag = #{delFlag,jdbcType=VARCHAR},
			</if>
			<if test="name != null" >
				name = #{name,jdbcType=VARCHAR},
			</if>
			<if test="subjectType != null" >
				subject_type = #{subjectType,jdbcType=VARCHAR},
			</if>
			<if test="taxCode != null" >
				tax_code = #{taxCode,jdbcType=VARCHAR},
			</if>
			<if test="deptPermissions != null and deptPermissions != ''">
				dept_permissions = #{deptPermissions, jdbcType=VARCHAR}
			</if>
		</set>
		where dept_id = #{deptId,jdbcType=BIGINT}
	</update>

	<select id="getParentDept" resultType="map">
		select DISTINCT
		dept_id  deptId,
		`name`
		from sys_dept
		where dept_id IN
		(
		select r.up_id from sys_dept d
		left join wm_relation r on d.dept_id=r.down_id
		where d.dept_id=#{query.deptId}
		union
		select r2.up_id from sys_dept d
		left join wm_relation r on d.dept_id=r.down_id
		left join wm_relation r2 on r.up_id=r2.down_id
		where d.dept_id=#{query.deptId}
				)
		and del_flag=0
		AND dept_id NOT in(1)
	</select>

	<select id="getChildDept" resultType="map">
		select DISTINCT
		dept_id  deptId,
		`name`
		from sys_dept
		where dept_id IN
		(
		select r.down_id from sys_dept d
		left join wm_relation r on d.dept_id=r.up_id
		where d.dept_id=#{query.deptId}
		union
		select r2.down_id from sys_dept d
		left join wm_relation r on d.dept_id=r.up_id
		left join wm_relation r2 on r.down_id=r2.up_id
		where d.dept_id=#{query.deptId}
				)
		and del_flag=0
		AND dept_id NOT in(1)
	</select>

	<select id="getDeptConvertToName" resultType="map">
		select  dept_id  deptId, `name` from sys_dept
	</select>

	<!-- 查询所有dept -->
	<select id="listAllDept" resultMap="sysDeptMap">
		select * from sys_dept
	</select>
</mapper>
