<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cftech.base.org.dao.OrgUnitMapper">

	<resultMap id="resultMap" type="com.cftech.base.org.model.OrgUnit">
		<id column="id" property="id"/>
		<result column="org_name" property="orgName"/>
		<result column="number" property="number"/>
		<result column="accounts_id" property="accountsId"/>
		<result column="wxorg_id" property="wxorgId"/>
		<result column="parent_id" property="parentId"/>
		<result column="parent_ids" property="parentIds"/>
		<result column="isadminunit" property="isAdminUnit"/>
		<result column="issalesunit" property="isSalesUnit"/>
		<result column="create_by" property="createBy"/>
		<result column="create_time" property="createTime"/>
		<result column="update_by" property="updateBy"/>
		<result column="update_time" property="updateTime"/>
		<result column="del_flag" property="delFlag"/>
		<result column="status" property="status"/>
		<result column="orgtype_id" property="orgtypeId"/>
		<result column="parentName" property="parentName"/>
		<result column="parentCode" property="parentCode"/>
	</resultMap>

	<resultMap id="resultMapVO" type="com.cftech.base.org.model.vo.OrgUnitVO">
		<id column="id" property="id"/>
		<result column="org_name" property="orgName"/>
		<result column="number" property="number"/>
		<result column="accounts_id" property="accountsId"/>
        <result column="parent_name" property="parentName"/>
		<result column="type_Name" property="typeName"/>
		<result column="isadminunit" property="isAdminUnit"/>
		<result column="issalesunit" property="isSalesUnit"/>
	</resultMap>

	<sql id="sqlWhere">
		<if test="conds!=null">
			<trim prefix="WHERE" prefixOverrides="AND|OR">
				<foreach collection="conds.conds" index="index" item="cond">
					${cond.linkType}
					<if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
					<if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
					<if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
					<if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
					<if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
					<if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
					<if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND #{cond.endValue}</if>
					<if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
					<if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
					<if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
					<if test="cond.condType == 'IN'">${cond.param} IN
						<foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
							#{item}
						</foreach>
					</if>
				</foreach>
			</trim>
		</if>
	</sql>

	<sql id="sqlColumns">
		id, `number`, create_by, create_time, update_by, update_time, del_flag, org_name, accounts_id,status,parent_id,
		parent_ids,isadminunit,issalesunit,wxorg_id,orgtype_id
	</sql>

	<insert id="save" parameterType="com.cftech.base.org.model.OrgUnit" useGeneratedKeys="true" keyProperty="id">
		insert into t_orgunit
		(
		id, `number`, create_by, create_time, update_by, update_time, del_flag, org_name, accounts_id,status,parent_id,
		parent_ids,isadminunit,issalesunit,wxorg_id, orgtype_id
		)
		values
		(
		#{id, jdbcType=BIGINT}, #{number, jdbcType=VARCHAR} ,#{createBy, jdbcType=BIGINT}, now(),  #{updateBy, jdbcType=BIGINT},
		now(),  #{delFlag, jdbcType=TINYINT},  #{orgName, jdbcType=VARCHAR},
		#{accountsId, jdbcType=BIGINT},#{status, jdbcType=VARCHAR},#{parentId, jdbcType=BIGINT},
		#{parentIds, jdbcType=VARCHAR}, #{isAdminUnit, jdbcType=TINYINT},#{isSalesUnit, jdbcType=TINYINT},#{wxorgId, jdbcType=VARCHAR},#{orgtypeId, jdbcType=BIGINT}
		)
	</insert>

	<select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> FROM t_orgunit t
		WHERE t.id=#{id}
	</select>

	<select id="findAllByAccountId" parameterType="java.lang.Long" resultMap="resultMap">
		SELECT <include refid="sqlColumns" /> FROM t_orgunit t
		WHERE t.del_flag = 0 and accounts_id = #{accountsId, jdbcType=BIGINT}
	</select>

	
	<select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
		SELECT COUNT(1) FROM t_orgunit
		<include refid="sqlWhere"/>
		and instr(parent_ids,'${orgId},')>0
	</select>

	<select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
		SELECT
			t.id, t.`number`, t.create_by, t.create_time, t.update_by, t.update_time, t.del_flag, t.org_name, t.accounts_id, t.status, t.parent_id,
			t.parent_ids, t.isadminunit, t.issalesunit, t.wxorg_id, t.orgtype_id, parent.number parentCode, parent.org_name parentName
		FROM t_orgunit t
		LEFT JOIN t_orgunit parent ON parent.id = t.parent_id
		<include refid="sqlWhere"/>
-- 		and instr(t.parent_ids,'${orgId},')>0
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>

	<update id="update" parameterType="com.cftech.base.org.model.OrgUnit">
		update t_orgunit
		<set>
			<if test="id != null">
				id = #{id, jdbcType=BIGINT},
			</if>
			<if test="number != null">
				`number` = #{number, jdbcType=VARCHAR},
			</if>
			<if test="createBy != null">
				create_by = #{createBy, jdbcType=BIGINT},
			</if>
			<if test="createTime != null">
				create_time = #{createTime, jdbcType=TIMESTAMP},
			</if>
			<if test="updateBy != null">
				update_by = #{updateBy, jdbcType=BIGINT},
			</if>
			<if test="updateTime != null">
				update_time=now(),
			</if>
			<if test="delFlag != null">
				del_flag = #{delFlag, jdbcType=TINYINT},
			</if>
			<if test="orgName != null">
				org_name = #{orgName, jdbcType=VARCHAR},
			</if>
			<if test="accountsId != null">
				accounts_id = #{accountsId, jdbcType=BIGINT},
			</if>
			<if test="status != null">
                `status` = #{status, jdbcType=VARCHAR},
			</if>
			<if test="parentId != null">
				parent_id = #{parentId, jdbcType=BIGINT},
			</if>
			<if test="parentIds != null">
				parent_ids = #{parentIds, jdbcType=VARCHAR},
			</if>
			<if test="isAdminUnit != null">
				isadminunit = #{isAdminUnit, jdbcType=TINYINT},
			</if>
			<if test="isSalesUnit != null">
				issalesunit = #{isSalesUnit, jdbcType=TINYINT},
			</if>
			<if test="wxorgId != null">
				wxorg_id = #{wxorgId, jdbcType=VARCHAR},
			</if>
			<if test="orgtypeId != null">
				orgtype_id = #{orgtypeId, jdbcType=BIGINT},
			</if>
		</set>
		where id=#{id,jdbcType=BIGINT}
	</update>

	<update id="delete" parameterType="java.lang.Long">
		update t_orgunit set del_flag=1 where id=#{id,jdbcType=BIGINT}
	</update>

    <update id="changeStatus" parameterType="java.util.Map">
        update t_orgunit set status=#{status, jdbcType=VARCHAR}
        <include refid="sqlWhere"/>
    </update>

    <update id="move" parameterType="java.util.Map">
		update t_orgunit set parent_id = #{parentId,jdbcType=BIGINT}, parent_ids = #{newParentIds,jdbcType=VARCHAR} where id = #{id,jdbcType=BIGINT};
		update t_orgunit set parent_ids = replace(parent_ids,#{oldParentIds,jdbcType=VARCHAR},#{newParentIds,jdbcType=VARCHAR}) where parent_ids like CONCAT('',#{oldParentIds,jdbcType=VARCHAR},'%');
	</update>

	<select id="fetchSearchVOByPage" parameterType="java.util.Map" resultMap="resultMapVO">
		SELECT t.id id, t.org_name org_name, t.number number, t.accounts_id accounts_id,
		t.isadminunit isadminunit, t.issalesunit issalesunit,
        IFNULL(p.org_name,'无') parent_name, tp.name type_Name
		FROM t_orgunit t
		left join t_orgunit p on p.id = t.parent_id
		left join t_orgtype tp on tp.id = t.orgtype_id
		<include refid="sqlWhere"/>
		and instr(t.parent_ids,'${orgId},')>0
		<if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
		<if test="limit>0">limit #{offset},#{limit}</if>
	</select>
</mapper>