<?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.WmDaAgentMapper">
	<resultMap id="wmDaAgentMap" type="com.qunzhixinxi.hnqz.admin.entity.WmDaAgent">
		<id property="id" column="id"/>
		<result property="gsmc" column="gsmc"/>
		<result property="yyzz" column="yyzz"/>
		<result property="sbjnzm" column="sbjnzm"/>
		<result property="frsfzzmZm" column="frsfzzm_zm"/>
		<result property="frsfzzmBm" column="frsfzzm_bm"/>
		<result property="frsfzzmSc" column="frsfzzm_sc"/>
		<result property="zjType" column="zj_type"/>
		<result property="zjNumber" column="zj_number"/>
		<result property="jsAccount" column="js_account"/>
		<result property="isExamine" column="is_examine"/>
		<result property="extDrugEnt" column="ext_drug_ent"/>
		<result property="temp1" column="temp1"/>
		<result property="temp2" column="temp2"/>
		<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="drugEntId" column="drug_ent_id"/>
		<result property="pgqx" column="pgqx"/>
		<result property="qylx" column="qylx"/>
		<result property="management" column="management"/>

	</resultMap>
	<insert id="insert1" parameterType="com.qunzhixinxi.hnqz.admin.entity.WmDaAgent" >
    insert into wm_da_agent (gsmc, yyzz,
      sbjnzm, frsfzzm_zm, frsfzzm_bm,
      frsfzzm_sc, zj_type, zj_number,
      js_account, is_examine, ext_drug_ent,
      temp1, temp2, dept_id,
      del_flag, enable_flag, tenant_id,
      create_time, create_user, update_time,
      update_user, drug_ent_id, pgqx, qylx
      )
    values (#{gsmc,jdbcType=VARCHAR}, #{yyzz,jdbcType=VARCHAR},
      #{sbjnzm,jdbcType=VARCHAR}, #{frsfzzmZm,jdbcType=VARCHAR}, #{frsfzzmBm,jdbcType=VARCHAR},
      #{frsfzzmSc,jdbcType=VARCHAR}, #{zjType,jdbcType=VARCHAR}, #{zjNumber,jdbcType=VARCHAR},
      #{jsAccount,jdbcType=VARCHAR}, #{isExamine,jdbcType=VARCHAR}, #{extDrugEnt,jdbcType=VARCHAR},
      #{temp1,jdbcType=VARCHAR}, #{temp2,jdbcType=VARCHAR}, #{deptId,jdbcType=VARCHAR},
      #{delFlag,jdbcType=VARCHAR}, #{enableFlag,jdbcType=VARCHAR}, #{tenantId,jdbcType=BIGINT},#{createTime,jdbcType=VARCHAR},
      #{createUser,jdbcType=BIGINT}, #{updateTime,jdbcType=VARCHAR}, #{updateUser,jdbcType=VARCHAR},
      #{drugEntId,jdbcType=VARCHAR}, #{pgqx,jdbcType=VARCHAR}, #{qylx,jdbcType=VARCHAR}
      )
  </insert>

	<!-- 分页查询派工企业 -->
	<select id="selectWmManagementPage" resultMap="wmDaAgentMap">
		SELECT
		a.*, d.dept_permissions, d.level as dept_level
		FROM wm_da_agent a
		LEFT JOIN sys_dept d ON d.dept_id = a.dept_id
		<where>
			AND d.dept_id IS NOT NULL
			AND (d.level = 3 OR d.level = 4 OR d.level = 5)
			<if test="query.gsmc != null and query.gsmc !=''">
				AND gsmc LIKE CONCAT('%',#{query.gsmc},'%')
			</if>
			<if test="query.zjNumber != null and query.zjNumber !=''">
				AND zj_number LIKE CONCAT('%',#{query.zjNumber},'%')
			</if>
			<if test="query.pgqx != null and query.pgqx!=''">
				AND pgqx = #{query.pgqx,jdbcType=VARCHAR}
			</if>
			<if test="query.updateTime != null and query.updateTime !=''">
				AND update_time = #{query.updateTime,jdbcType=VARCHAR}
			</if>
			<if test="query.createTime != null and query.createTime!='' ">
				AND create_time = #{query.createTime,jdbcType=VARCHAR}
			</if>
			<if test="query.yyzz != null and query.yyzz!='' ">
				AND yyzz = #{query.yyzz,jdbcType=VARCHAR}
			</if>
			<if test="query.sbjnzm != null and query.sbjnzm!='' ">
				AND sbjnzm = #{query.sbjnzm,jdbcType=VARCHAR}
			</if>
			<if test="query.frsfzzmZm != null and query.frsfzzmZm!='' ">
				AND frsfzzm_zm = #{query.frsfzzmZm,jdbcType=VARCHAR}
			</if>
			<if test="query.frsfzzmBm != null and query.frsfzzmBm!='' ">
				AND frsfzzm_bm = #{query.frsfzzmBm,jdbcType=VARCHAR}
			</if>
			<if test="query.frsfzzmSc != null and query.frsfzzmSc!=''">
				AND frsfzzm_sc = #{query.frsfzzmSc,jdbcType=VARCHAR}
			</if>
			<if test="query.extDrugEnt != null and query.extDrugEnt!=''">
				AND ext_drug_ent = #{query.extDrugEnt,jdbcType=VARCHAR}
			</if>
			<if test="query.qylx != null and query.qylx!=''">
				AND qylx = #{query.qylx,jdbcType=VARCHAR}
			</if>
		</where>
		ORDER BY id DESC
	</select>

	<select id="selectWmManagementList" resultMap="wmDaAgentMap">
		select
		*
		from wm_da_agent where 1=1
		<if test="query.gsmc != null and query.gsmc !=''">
			AND gsmc like CONCAT('%',#{query.gsmc},'%')
		</if>
		<if test="query.zjNumber != null and query.zjNumber !=''">
			AND zj_number like CONCAT('%',#{query.zjNumber},'%')
		</if>
		<if test="query.pgqx != null and query.pgqx!=''">
			AND pgqx = #{query.pgqx,jdbcType=VARCHAR}
		</if>
		<if test="query.updateTime != null and query.updateTime !=''">
			AND update_time = #{query.updateTime,jdbcType=VARCHAR}
		</if>
		<if test="query.createTime != null and query.createTime!='' ">
			AND create_time = #{query.createTime,jdbcType=VARCHAR}
		</if>
		<if test="query.yyzz != null and query.yyzz!='' ">
			AND yyzz = #{query.yyzz,jdbcType=VARCHAR}
		</if>
		<if test="query.sbjnzm != null and query.sbjnzm!='' ">
			AND sbjnzm = #{query.sbjnzm,jdbcType=VARCHAR}
		</if>
		<if test="query.frsfzzmZm != null and query.frsfzzmZm!='' ">
			AND frsfzzm_zm = #{query.frsfzzmZm,jdbcType=VARCHAR}
		</if>
		<if test="query.frsfzzmBm != null and query.frsfzzmBm!='' ">
			AND frsfzzm_bm = #{query.frsfzzmBm,jdbcType=VARCHAR}
		</if>
		<if test="query.frsfzzmSc != null and query.frsfzzmSc!=''">
			AND frsfzzm_sc = #{query.frsfzzmSc,jdbcType=VARCHAR}
		</if>
		<if test="query.extDrugEnt != null and query.extDrugEnt!=''">
			AND ext_drug_ent = #{query.extDrugEnt,jdbcType=VARCHAR}
		</if>
		<if test="query.qylx != null and query.qylx!=''">
			AND qylx = #{query.qylx,jdbcType=VARCHAR}
		</if>
		order by id desc
	</select>

	<select id="selectExtDrugEntList" resultMap="wmDaAgentMap">
		select
		ext_drug_ent
		from wm_da_agent
		where
		gsmc = #{query.gsmc,jdbcType=VARCHAR}
	</select>

	<select id="selectByGsmcList" resultMap="wmDaAgentMap">
		select
		*
		from wm_da_agent
		where
		gsmc = #{query.gsmc,jdbcType=VARCHAR}
	</select>

	<select id="selectByGsmc" resultMap="wmDaAgentMap">
		select
		*
		from wm_da_agent
		where
		gsmc = #{query.gsmc,jdbcType=VARCHAR}
	</select>

	<select id="selectDeptId" resultMap="wmDaAgentMap">
		select
			a.*, d.dept_permissions
		from wm_da_agent a
		left join sys_dept d
		on a.dept_id = d.dept_id
		where
			a.dept_id = #{deptId,jdbcType=VARCHAR}
	</select>

	<select id="selectDeptId1" resultType="java.util.HashMap">
		select
		cast(id as char) as id,gsmc as name
		from wm_da_agent
		where
		dept_id = #{deptId,jdbcType=VARCHAR}
	</select>

	<select id="selectExtDrugEnt" resultMap="wmDaAgentMap">
		select
		*
		from wm_da_agent
		where
		ext_drug_ent = #{extDrugEnt,jdbcType=VARCHAR}
	</select>

	<select id="selectByDeptId" resultMap="wmDaAgentMap" parameterType="java.lang.String">
		select
		*
		from wm_da_agent
		where dept_id = #{deptId,jdbcType=VARCHAR}
	</select>


	<select id="selectByDeptIds" resultMap="wmDaAgentMap" parameterType="java.lang.Integer">
		select
		*
		from wm_da_agent
		where dept_id = #{deptId,jdbcType=VARCHAR}
	</select>


	<select id="selectByDrugIds" resultMap="wmDaAgentMap" parameterType="java.util.HashMap">
		select d.*
		from wm_da_agent d
		left join  wm_relation r on r.down_id=d.id
		where r.up_id = #{upId,jdbcType=INTEGER}
		UNION
		select * from wm_da_agent where id= #{Id,jdbcType=INTEGER}

	</select>

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

	</select>

	<select id="selectByRelationId" resultMap="wmDaAgentMap" parameterType="java.lang.Integer">
		select *
		from
		 wm_da_agent
		 where id= #{Id,jdbcType=INTEGER}
	</select>

	<select id="selectByRelationIds" resultMap="wmDaAgentMap" parameterType="java.util.HashMap">
		select d.*
		from wm_da_agent d
		left join  wm_relation r on r.down_id=d.dept_id
		where r.up_id = #{upId,jdbcType=INTEGER}

	</select>

	<select id="selectEntNameByDrugIds" resultMap="wmDaAgentMap" parameterType="java.util.HashMap">
		select d.id,d.entname
		from wm_da_drug_ent  d
		left join  wm_relation r on r.down_id=d.id
		left join  wm_da_drug_ent o on r.up_id=o.id
		where r.up_id = #{upId,jdbcType=INTEGER}
		UNION
		select id,gsmc from wm_da_agent where id= #{Id,jdbcType=INTEGER}

	</select>

	<select id="selectByUserDeptId" resultMap="wmDaAgentMap" parameterType="java.lang.Integer">
		select
		*
		from wm_da_agent
		where dept_id = #{deptId,jdbcType=VARCHAR}
	</select>

	<select id="selectWmDaDrugEntByDeptIds" resultMap="wmDaAgentMap" parameterType="java.util.HashMap">
		select d.*
		from wm_da_agent d
		left join  wm_relation r on r.down_id=d.dept_id
		where r.up_id = #{upId,jdbcType=INTEGER}
		UNION
		select * from wm_da_agent where dept_id= #{deptId,jdbcType=VARCHAR}

	</select>

	<select id="selectWmDaDrugEntByDeptId" resultMap="wmDaAgentMap" parameterType="java.util.HashMap">
		select d.id,d.gsmc
		from wm_da_agent d
		left join  wm_relation r on r.down_id=d.dept_id
		left join  wm_da_drug_ent o on r.up_id=o.dept_id
		where r.up_id = #{upId,jdbcType=INTEGER}
		UNION
		select id,entname from wm_da_drug_ent where dept_id= #{deptId,jdbcType=INTEGER}

	</select>

	<select id="selectWmDaAgentByRelationId" resultMap="wmDaAgentMap" parameterType="java.lang.Integer">
		select *
		from
		 wm_da_agent
		 where dept_id= #{deptId,jdbcType=INTEGER}
	</select>

	<select id="selectWmDaAgentByRelationIds" resultMap="wmDaAgentMap" parameterType="java.util.HashMap">
		select d.*
		from wm_da_agent d
		left join  wm_relation r on r.down_id=d.dept_id
		where r.up_id = #{upId,jdbcType=INTEGER}

	</select>

	<select id="selectWmDaAgentByDeptId" resultMap="wmDaAgentMap" parameterType="java.util.HashMap">
		select d.id,d.gsmc,d.enable_flag
		from wm_da_agent d
		left join  wm_relation r on r.down_id=d.dept_id
		left join  wm_da_drug_ent o on r.up_id=o.dept_id
		where r.up_id = #{upId,jdbcType=INTEGER}
		UNION
		select id,entname,enable_flag from wm_da_drug_ent where dept_id= #{deptId,jdbcType=INTEGER}

	</select>

	<select id="selectWmDaAgentByDeptIds" resultMap="wmDaAgentMap" parameterType="java.lang.String">
		select *
		from
		 wm_da_agent
		 where dept_id= #{deptId,jdbcType=VARCHAR}
	</select>

	<!-- 根据sys_dept表的level字段查询 -->
	<select id="listAgentByDeptLevel" resultMap="wmDaAgentMap">
		SELECT a.*
		FROM wm_da_agent a
			LEFT JOIN sys_dept d ON d.dept_id = a.dept_id
		WHERE a.del_flag = 0
		  AND a.enable_flag = 0
		  AND d.level = #{deptLevel}
	</select>

	<!-- 根据deptRelation查询 -->
	<select id="listAgentByDeptRelation" resultMap="wmDaAgentMap">
		SELECT
			*
		FROM
			wm_da_agent a
				LEFT JOIN sys_dept_relation r ON r.descendant = a.dept_id
		WHERE
			r.ancestor = #{deptId, jdbcType=INTEGER}
	</select>

</mapper>
