<?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="org.springblade.system.mapper.TransferProblemMapper">
	<insert id="add">
		INSERT INTO da_ky_transfer_issue
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="proId != null and proId != ''">pro_id,</if>
			<if test="proName != null and proName != ''">pro_name,</if>
			<if test="singleProName != null and singleProName != ''">single_pro_name,</if>
			<if test="transferTime != null">transfer_time,</if>
			<if test="deptId != null and deptId != ''">dept_id,</if>
			<if test="deptName != null and deptName != ''">dept_name,</if>
			<if test="transferIssue != null and transferIssue != ''">transfer_issue,</if>
			<if test="createUserId != null">create_user_id,</if>
			<if test="createUserName != null and createUserName != ''">create_user_name,</if>
			<if test="updateUserId != null">update_user_id,</if>
			<if test="updateUserName != null and updateUserName != ''">update_user_name,</if>
		</trim>
		<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
			<if test="proId != null and proId != ''">#{proId},</if>
			<if test="proName != null and proName != ''">#{proName},</if>
			<if test="singleProName != null and singleProName != ''">#{singleProName},</if>
			<if test="transferTime != null">#{transferTime},</if>
			<if test="deptId != null and deptId != ''">#{deptId},</if>
			<if test="deptName != null and deptName != ''">#{deptName},</if>
			<if test="transferIssue != null and transferIssue != ''">#{transferIssue},</if>
			<if test="createUserId != null">#{createUserId},</if>
			<if test="createUserName != null and createUserName != ''">#{createUserName},</if>
			<if test="updateUserId != null">#{updateUserId},</if>
			<if test="updateUserName != null and updateUserName != ''">#{updateUserName},</if>
		</trim>
	</insert>
	<update id="edit">
		UPDATE da_ky_transfer_issue
		<trim prefix="SET" suffixOverrides=",">
			<if test="proId != null and proId != ''">pro_id = #{proId},</if>
			<if test="proName != null and proName != ''">pro_name = #{proName},</if>
			<if test="singleProName != null and singleProName != ''">single_pro_name = #{singleProName},</if>
			<if test="transferTime != null">transfer_time = #{transferTime},</if>
			<if test="deptId != null and deptId != ''">dept_id = #{deptId},</if>
			<if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
			<if test="transferIssue != null and transferIssue != ''">transfer_issue = #{transferIssue},</if>
			<if test="updateUserId != null">update_user_id = #{updateUserId},</if>
			<if test="updateUserName != null and updateUserName != ''">update_user_name = #{updateUserName},</if>
		</trim>
		WHERE id = #{id}
	</update>
	<delete id="del">
		DELETE FROM da_ky_transfer_issue
		WHERE id = #{id}
	</delete>
	<select id="list" resultType="org.springblade.system.domain.TransferProblemDto">
		SELECT
		dkti.id,
		dkti.pro_id AS proId,
		dkti.pro_name AS proName,
		dkti.single_pro_name AS singleProName,
		DATE_FORMAT(dkti.transfer_time, '%Y-%m-%d') AS transferTime,
		dkti.dept_id AS deptId,
        (SELECT
        IF(
        d.ancestors IS NULL
        OR d.ancestors = ''
        OR d.ancestors = '0',
        d.dept_name,
        CONCAT((
        SELECT
        GROUP_CONCAT( dt.dept_name SEPARATOR ' / ' )
        FROM
        blade_dept dt
        WHERE
        FIND_IN_SET( dt.id, d.ancestors )),
        ' / ',
        d.dept_name
        ))) AS deptName,
		dkti.transfer_issue AS transferIssue,
		dkti.create_time AS createTime,
		dkti.update_time AS updateTime,
		dkti.create_user_id AS createUserId,
		dkti.create_user_name AS createUserName,
		dkti.update_user_id AS updateUserId,
		dkti.update_user_name AS updateUserName,
		dkti.del_flag AS delFlag
		FROM
			da_ky_transfer_issue dkti
		left join record_da_ky_project dkp ON dkti.pro_id = dkp.id
		LEFT JOIN blade_dept d ON dkti.dept_id = d.id AND d.is_deleted = '0'
		WHERE
		dkti.del_flag = '1'
		<if test="singleProName != null and singleProName != ''">
			AND dkti.single_pro_name like concat('%', #{singleProName}, '%')
		</if>
		<if test="proType != null and proType != ''">
			and dkp.pro_type = #{proType}
		</if>
		<if test="voltageLevel != null and voltageLevel != ''">
			and dkp.voltage_level = #{voltageLevel}
		</if>
		ORDER BY
		  dkti.create_time DESC
	</select>
	<select id="getTransferProblemById" resultType="org.springblade.system.domain.TransferProblemDto">
		SELECT
			dkti.id,
			dkti.pro_id AS proId,
			dkti.pro_name AS proName,
			dkti.single_pro_name AS singleProName,
			dkti.transfer_time AS transferTime,
			dkti.dept_id AS deptId,
			dkti.dept_name AS deptName,
			dkti.transfer_issue AS transferIssue,
			dkti.create_time AS createTime,
			dkti.update_time AS updateTime,
			dkti.create_user_id AS createUserId,
			dkti.create_user_name AS createUserName,
			dkti.update_user_id AS updateUserId,
			dkti.update_user_name AS updateUserName,
			dkti.del_flag AS delFlag
		FROM
		da_ky_transfer_issue dkti
		WHERE
			dkti.id = #{id}
	</select>
</mapper>
