<?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.ruoyi.system.mapper.project.BmDeptPlanMapper">
    
    <resultMap type="BmDeptPlan" id="BmDeptPlanResult">
        <result property="id"    column="id"    />
        <result property="sysDeptId"    column="sys_dept_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="year"    column="year"    />
        <result property="plan"    column="plan"    />
        <result property="targetSigningNum"    column="target_signing_num"    />
        <result property="targetContractAmount"    column="target_contract_amount"    />
        <result property="targetArrivalAmount"    column="target_arrival_amount"    />
        <result property="targetInvestmentNum"    column="target_investment_num"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <sql id="selectBmDeptPlanVo">
        select id, sys_dept_id, create_by, create_time, update_by, update_time, is_del, year, plan, target_signing_num, target_contract_amount, target_arrival_amount, target_investment_num from bm_dept_Plan
    </sql>

    <select id="selectBmDeptPlanList" parameterType="BmDeptPlan" resultMap="BmDeptPlanResult">
        select p.id, p.sys_dept_id, p.create_by, p.create_time, p.update_by, p.update_time, p.is_del, p.year, p.plan, p.target_signing_num, p.target_contract_amount, p.target_arrival_amount, p.target_investment_num, d.dept_name
        from bm_dept_plan p
        left join sys_dept d on p.sys_dept_id = d.dept_id and p.is_del = '0'
        <where>
            <if test="sysDeptId != null "> and sys_dept_id = #{sysDeptId}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="year != null  and year != ''"> and year = #{year}</if>
            <if test="plan != null "> and plan = #{plan}</if>
            <if test="targetSigningNum != null "> and target_signing_num = #{targetSigningNum}</if>
            <if test="targetContractAmount != null "> and target_contract_amount = #{targetContractAmount}</if>
            <if test="targetArrivalAmount != null "> and target_arrival_amount = #{targetArrivalAmount}</if>
            <if test="targetInvestmentNum != null "> and target_investment_num = #{targetInvestmentNum}</if>
        </where>
        order by p.create_time DESC
    </select>

    <select id="selectBmDeptPlanById" parameterType="Long" resultMap="BmDeptPlanResult">
        <include refid="selectBmDeptPlanVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertBmDeptPlan" parameterType="BmDeptPlan" useGeneratedKeys="true" keyProperty="id">
        insert into bm_dept_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="sysDeptId != null">sys_dept_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="year != null">year,</if>
            <if test="plan != null">plan,</if>
            <if test="targetSigningNum != null">target_signing_num,</if>
            <if test="targetContractAmount != null">target_contract_amount,</if>
            <if test="targetArrivalAmount != null">target_arrival_amount,</if>
            <if test="targetInvestmentNum != null">target_investment_num,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sysDeptId != null">#{sysDeptId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="year != null">#{year},</if>
            <if test="plan != null">#{plan},</if>
            <if test="targetSigningNum != null">#{targetSigningNum},</if>
            <if test="targetContractAmount != null">#{targetContractAmount},</if>
            <if test="targetArrivalAmount != null">#{targetArrivalAmount},</if>
            <if test="targetInvestmentNum != null">#{targetInvestmentNum},</if>
         </trim>
    </insert>

    <update id="updateBmDeptPlan" parameterType="BmDeptPlan">
        update bm_dept_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="sysDeptId != null">sys_dept_id = #{sysDeptId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="year != null">year = #{year},</if>
            <if test="plan != null">plan = #{plan},</if>
            <if test="targetSigningNum != null">target_signing_num = #{targetSigningNum},</if>
            <if test="targetContractAmount != null">target_contract_amount = #{targetContractAmount},</if>
            <if test="targetArrivalAmount != null">target_arrival_amount = #{targetArrivalAmount},</if>
            <if test="targetInvestmentNum != null">target_investment_num = #{targetInvestmentNum},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBmDeptPlanById" parameterType="Long">
        delete from bm_dept_plan where id = #{id}
    </delete>

    <delete id="deleteBmDeptPlanByIds" parameterType="String">
        delete from bm_dept_plan where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="selectUnitList"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	ifnull(s1.dept_name,'') AS deptName,
	ifnull(s1.plan,0) AS targetInvestmentNum,
	ifnull(count( p.id ),0) AS successMission
    FROM
	(
	SELECT
		sd.dept_name,
		d.sys_dept_id,
		d.plan
	FROM
	    sys_dept sd
		LEFT JOIN `bm_dept_plan` d ON d.sys_dept_id = sd.dept_id and d.is_del = '0' and sd.del_flag = '0'
	) s1
	LEFT JOIN bm_project p ON p.dept_id = s1.sys_dept_id and p.is_del = '0'
    GROUP BY
	s1.sys_dept_id
    ORDER BY
	successMission DESC,
	targetInvestmentNum DESC
    </select>

    <select id="selectUnitAmountList"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	sd.dept_name AS deptName,
	ifnull(d.target_arrival_amount,0) AS targetArrivalAmount,
	ifnull(s1.payForMoney,0) as payForMoney
    FROM
    sys_dept sd
	LEFT JOIN `bm_dept_plan` d ON d.sys_dept_id = sd.dept_id and sd.del_flag = '0'
	AND d.is_del = '0'
	LEFT JOIN (
	SELECT
		sum(s.payForMoney ) AS payForMoney,
		s.dept_id
	FROM
		(
		SELECT
			cp.pay_for_money AS payForMoney,
			p.dept_id
		FROM
			bm_project p
			LEFT JOIN bm_construction_pay cp ON cp.bm_project_id = p.id
			AND p.is_del = '0'
		) s
	GROUP BY
		s.dept_id
	) s1 ON s1.dept_id = d.sys_dept_id
    ORDER BY
	s1.payForMoney DESC,
	d.target_arrival_amount DESC
    </select>

    <select id="selectUnitClueList"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	ifnull(s1.dept_name,'') AS deptName,
	ifnull(s1.plan,0) AS plan,
	ifnull(count( p.id ),0) AS successMission
    FROM
	(
	SELECT
		sd.dept_name,
		d.sys_dept_id,
		d.plan
	FROM
	    sys_dept sd
		LEFT JOIN `bm_dept_plan` d ON d.sys_dept_id = sd.dept_id and d.is_del = '0' and sd.del_flag = '0'
	) s1
	LEFT JOIN bm_project p ON p.dept_id = s1.sys_dept_id and p.is_del = '0'
    GROUP BY
	s1.sys_dept_id
    ORDER BY
	successMission DESC,
	plan DESC
    </select>

    <select id="selectTargetSigningList"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	ifnull(s1.dept_name,'') AS deptName,
	ifnull(s1.target_signing_num,0) AS targetSigningNum,
	ifnull(count( p.id ),0) AS successMission
    FROM
	(
	SELECT
		sd.dept_name,
		d.sys_dept_id,
		d.target_signing_num
	FROM
	    sys_dept sd
		LEFT JOIN `bm_dept_plan` d ON d.sys_dept_id = sd.dept_id and d.is_del = '0' and sd.del_flag = '0'
	) s1
	LEFT JOIN bm_project p ON p.dept_id = s1.sys_dept_id and p.is_del = '0' and p.project_status &gt; 4
    GROUP BY
	s1.sys_dept_id
    ORDER BY
	successMission DESC,
	targetSigningNum DESC
    </select>

    <select id="selectTotAmtList"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	d.dept_name as deptName,
	ifnull(dp.target_contract_amount,0) as targetContractAmount,
	ifnull(p.tot_amt,0) as totAmt
    FROM
	sys_dept d
	LEFT JOIN bm_project p ON p.dept_id = d.dept_id
	AND d.del_flag = '0' and p.is_del = '0'
	LEFT JOIN bm_dept_plan dp ON dp.sys_dept_id = d.dept_id
	GROUP BY d.dept_id
    </select>


    <select id="selectProjectUnitAmountEharts"  parameterType="string" resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	sd.dept_name AS deptName,
	ifnull( s1.payForMoney, 0 ) AS payForMoney
    FROM
	sys_dept sd
	LEFT JOIN `bm_dept_plan` d ON d.sys_dept_id = sd.dept_id
	AND sd.del_flag = '0'
	AND d.is_del = '0'
	LEFT JOIN (
	SELECT
		sum( s.payForMoney ) AS payForMoney,
		s.dept_id
	FROM
		(
		SELECT
			cp.pay_for_money AS payForMoney,
			p.dept_id
		FROM
			bm_project p
			LEFT JOIN bm_construction_pay cp ON cp.bm_project_id = p.id
			AND p.is_del = '0' and year(cp.create_time) = #{year}
		) s
	GROUP BY
		s.dept_id
	) s1 ON s1.dept_id = d.sys_dept_id
    ORDER BY
	s1.payForMoney DESC
    </select>


    <select id="selectInvestmentList"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	d.dept_name AS deptName,
	ifnull( dp.target_investment_num, 0 ) AS targetInvestmentNum,
	ifnull( count(p.id), 0 ) AS successMission
    FROM
	sys_dept d
	LEFT JOIN bm_project p ON p.dept_id = d.dept_id
	AND d.del_flag = '0' and p.is_del = '0' and p.project_status &gt; 8
	LEFT JOIN bm_dept_plan dp ON dp.sys_dept_id = d.dept_id
    GROUP BY
	d.dept_id
	order by successMission DESC,
	targetInvestmentNum DESC
    </select>



    <select id="selectProjectInvestmentEharts"  parameterType="string" resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	d.dept_name AS deptName,
	ifnull( count(p.id), 0 ) AS successMission
    FROM
	sys_dept d
	LEFT JOIN bm_project p ON p.dept_id = d.dept_id
	AND d.del_flag = '0' and p.is_del = '0' and p.project_status &gt; 8 and year(p.create_time) = #{year}
	LEFT JOIN bm_dept_plan dp ON dp.sys_dept_id = d.dept_id
    GROUP BY
	d.dept_id
    </select>
</mapper>