<?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.workmonthlyplan.dao.WorkMonthlyPlanThisDao">
    
	<sql id="workMonthlyPlanThisColumns">
		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.work_monthly_plan_id AS "workMonthlyPlanId",
		a.work_client_info_id AS "workClientInfoId",
		a.annual_collection_plan AS "annualCollectionPlan",
		a.before_this_month_money AS " beforeThisMonthMoney",
		a.remaining_plan AS "remainingPlan",
		a.this_month_plan_money AS " thisMonthPlanMoney",
		a.client_name as "clientName",
		a.estimate_price AS " estimatePrice",
		a.accumulative_price AS " accumulativePrice",
		a.contract_num AS " contractNum",
		a.contract_name AS " contractName",
		DATE_FORMAT(w.plan_monthly, '%Y-%m') as "planMonthly",
		o.name as "officeName"
	</sql>

	<sql id="workMonthlyPlanThisJoins">
		left  JOIN work_client_info c on c.id=a.work_client_info_id
		left join work_monthly_plan w on a.work_monthly_plan_id=w.id
		left join sys_office o on o.id=w.office_id
	</sql>
	
    
	<select id="get" resultType="WorkMonthlyPlanThis" >
		SELECT 
			<include refid="workMonthlyPlanThisColumns"/>
		FROM work_monthly_plan_this a
		<include refid="workMonthlyPlanThisJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkMonthlyPlanThis" >
		SELECT 
			<include refid="workMonthlyPlanThisColumns"/>
		FROM work_monthly_plan_this a
		<include refid="workMonthlyPlanThisJoins"/>
		<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>
	
	<select id="findAllList" resultType="WorkMonthlyPlanThis" >
		SELECT 
			<include refid="workMonthlyPlanThisColumns"/>
		FROM work_monthly_plan_this a
		<include refid="workMonthlyPlanThisJoins"/>
		<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_monthly_plan_this(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			work_monthly_plan_id,
			work_client_info_id,
			annual_collection_plan,
			before_this_month_money,
			remaining_plan,
			this_month_plan_money,
			estimate_price,
			accumulative_price,
			contract_num,
			client_name,
			contract_name
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{workMonthlyPlanId},
			#{workClientInfoId},
			#{annualCollectionPlan},
			#{beforeThisMonthMoney},
			#{remainingPlan},
			#{thisMonthPlanMoney},
			#{estimatePrice},
			#{accumulativePrice},
			#{contractNum},
			#{clientName},
			#{contractName}
		)
	</insert>

	<update id="update">
		UPDATE work_monthly_plan_this SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			work_monthly_plan_id = #{workMonthlyPlanId},
			work_client_info_id = #{workClientInfoId},
			annual_collection_plan = #{annualCollectionPlan},
			before_this_month_money = #{beforeThisMonthMoney},
			remaining_plan = #{remainingPlan},
			del_flag =#{delFlag},
			this_month_plan_money = #{thisMonthPlanMoney},
			estimate_price = #{estimatePrice},
			accumulative_price = #{accumulativePrice},
			contract_num = #{contractNum},
			client_name = #{clientName},
			contract_name = #{contractName}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE work_monthly_plan_this SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_monthly_plan_this SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkMonthlyPlanThis" statementType="STATEMENT">
		select * FROM work_monthly_plan_this  where ${propertyName} = '${value}'
	</select>

	<select id="findByWorkMonthlyPlanId" resultType="WorkMonthlyPlanThis" >
	select <include refid="workMonthlyPlanThisColumns"/>
	FROM work_monthly_plan_this a
	<include refid="workMonthlyPlanThisJoins"/>
	where a.work_monthly_plan_id = #{workMonthlyPlanId} and a.del_flag = 0
</select>

	<select id="findByWorkMonthlyPlanThis" resultType="WorkMonthlyPlanThis" >
		select
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.work_client_info_id AS "workClientInfoId",
		a.annual_collection_plan AS "annualCollectionPlan",
		/*a. before_this_month_money AS " beforeThisMonthMoney",*/
		/*a.remaining_plan AS "remainingPlan",*/
		/*a. this_month_plan_money AS " thisMonthPlanMoney",*/
		a.client_name as "clientName",
		a.estimate_price AS " estimatePrice",
		a.contract_num AS " contractNum",
		a.contract_name AS " contractName"
		FROM work_monthly_plan_this a
		<include refid="workMonthlyPlanThisJoins"/>
		where a.work_monthly_plan_id = #{workMonthlyPlanId} and a.del_flag = 0
	</select>
</mapper>