<?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.admin.mapper.WmDaDrugEntMapper">

	<resultMap id="wmDaDrugEntMap" type="com.qunzhixinxi.hnqz.admin.entity.WmDaDrugEnt">
		<id property="id" column="id"/>
		<result property="entname" column="entname"/>
		<result property="entcode" column="entcode"/>
		<result property="temp1" column="temp1"/>
		<result property="temp2" column="temp2"/>
		<result property="temp3" column="temp3"/>
		<result property="temp4" column="temp4"/>
		<result property="deptId" column="dept_id"/>
		<result property="delFlag" column="del_flag"/>
		<result property="enableFlag" column="enable_flag"/>
		<result property="tenantId" column="tenant_id"/>
		<result property="createTime" column="create_time"/>
		<result property="createUser" column="create_user"/>
		<result property="updateTime" column="update_time"/>
		<result property="updateUser" column="update_user"/>
		<result property="pgqx" column="pgqx"/>
		<result property="management" column="management"/>
	</resultMap>

	<!-- 分页查询 -->
	<select id="selectPage" resultMap="wmDaDrugEntMap">
		SELECT
			e.id,
			e.entname,
			e.entcode,
			e.temp1,
			e.temp2,
			e.temp3,
			e.temp4,
			e.dept_id,
			e.del_flag,
			e.enable_flag,
			e.tenant_id,
			e.create_time,
			e.create_user,
			e.update_time,
			e.update_user,
			e.pgqx,
			e.management,
			e.subject_type,
			d.dept_permissions
		FROM wm_da_drug_ent e LEFT JOIN sys_dept d ON e.dept_id=d.dept_id
		<where>
			<if test="query.entname != null and query.entname != ''">
				AND e.entname LIKE CONCAT('%', #{query.entname, jdbcType=VARCHAR}, '%')
			</if>
			<if test="query.entcode != null and query.entcode != ''">
				AND e.entcode = #{query.entcode, jdbcType=VARCHAR}
			</if>
		</where>
		ORDER BY e.create_time DESC
	</select>

	<select id="selectByDrugId" resultType="java.lang.Integer" parameterType="java.util.HashMap">
		select o.entname,d.gsmc
		from wm_relation r
		left join  wm_da_drug_ent o on r.up_id=o.id
		left join  wm_da_agent d on r.up_id=d.id
		where r.up_id = #{upId,jdbcType=INTEGER}

	</select>

	<select id="selectEntNameByDrugId" resultMap="wmDaDrugEntMap" parameterType="java.util.HashMap">
   select
   id,dept_id,entname
   from wm_da_drug_ent
   where
   id = #{id,jdbcType=VARCHAR}
</select>

	<select id="selectEntNameByDrugIds" resultMap="wmDaDrugEntMap" parameterType="java.util.HashMap">

			select d.id,d.entname,d.dept_id
			from wm_da_drug_ent  d
			left join  wm_relation r on r.up_id=d.id
			left join  wm_da_agent o on r.down_id=o.id
			where r.down_id = #{downId,jdbcType=INTEGER}
			UNION
			select id,gsmc,dept_id from wm_da_agent where id= #{Id,jdbcType=INTEGER}
	</select>
	<select id="selectByDeptId" resultMap="wmDaDrugEntMap" parameterType="java.util.HashMap">
   select
   *
   from wm_da_drug_ent
   where
   dept_id = #{deptId,jdbcType=VARCHAR}
</select>

	<select id="selectByUserDeptId" resultType="java.util.HashMap" parameterType="java.lang.Integer">
   select
   id,entname as gsmc,entcode as zjNumber,management
   from wm_da_drug_ent
   where
   dept_id = #{deptId,jdbcType=VARCHAR}
</select>
	<select id="selectDeptId" resultMap="wmDaDrugEntMap">
		select
		*
		from wm_da_drug_ent
		where
		dept_id = #{deptId,jdbcType=INTEGER}
	</select>
	<select id="selectDeptId1" resultType="java.util.HashMap">
		select
		cast(id as char) as id,entname as name
		from wm_da_drug_ent
		where
		dept_id = #{deptId,jdbcType=VARCHAR}
	</select>
	<select id="selectId" resultMap="wmDaDrugEntMap">
		select
		*
		from wm_da_drug_ent
		where
		id = #{id,jdbcType=BIGINT}
	</select>

	<select id="selectEntNameByDeptId" resultMap="wmDaDrugEntMap" parameterType="java.util.HashMap">
   select
   id,dept_id,entname
   from wm_da_drug_ent
   where
   dept_id = #{deptId,jdbcType=INTEGER}
</select>

	<select id="selectEntNameByByDeptIds" resultMap="wmDaDrugEntMap" parameterType="java.util.HashMap">

			select d.id,d.entname,d.dept_id
			from wm_da_drug_ent  d
			left join  wm_relation r on r.up_id=d.dept_id
			left join  wm_da_agent o on r.down_id=o.dept_id
			where r.down_id = #{downId,jdbcType=INTEGER}
			UNION
			select id,gsmc,dept_id from wm_da_agent where dept_id= #{deptId,jdbcType=VARCHAR}
	</select>

	<select id="getListByCsoDeptId" resultMap="wmDaDrugEntMap" >

			SELECT DISTINCT
				*
			FROM
				wm_da_drug_ent
			WHERE
				dept_id IN (
					SELECT
						up_id
					FROM
						wm_relation
					WHERE
						down_id = #{deptId,jdbcType=INTEGER}
					UNION
						SELECT
							up_id
						FROM
							wm_relation
						WHERE
							down_id IN (
								SELECT
									up_id
								FROM
									wm_relation
								WHERE
									down_id = #{deptId,jdbcType=INTEGER}
							)
				)
	</select>


</mapper>
