<?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.jeeplus.modules.workfullmanage.dao.WorkFullManageDao">
    
	<sql id="workFullManageColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.project_id AS "projectId.id",
		a.contract_id AS "contractId",
		a.psort AS "psort",
		a.ssort AS "ssort",
		a.full_project_number AS "fullProjectNumber",
		a.full_project_source AS "fullProjectSource",
		a.full_project_name AS "fullProjectName",
		a.full_industry_type AS "fullIndustryType",
		a.full_structure_type AS "fullStructureType",
		a.full_invest_money AS "fullInvestMoney",
		a.client_id AS "clientId",
		a.build_id AS "buildId",
		a.full_project_type AS "fullProjectType",
		a.full_project_address AS "area.id",
		a.full_project_property AS "fullProjectProperty",
		a.full_project_scale AS "fullProjectScale",
		a.full_count_unit AS "fullCountUnit",
		a.full_project_feature AS "fullProjectFeature",
		a.full_project_status AS "fullProjectStatus",
		a.host_company AS "hostCompany.id",
		a.follow_company AS "followCompany.id",
		a.talk_start_time AS "talkStartTime",
		a.talk_end_time AS "talkEndTime",
		a.ask_pay_mode AS "askPayMode",
		a.standard_explain AS "standardExplain",
		a.fixed_price AS "fixedPrice",
		a.client_require AS "clientRequire",
		a.host_master AS "hostMaster",
		a.follow_master AS "followMaster",
		a.host_member AS "hostMember",
		a.follow_member AS "followMember",
		a.constructId AS "constructId.id",
		a.designId AS "designId.id",
		a.surveyId AS "surveyId.id",
		(select name from sys_user where id = a.create_by) AS "createBy.name",
		ci.`name` AS "ciName",
		a.company_id AS "companyId",
		a.office_id AS "officeId",
		workFullRecord.id AS "workFullRecord.id",
		workFullRecord.back_record_status AS "workFullRecord.backRecordStatus",
		area.name AS "area.name",
		a.project_id AS "workProject.id",
		workProjectCompletion.id AS "workProjectCompletion.id",
		workProjectCompletion.project_stutas AS "workProjectCompletion.projectStatus",
		workProjectCompletion.process_instance_id AS "workProjectCompletion.processInstanceId",
		workProjectCompletion.create_By AS "workProjectCompletion.createBy.id",
		a.ext,
		a.contractName,
		a.is_total AS "isTotal",
		a.total_full_id AS "totalFullId",
		a.total_full_ids AS "totalFullIds",
		a.is_total_name AS "isTotalName",
		a.common_flag AS "commonFlag",
		a.sort AS "sort",
		(select full_project_name from work_full_manage where id = a.total_full_id) totalFullName,
		(select full_project_number from work_full_manage where id = a.total_full_id) totalFullNumber
	</sql>
	
	<sql id="workFullManageJoins">
		LEFT JOIN sys_area area ON area.id = a.full_project_address
		LEFT JOIN sys_office hostCompany ON hostCompany.id = a.host_company
		LEFT JOIN sys_office followCompany ON followCompany.id = a.follow_company
		LEFT JOIN work_full_design designId on designId.id = a.designId
		LEFT JOIN work_full_survey surveyId on surveyId.id = a.surveyId
		LEFT JOIN work_full_construct constructId on constructId.id = a.constructId
		LEFT JOIN work_client_info ci on ci.id = a.client_id
		LEFT JOIN work_client_info cs on cs.id = a.build_id
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN work_full_record workFullRecord ON workFullRecord.work_full_manage_id = a.id
		LEFT JOIN work_project workProject ON workProject.id = a.project_id
		LEFT JOIN (select * FROM work_project_completion where del_flag = 0) workProjectCompletion on workProjectCompletion.project_id = a.id
	</sql>

	<select id="get" resultType="WorkFullManage" >
		SELECT 
			<include refid="workFullManageColumns"/>
		FROM work_full_manage a
		<include refid="workFullManageJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="WorkFullManage" >
		SELECT 
			<include refid="workFullManageColumns"/>
		FROM work_full_manage a
		<include refid="workFullManageJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="fullProjectNumber != null and fullProjectNumber != ''">
				AND a.full_project_number = #{fullProjectNumber}
			</if>
			<if test="fullProjectName != null and fullProjectName != ''">
				AND a.full_project_name like
				<if test="dbName == 'oracle'">'%'||#{fullProjectName}||'%'</if>
				<if test="dbName == 'mysql'">CONCAT('%', #{fullProjectName}, '%')</if>
			</if>
			<if test="fullProjectStatus != null and fullProjectStatus != ''">
				AND a.full_project_status = #{fullProjectStatus}
			</if>
			<if test="area != null and area.id != null and area.id != ''">
				AND (a.full_project_address = #{area.id} OR find_in_set(#{area.id},a.area_parent_ids))
			</if>
			<if test="psort != null and psort != ''">
				AND a.psort = #{psort}
			</if>
			<if test="ssort != null and ssort != ''">
				AND a.ssort = #{ssort}
			</if>
			<if test="ciName != null and ciName != ''">
				AND  ci.`name` like concat('%',#{ciName},'%')
			</if>
			<if test="totalFullIds != null and totalFullIds != ''">
				AND a.total_full_ids LIKE
				<if test="dbName == 'oracle'">'%'||#{totalFullIds}||'%'</if>
				<if test="dbName == 'mssql'">'%'+#{totalFullIds}+'%'</if>
				<if test="dbName == 'mysql'">concat('%',#{totalFullIds},'%')</if>
			</if>
			<if test="totalFullId != null and totalFullId != ''">
				AND a.total_full_id =#{totalFullId}
			</if>
			<choose>
				<when test="queryCon != null and queryCon != ''">
					and (
						find_in_set(#{queryCon},a.host_master)
						or
						find_in_set(#{queryCon},a.follow_master)
						or
						find_in_set(#{queryCon},a.host_member)
						or
						find_in_set(#{queryCon},a.follow_member)
						or
						find_in_set(#{masterId},a.masterId)
						OR a.create_by = #{queryCon}
						${sqlMap.dsf}
					)
				</when>
				<otherwise>
					${sqlMap.dsf}
				</otherwise>
			</choose>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.sort DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkFullManage" >
		SELECT 
			<include refid="workFullManageColumns"/>
		FROM work_full_manage a
		<include refid="workFullManageJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO work_full_manage(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			contract_id,
			full_project_number,
			full_project_source,
			full_project_name,
			full_industry_type,
			full_structure_type,
			full_invest_money,
			client_id,
			build_id,
			full_project_type,
			full_project_address,
			area_parent_ids,
			full_project_property,
			full_project_scale,
			full_count_unit,
			full_project_feature,
			host_company,
			follow_company,
			talk_start_time,
			talk_end_time,
			ask_pay_mode,
			standard_explain,
			fixed_price,
			client_require,
			host_master,
			follow_master,
			host_member,
			follow_member,
			constructId,
			surveyId,
			designId,
			full_project_status,
			company_id,
			project_id,
			office_id,
			masterId,
			ext,
			contractName,
			is_total,
			psort,
			ssort,
			total_full_id,
			is_total_name,
			total_full_ids,
			common_flag,
			sort
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{contractId},
			#{fullProjectNumber},
			#{fullProjectSource},
			#{fullProjectName},
			#{fullIndustryType},
			#{fullStructureType},
			#{fullInvestMoney},
			#{clientId},
			#{buildId},
			#{fullProjectType},
			#{area.id},
			#{area.parentIds},
			#{fullProjectProperty},
			#{fullProjectScale},
			#{fullCountUnit},
			#{fullProjectFeature},
			#{hostCompany.id},
			#{followCompany.id},
			#{talkStartTime},
			#{talkEndTime},
			#{askPayMode},
			#{standardExplain},
			#{fixedPrice},
			#{clientRequire},
			#{hostMaster},
			#{followMaster},
			#{hostMember},
			#{followMember},
			#{constructId.id},
			#{surveyId.id},
			#{designId.id},
			#{fullProjectStatus},
			#{companyId},
			#{workProject.id},
			#{officeId},
			#{masterId},
			#{ext},
			#{contractName},
			#{isTotal},
			#{psort},
			#{ssort},
			#{totalFullId},
			#{isTotalName},
			#{totalFullIds},
			#{commonFlag},
			#{sort}
		)
	</insert>
	
	<update id="update">
		UPDATE work_full_manage SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			contract_id = #{contractId},
			full_project_number = #{fullProjectNumber},
			full_project_source = #{fullProjectSource},
			full_project_name = #{fullProjectName},
			full_industry_type = #{fullIndustryType},
			full_structure_type = #{fullStructureType},
			full_invest_money = #{fullInvestMoney},
			client_id = #{clientId},
			build_id = #{buildId},
			full_project_type = #{fullProjectType},
			full_project_address = #{area.id},
			full_project_property = #{fullProjectProperty},
			full_project_scale = #{fullProjectScale},
			full_count_unit = #{fullCountUnit},
			full_project_feature = #{fullProjectFeature},
			host_company = #{hostCompany.id},
			follow_company = #{followCompany.id},
			talk_start_time = #{talkStartTime},
			talk_end_time = #{talkEndTime},
			ask_pay_mode = #{askPayMode},
			standard_explain = #{standardExplain},
			fixed_price = #{fixedPrice},
			client_require = #{clientRequire},
			host_master = #{hostMaster},
			follow_master = #{followMaster},
			host_member = #{hostMember},
			follow_member = #{followMember},
			constructId = #{constructId.id},
			surveyId = #{surveyId.id},
			designId = #{designId.id},
			full_project_status = #{fullProjectStatus},
			area_parent_ids = #{area.parentIds},
			office_id = #{officeId},
			masterId = #{masterId},
			ext = #{ext},
			psort = #{psort},
			ssort = #{ssort},
			contractName = #{contractName},
			sort = #{sort}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_full_manage
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_full_manage SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkFullManage" statementType="STATEMENT">
		SELECT * form work_full_manage where ${propertyName} = '${value}'

		<!--SELECT
		<include refid="workFullManageColumns"/>
		FROM work_full_manage a
		<include refid="workFullManageJoins"/>-->
	</select>
	
	<!-- 批量插入主办方项目负责人,协办方项目负责人,主办方项目成员,协办方项目成员 -->
	<insert id="insertHostMaster">
		INSERT INTO work_full_user(project_id,user_id,status)
		<foreach collection="hostMasterList" item="user" separator=" union all ">
			SELECT #{id}, #{user.id},'1' FROM dual
		</foreach>
	</insert>
	<insert id="insertFollowMaster">
		INSERT INTO work_full_user(project_id,user_id,status)
		<foreach collection="followMasterList" item="user" separator=" union all ">
			SELECT #{id}, #{user.id},'2' FROM dual
		</foreach>
	</insert>
	<insert id="insertHostMember">
		INSERT INTO work_full_user(project_id,user_id,status)
		<foreach collection="hostMemberList" item="user" separator=" union all ">
			SELECT #{id}, #{user.id},'3' FROM dual
		</foreach>
	</insert>
	<insert id="insertFollowMember">
		INSERT INTO work_full_user(project_id,user_id,status)
		<foreach collection="followMemberList" item="user" separator=" union all ">
			SELECT #{id}, #{user.id},'4' FROM dual
		</foreach>
	</insert>

	<select id="getByPId" resultType="WorkFullManage">
		SELECT
		<include refid="workFullManageColumns"/>
		FROM work_full_manage a
		<include refid="workFullManageJoins"/>
		WHERE a.project_id = #{id}
	</select>


	<select id="findListByprojectt" resultType="WorkFullManage">
		SELECT
		*
		FROM work_full_manage a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="isTotal != null and isTotal != ''">
				AND a.is_total = #{isTotal}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findByList" resultType="WorkFullManage">
		SELECT
		a.id,a.sort
		FROM work_full_manage a
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			AND (
			a.total_full_id = #{totalFullId} OR a.id= #{totalFullId})
		</where>
		ORDER BY a.sort DESC
	</select>
</mapper>