<?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.nbclass.szxgl.mapper.ProjectMapper">
  <resultMap id="BaseResultMap" type="com.nbclass.szxgl.model.Project">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="CHAR" property="id" />
    <result column="_name" jdbcType="VARCHAR" property="name" />
    <result column="_type" jdbcType="CHAR" property="type" />
    <result column="_importance" jdbcType="CHAR" property="importance" />
    <result column="_area" jdbcType="CHAR" property="area" />
    <result column="_status" jdbcType="TINYINT" property="status" />
    <result column="_start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="_end_time" jdbcType="TIMESTAMP" property="endTime" />
      <result column="_plan_time" jdbcType="TIMESTAMP" property="planTime" />
    <result column="_create_user_id" jdbcType="CHAR" property="createUserId" />
    <result column="_create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="_update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="_notify" jdbcType="SMALLINT" property="notify" />
    <result column="_customer" jdbcType="VARCHAR" property="customer" />
    <result column="_contractno" jdbcType="VARCHAR" property="contractno" />
    <result column="_content" jdbcType="LONGVARCHAR" property="content" />
    <result column="_project_client_id" jdbcType="CHAR" property="projectClientId" />
    <result column="_project_clien_array" jdbcType="VARCHAR" property="projectClienArray" />
  </resultMap>
  	
	<select id="getList" parameterType="com.nbclass.szxgl.model.Project" resultType="com.nbclass.szxgl.model.Project">
		SELECT  p.id, p._name, p._type, p._importance, p._area, p._status, p._start_time, p._end_time, p._plan_time,
		p._create_user_id, p._create_time, p._update_time, p._notify, p._customer, p._contractno, p._project_client_id,
		p._project_clien_array,
		lv1.list_value AS typeName, lv2.list_value AS importanceName,
		sy_users.true_name as createUserName,
		(SELECT GROUP_CONCAT(_table_id) FROM project_user WHERE _type = 1 AND _project_id=p.id) AS fzrIds,
		(SELECT GROUP_CONCAT(true_name) FROM sy_users WHERE user_name IN (SELECT _table_id FROM project_user WHERE _type = 1 AND _project_id=p.id)) AS fzrNames 
		<!-- (SELECT COUNT(*) FROM project_task WHERE _project_id = p.id AND _create_user_id = 'pangtao') AS taskNum  -->
		FROM project p 
		LEFT JOIN project_user pu ON p.id = pu._project_id
		LEFT JOIN project_task_user ptu ON p.id = ptu._project_id 
		LEFT JOIN list_values lv1 ON p._type = lv1.id 
		LEFT JOIN list_values lv2 ON p._importance = lv2.id 
		LEFT JOIN sy_users ON p._create_user_id = sy_users.user_name 
		WHERE 1=1 
		<if test="id != null and id != ''">
			AND p.id = #{id} 
		</if>
		<if test="name != null and name != ''">
			AND p._name like concat('%', #{name}, '%')
		</if>
		<if test="area != null and area != ''">
			AND (SELECT COUNT(*) FROM project_city WHERE _type = #{area} AND _project_id = p.id)>0
		</if>
		<if test="status != null and status >= 0">
			AND p._status = #{status} 
		</if>
		<if test="starttime != null and starttime != ''">
			<![CDATA[ AND  DATE_FORMAT(p._start_time,"%Y-%m-%d") >= #{starttime} ]]>
		</if>
		<if test="endtime != null and endtime != ''">
			<![CDATA[ AND  DATE_FORMAT(p._end_time,"%Y-%m-%d") <= #{endtime} ]]>
		</if>
        <if test="plantime != null and plantime != ''">
            <![CDATA[ AND  DATE_FORMAT(p._plan_time,"%Y-%m-%d") <= #{plantime} ]]>
        </if>
		<if test="createUserName != null and createUserName != ''">
			AND (p._create_user_id=#{createUserName} or pu._table_id=#{createUserName} or ptu._user_id=#{createUserName}) 
		</if>
		
		<if test="deptUserIds != null"><!-- 选择企业部门(部门里所有成员)或者选择多个用户的userid -->
			AND (
				p._create_user_id in
				<foreach item="item" collection="deptUserIds" open="(" separator="," close=")">
		    		#{item}
				</foreach>

				or pu._table_id in 
				<foreach item="item" collection="deptUserIds" open="(" separator="," close=")">
		    		#{item}
				</foreach>

				or ptu._user_id in 
				<foreach item="item" collection="deptUserIds" open="(" separator="," close=")">
		    		#{item}
				</foreach>
			)
		</if>
		
		<choose>
			<when test="isAdmin != null and isAdmin == 'Y'.toString()">
				<!-- admin权限不加登录用户账号作权限 -->
			</when>
			<otherwise>
				AND (p._create_user_id=#{loginName} or pu._table_id=#{loginName} or ptu._user_id=#{loginName}) 
			</otherwise>
		</choose>
		
		group by p.id 
		order by p._status asc, p._create_time desc
	</select>
	
	<select id="findById" parameterType="java.lang.String" resultType="com.nbclass.szxgl.model.Project">
		SELECT  p.*,
		(SELECT GROUP_CONCAT(lv.list_value)  FROM project_city pcity ,list_values lv WHERE pcity._project_id = p.id AND lv.id = pcity._type) as areaShow,
		(SELECT GROUP_CONCAT(_table_id) FROM project_user WHERE _type = 1 AND _project_id=p.id) AS fzrIds,
		(SELECT GROUP_CONCAT(_table_id) FROM project_user WHERE _type = 3 AND _project_id=p.id) AS csrIds
		FROM project p
		WHERE p.id = #{id}
	</select>
  	
  	<!-- 根据部门ID，递归查询所有子部门ID -->
  	<select id="queryDeptIdsByDeptId" resultType="java.lang.String">
  		SELECT queryDeptIdsByDeptId(#{deptId})
  	</select>
  	
  	<!-- 根据部门IDs查询所有用户ID -->
  	<select id="queryUserIdsByDeptIds" parameterType="java.util.List" resultType="java.lang.String">
  		SELECT GROUP_CONCAT(UserID) FROM qywx_user WHERE Department IN 
  		<foreach item="item" collection="list" open="(" separator="," close=")">
    		#{item}
		</foreach>
  	</select>
  	<!--  -->
  	<select id="findProductById" parameterType="java.lang.String" resultType="map">
		SELECT t.user_id,t.user_type,su.true_name,p.* FROM(
			SELECT
			pu._project_id,
			 pu._table_id AS user_id,pu._type as user_type
			FROM
				 project_user pu
			WHERE pu._type = '1'
			UNION
			SELECT
			pu._project_id,
			 pu._table_id AS user_id,pu._type as user_type
			FROM
				 project_user pu 
			WHERE pu._type = '3'
			) t LEFT JOIN project p ON  t._project_id = p.id
			LEFT JOIN sy_users su ON su.user_name = t.user_id
			WHERE
				p.id = #{id}
			ORDER BY t.user_type,t.user_id
	</select>
	
	<update id="updateProjectStatus">
		update project set
		<if test="contractno != null and contractno != ''">
			_contractno = #{contractno},
		</if>
        <if test="setuptime != null and setuptime != ''">
            _end_time = NULL,
        </if>
        <if test="updatetime != null and updatetime != ''">
            _end_time = NOW(),
        </if>
		_status = #{status} where id = #{id}
	</update>
  	 	
</mapper>