<?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.workreceiptsregister.dao.WorkReceiptsRegisterDao">
    
	<sql id="workReceiptsRegisterColumns">
		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.del_flag AS "delFlag",
		a.arrival_date AS "arrivalDate",
		a.client_accno AS "clientAccno",
		a.client_accname AS "clientAccname",
		a.arrival_amt AS "arrivalAmt",
		a.register_amt AS "registerAmt",
		a.status AS "status",
		a.company_id AS "company.id",
		s1.name AS "company.name",
		a.office_id AS "office.id",
		a.com_id AS "comp.id",
		a.project_id AS "project.id",
		a.ext
	</sql>
	
	<sql id="workReceiptsRegisterJoins">
        LEFT JOIN sys_office o on a.office_id = o.id
        LEFT JOIN sys_office s ON s.id = a.com_id
        LEFT JOIN sys_office s1 ON s1.id = a.company_id
        LEFT JOIN sys_user u ON u.id = a.create_by
	</sql>
	
    
	<select id="get" resultType="WorkReceiptsRegister" >
		SELECT 
			<include refid="workReceiptsRegisterColumns"/>
		FROM work_receipts_register a
		<include refid="workReceiptsRegisterJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReceiptsRegister" >
		SELECT 
			<include refid="workReceiptsRegisterColumns"/>, COUNT(rd.id) as "detailCou"
		FROM work_receipts_register a
		<include refid="workReceiptsRegisterJoins"/>
        LEFT JOIN work_receipts_register_detail rd ON a.id = rd.register_id
		<where>
            <if test="clientAccno != null and clientAccno !=''">
                a.client_accno LIKE concat('%',#{clientAccno},'%')
            </if>
            <if test="clientAccname != null and clientAccname !=''">
                AND a.client_accname LIKE concat('%',#{clientAccname},'%')
            </if>
            <if test="beginDate != null and beginDate !=''">
                AND a.arrival_date >= #{beginDate}
            </if>
            <if test="endDate != null and endDate !=''">
                AND a.arrival_date &lt;= #{endDate}
            </if>
            <if test="status != null and status !=''">
                AND a.status = #{status}
            </if>
            <if test="minArr != null and minArr !=0">
                AND a.arrival_amt+0 >= #{minArr}
            </if>
            <if test="maxArr != null and maxArr !=0">
                AND a.arrival_amt+0 &lt;= #{maxArr}
            </if>
            <if test="minReg != null and minReg !=0">
                AND a.register_amt+0 >= #{minReg}
            </if>
            <if test="maxReg != null and maxReg !=0">
                AND a.register_amt+0 &lt;= #{maxReg}
            </if>
			AND a.del_flag = #{DEL_FLAG_NORMAL}
            ${sqlMap.dsf}
		</where>
        GROUP BY a.id
		<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="WorkReceiptsRegister" >
		SELECT 
			<include refid="workReceiptsRegisterColumns"/>
		FROM work_receipts_register a
		<include refid="workReceiptsRegisterJoins"/>
		<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_receipts_register(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			del_flag,
			arrival_date,
			client_accno,
			client_accname,
			arrival_amt,
			register_amt,
			status,
			company_id,
			office_id,
		    com_id,
		    project_id,
		    ext
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{delFlag},
			#{arrivalDate},
			#{clientAccno},
			#{clientAccname},
			#{arrivalAmt},
			#{registerAmt},
			#{status},
			#{company.id},
			#{office.id},
		    #{comp.id},
		    #{project.id},
		    #{ext}
		)
	</insert>
	
	<update id="update">
		UPDATE work_receipts_register SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			arrival_date = #{arrivalDate},
			client_accno = #{clientAccno},
			client_accname = #{clientAccname},
			arrival_amt = #{arrivalAmt},
			register_amt = #{registerAmt},
			status = #{status},
			company_id = #{company.id},
			office_id = #{office.id},
		    com_id = #{comp.id},
		    project_id = #{project.id},
		    ext = #{ext}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		UPDATE work_receipts_register SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_receipts_register SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReceiptsRegister" statementType="STATEMENT">
		select * FROM work_receipts_register  where ${propertyName} = '${value}'
	</select>

    <select id="findCompanyList" resultType="com.jeeplus.modules.workreceiptsregister.entity.WorkReceiptsRegisterCompany">
        select
		  a.id AS "id",
		  a.top_company AS "name",
		   ifnull(sum(t3.register_amt),0) as "receiptAmt",
		  ifnull(sum(t4.revise_money),0) as "inAmt",
			ifnull(sum(t5.revise_money),0) as "outAmt"
		from sys_office a
		left join sys_office t2 on (t2.parent_ids like concat('%',a.id,'%') and t2.type='2')
		LEFT JOIN (SELECT * from work_receipts_register WHERE arrival_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') AND del_flag='0') t3 ON t3.office_id = t2.id
		LEFT JOIN (SELECT * from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='1' and del_flag = '0' and status = '5') t4 on t4.office_id = t2.id
		LEFT JOIN (SELECT * from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='2' and del_flag = '0' and status = '5') t5 on t5.office_id = t2.id
		where a.type in('1','3')
		and ( a.parent_id = #{companyId} or a.id = #{companyId})
		group by a.id
		order by a.create_date ASC
    </select>

    <select id="findOfficeList" resultType="com.jeeplus.modules.workreceiptsregister.entity.WorkReceiptsRegisterOffice">
        select
		  a.id AS "id",
		  a.top_company AS "name",
		ifnull(sum(t3.register_amt),0) as "receiptAmt",
		ifnull(sum(t4.revise_money),0) as "inAmt",
		ifnull(sum(t5.revise_money),0) as "outAmt"
		from sys_office a
		LEFT JOIN (SELECT id, register_date,register_amt,office_id from work_receipts_register_detail WHERE register_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and status ='1') t3 ON t3.office_id = a.id
		LEFT JOIN (SELECT revise_money,office_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='1' and del_flag = '0' and status = 5) t4 on t4.office_id = a.id
		LEFT JOIN (SELECT revise_money,office_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='2' and del_flag = '0' and status = 5) t5 on t5.office_id = a.id
		<where>
			a.del_flag = '0'
            and a.type ='2'
			and a.parent_id=#{companyId}
			<if test="officeId != null and officeId != ''">
				AND a.id = #{officeId}
			</if>
        </where>
		group by a.id
    </select>
	<select id="findOfficeListSum" resultType="WorkReceiptsRegisterOffice" >
		select
		a.id AS "id",
		a.top_company AS "name",
		ifnull(sum(t3.register_amt),0) as "receiptAmt",
		ifnull(sum(t4.revise_money),0) as "inAmt",
		ifnull(sum(t5.revise_money),0) as "outAmt"
		from sys_office a
		LEFT JOIN (SELECT id, register_date,register_amt,office_id from work_receipts_register_detail WHERE register_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and status ='1') t3 ON t3.office_id = a.id
		LEFT JOIN (SELECT revise_money,office_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='1' and del_flag = '0' and status = 5) t4 on t4.office_id = a.id
		LEFT JOIN (SELECT revise_money,office_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='2' and del_flag = '0' and status = 5) t5 on t5.office_id = a.id
	  where
		a.del_flag = '0'
		and a.type ='2'
		and a.parent_ids like concat('%', #{companyId},'%')
	</select>
    <select id="findInfoList" resultType="WorkReceiptsRegisterInfo">
        select
          a.id AS "office.id",
          a.top_company AS "office.name",
          t3.register_amt AS "receiptAmt",
          t4.client_accname AS "clientName",
          t4.arrival_date AS "arrivalDate",
          t5.number AS "invoiceNum"
        from sys_office a
        LEFT JOIN (SELECT register_amt,invoice_id,office_id,register_id from work_receipts_register_detail WHERE register_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and status ='1') t3 ON t3.office_id = a.id
        LEFT JOIN (SELECT id,arrival_date,client_accname from work_receipts_register ) t4 ON t4.id = t3.register_id
        LEFT JOIN (SELECT id,`number` from work_invoice_detail ) t5 on t5.id = t3.invoice_id
        <where>
            <if test="office !=null and office.id !=null and office.id !=''">
                a.parent_ids like concat('%',#{office.id},'%') or a.id = #{office.id}
            </if>
        </where>
    </select>

    <select id="findCompMonthList" resultType="WorkReceiptsRegisterMonth">
        select
           ifnull(sum(t3.register_amt),0)  as "receiptAmt",
             ifnull(sum(t4.revise_money),0)  as "inAmt",
             ifnull(sum(t5.revise_money),0)  as "outAmt",
            date_format(t3.arrival_date, '%Y-%m') AS "datestr"
            FROM work_receipts_register t3
        LEFT JOIN (SELECT revise_money,date_format(revise_date, '%Y-%m') as "datestr",company_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='1' and del_flag = '0' and status = 5) t4 ON t4.datestr = date_format(t3.arrival_date, '%Y-%m')
        LEFT JOIN (SELECT revise_money,date_format(revise_date, '%Y-%m') as "datestr",company_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='2' and del_flag = '0' and status = 5) t5 ON t5.datestr = date_format(t3.arrival_date, '%Y-%m')
        where t3.arrival_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') AND t3.del_flag='0'
       and t3.office_id in (select o.id from sys_office o where o.parent_ids LIKE concat('%' #{companyId}, '%'))
		group by date_format(t3.arrival_date, '%Y-%m')
    </select>

    <select id="findOfficeMonthList" resultType="WorkReceiptsRegisterMonth">
         select
          ifnull(sum(t3.register_amt),0) as "receiptAmt",
		ifnull(sum(t4.revise_money),0)  as "inAmt",
		ifnull(sum(t5.revise_money),0)  as "outAmt",
            date_format(t3.arrival_date, '%Y-%m') AS "datestr"
            FROM work_receipts_register t3
        LEFT JOIN (SELECT revise_money,date_format(revise_date, '%Y-%m') as "datestr",company_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='1' and del_flag = '0' and status = 5) t4 ON t4.datestr = date_format(t3.arrival_date, '%Y-%m')
        LEFT JOIN (SELECT revise_money,date_format(revise_date, '%Y-%m') as "datestr",company_id from work_receipts_revise WHERE revise_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') and revise_type='2' and del_flag = '0' and status = 5) t5 ON t5.datestr = date_format(t3.arrival_date, '%Y-%m')
        where t3.arrival_date BETWEEN concat(#{receiptDate},'-01-01') and concat(#{receiptDate},'-12-31') AND t3.del_flag='0'
       and t3.office_id in (select o.id from sys_office o where o.parent_ids LIKE concat('%' #{companyId}, '%'))
		<if test="officeId != null and officeId != ''">
			AND t3.office_id = #{officeId}
		</if>
		group by date_format(t3.arrival_date, '%Y-%m')
    </select>
</mapper>