<?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.WorkMonthlyPlanDao">

	<sql id="workMonthlyPlanColumns">
		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.plan_monthly AS "planMonthly",
		a.office_id AS "officeId",
		o.top_company as officeName,
		u.name as name,
		a.plan_status AS "planStatus",
		a.company_id AS "companyId",
		a.year_sum_money AS "yearSumMoney",
		a.before_sum_money AS "beforeSumMoney",
		a.remaining_sum_money AS "remainingSumMoney",
		a.this_sum_money AS "thisSumMoney",
		a.estimate_price AS "estimatePrice",
		a.accumulative_price AS "accumulativePrice"
	</sql>

	<sql id="workMonthlyPlanJoins">
		LEFT JOIN sys_office o ON o.id = a.office_id
		LEFT JOIN sys_office s ON s.id = a.company_id
		LEFT JOIN sys_user u ON u.id = a.create_by
	</sql>


	<select id="get" resultType="WorkMonthlyPlan" >
		SELECT
		<include refid="workMonthlyPlanColumns"/>
		FROM work_monthly_plan a
		<include refid="workMonthlyPlanJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="WorkMonthlyPlan" >
		SELECT
		<include refid="workMonthlyPlanColumns"/>
		FROM work_monthly_plan a
		<include refid="workMonthlyPlanJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="planMonthly != null and planMonthly != ''">
				AND a.plan_monthly = #{planMonthly}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
			<if test="planStatus != null and planStatus != ''">
				AND a.plan_status = #{planStatus}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			${sqlMap.dsf}
		</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="queryCount" resultType="int" >
		SELECT
		count(1)
		FROM work_monthly_plan a
		<include refid="workMonthlyPlanJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="planMonthly != null and planMonthly != ''">
				AND a.plan_monthly = #{planMonthly}
			</if>
			<if test="officeId != null and officeId != ''">
				AND a.office_id = #{officeId}
			</if>
			<if test="planStatus != null and planStatus != ''">
				AND a.plan_status = #{planStatus}
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			${sqlMap.dsf}
		</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="WorkMonthlyPlan" >
		SELECT
		<include refid="workMonthlyPlanColumns"/>
		FROM work_monthly_plan a
		<include refid="workMonthlyPlanJoins"/>
		<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(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			plan_monthly,
			office_id,
			plan_status,
			company_id,
			year_sum_money,
			before_sum_money,
			remaining_sum_money,
			this_sum_money,
			estimate_price,
			accumulative_price
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{planMonthly},
			#{officeId},
			#{planStatus},
			#{companyId},
			#{yearSumMoney},
			#{beforeSumMoney},
			#{remainingSumMoney},
			#{thisSumMoney},
			#{estimatePrice},
			#{accumulativePrice}
		)
	</insert>

	<update id="update">
		UPDATE work_monthly_plan SET
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			plan_monthly = #{planMonthly},
			office_id = #{officeId},
			plan_status = #{planStatus},
			company_id = #{companyId},
			year_sum_money = #{yearSumMoney},
			before_sum_money = #{beforeSumMoney},
			remaining_sum_money = #{remainingSumMoney},
			this_sum_money = #{thisSumMoney},
			estimate_price = #{estimatePrice},
			accumulative_price = #{accumulativePrice}
		WHERE id = #{id}
	</update>


	<!--物理删除-->
	<update id="delete">
			UPDATE work_monthly_plan SET
			del_flag = '1'
		WHERE id = #{id}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_monthly_plan SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>


	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkMonthlyPlan" statementType="STATEMENT">
		select * FROM work_monthly_plan  where ${propertyName} = '${value}'
	</select>


	<select id="findHisList" resultType="WorkMonthlyPlan" >
		select <include refid="workMonthlyPlanColumns"/>
		FROM work_monthly_plan a
		<include refid="workMonthlyPlanJoins"/>
		where a.office_id = #{officeId} and a.del_flag = 0 and a.plan_status=1 order by a.plan_monthly desc limit 0,12
	</select>

	<!--删除子表-->
	<update id="deleteWorkMonthlyPlanThis">
		UPDATE work_monthly_plan_this SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE work_monthly_plan_id = #{id}
	</update>


	<select id="getThisMaxMonthly" resultType="WorkMonthlyPlan" >
		SELECT DATE_FORMAT(max(t.plan_monthly),'%Y-%m-%d') planMonthly
            FROM work_monthly_plan t
           WHERE t.office_id = #{officeId}
           AND t.del_flag =0
	</select>

    <select id="getMonthly" resultType="WorkMonthlyPlan" >
		SELECT *
           FROM work_monthly_plan a
           WHERE a.plan_monthly = #{planMonthly}
           AND a.del_flag =0
           and a.office_id = #{officeId}
           and a.id !=#{id}
	</select>


<!--	<select id="getBeforeThisMonthMoney" parameterType="Map" resultType="int" >
		SELECT
		IFNULL(sum(register_amt),0) as register_amt
		FROM
		work_receipts_register_detail w
		WHERE
		w.invoice_id IN (SELECT d.id FROM work_invoice_detail d WHERE d.invoice_id
		IN (
		SELECT i.id FROM work_invoice i WHERE i.project_id
		IN (
		SELECT pr.id FROM project_records pr WHERE pr.contract_id
		IN (
		 SELECT t.id FROM work_contract_info t WHERE t.client_id =#{clientId} and t.office_id=#{officeId} ) ) ) )
		AND w.status = 1
		AND w.register_date between (
		SELECT MAKEDATE(YEAR(NOW()), 1))
		AND  ( SELECT date_sub( date_add( #{planMonthly},INTERVAL - DAY (#{planMonthly}) + 1 DAY),INTERVAL 24 HOUR))
	</select>-->
	<select id="getBeforeThisMonthMoney"  resultType="WorkMonthlyPlanThis" >
       SELECT
		IFNULL(sum(register_amt),0) as beforeThisMonthMoney,
       (SELECT
		IFNULL(sum(register_amt),0) as accumulativePrice
		FROM
		work_receipts_register_detail w
		WHERE
		w.invoice_id IN (SELECT d.id FROM work_invoice_detail d WHERE d.invoice_id
		IN (
		SELECT i.id FROM work_invoice i WHERE i.project_id
		IN (
		SELECT pr.id FROM project_records pr WHERE pr.contract_id=#{contractId}
		 ) ) )
		AND w.status = 1) as accumulativePrice
		FROM
		work_receipts_register_detail w
		WHERE
		w.invoice_id IN (SELECT d.id FROM work_invoice_detail d WHERE d.invoice_id
		IN (
		SELECT i.id FROM work_invoice i WHERE i.project_id
		IN (
		SELECT pr.id FROM project_records pr WHERE pr.contract_id=#{contractId}
		 ) ) )
		AND w.status = 1
		AND w.register_date between (
		SELECT MAKEDATE(YEAR(NOW()), 1))
		AND  ( SELECT date_sub( date_add(#{planMonthly},INTERVAL - DAY (#{planMonthly}) + 1 DAY),INTERVAL 24 HOUR))
	</select>
</mapper>