<?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="targetContractAmount"    column="target_contract_amount"    />
        <result property="targetArrivalAmount"    column="target_arrival_amount"    />
        <result property="remark"    column="remark"    />
        <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,  target_contract_amount, target_arrival_amount, remark 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.target_contract_amount, p.target_arrival_amount, p.remark, 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>
            is_del = '0'
            <if test="sysDeptId != null "> and sys_dept_id = #{sysDeptId}</if>
            <if test="year != null  and year != ''"> and year = #{year}</if>
            <if test="targetContractAmount != null "> and target_contract_amount = #{targetContractAmount}</if>
            <if test="targetArrivalAmount != null "> and target_arrival_amount = #{targetArrivalAmount}</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="targetContractAmount != null">target_contract_amount,</if>
            <if test="targetArrivalAmount != null">target_arrival_amount,</if>
            <if test="remark != null">remark,</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="targetContractAmount != null">#{targetContractAmount},</if>
            <if test="targetArrivalAmount != null">#{targetArrivalAmount},</if>
            <if test="remark != null">#{remark},</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="targetContractAmount != null">target_contract_amount = #{targetContractAmount},</if>
            <if test="targetArrivalAmount != null">target_arrival_amount = #{targetArrivalAmount},</if>
            <if test="remark != null">remark = #{remark},</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" parameterType="string"  resultMap="BmDeptPlanResult">
		select ifnull(p.target_contract_amount,0) as target_contract_amount , ifnull(p.target_arrival_amount,0) as target_arrival_amount, d.dept_name
		from bm_dept_plan p right join sys_dept d on p.sys_dept_id = d.dept_id and p.is_del = '0' and p.year = #{year} where d.parent_id = 124
    </select>



    <select id="selectUnitClueList" parameterType="string"  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_id,
		sd.dept_name,
		sd.parent_id,
		a.sys_dept_id,
		(ifnull(a.project_million_num,0)+ ifnull(a.project_pro_num,0)+ ifnull(a.project_investment_num,0))as plan
	FROM
		sys_dept sd
		LEFT JOIN `bm_dept_plan_area` a ON a.sys_dept_id = sd.dept_id
		AND a.is_del = '0'
	WHERE
		sd.del_flag = '0'
	) s1
	LEFT JOIN bm_project p ON p.dept_id = s1.sys_dept_id
	AND p.is_del = '0'
	and year(p.create_time) = #{year} and p.project_status in (1,2,3,4)
	where
	s1.parent_id = 105
    GROUP BY
	s1.dept_id
    ORDER BY
	successMission DESC,
	plan DESC
    </select>

	<select id="selectUnitClueCityList" parameterType="string"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	ifnull( s1.dept_name, '' ) AS deptName,
	ifnull( s1.target_arrival_amount, 0 ) AS targetArrivalAmount,
	ifnull( count( p.id ), 0 ) AS successMission
    FROM
	(
	SELECT
		sd.dept_id,
		sd.dept_name,
		sd.parent_id,
		a.sys_dept_id,
		a.target_arrival_amount
	FROM
		sys_dept sd
		LEFT JOIN `bm_dept_plan` a ON a.sys_dept_id = sd.dept_id
		AND a.is_del = '0'
	WHERE
		sd.del_flag = '0'
	) s1
	LEFT JOIN bm_project p ON p.dept_id = s1.sys_dept_id
	AND p.is_del = '0'
	and year(p.create_time) = #{year} and p.project_status in (1,2,3,4)
	where
	s1.parent_id = 124
    GROUP BY
	s1.dept_id
    ORDER BY
	successMission DESC,
	targetArrivalAmount DESC
    </select>

    <select id="selectTargetSigningList" parameterType="String"  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_id,
		sd.dept_name,
		sd.parent_id,
		a.sys_dept_id,
		(ifnull(a.project_million_num,0)+ ifnull(a.project_pro_num,0)+ ifnull(a.project_investment_num,0))as target_signing_num
	FROM
	    sys_dept sd
		LEFT JOIN `bm_dept_plan_area` a ON a.sys_dept_id = sd.dept_id and a.is_del = '0'
		where 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 and year(p.create_time) = #{year}
	where
	s1.parent_id = 105
    GROUP BY
	s1.dept_id
    ORDER BY
	successMission DESC,
	targetSigningNum DESC
    </select>

    <select id="selectTotAmtList"  parameterType="String" resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	d.dept_name AS deptName,
	ifnull( dp.target_arrival_amount, 0 ) AS targetArrivalAmount,
	ifnull( p.tot_amt, 0 ) AS totAmt
    FROM
	sys_dept d
	LEFT JOIN bm_project p ON p.dept_id = d.dept_id
	AND p.is_del = '0' and year(p.create_time) = #{year}
	LEFT JOIN bm_dept_plan dp ON dp.sys_dept_id = d.dept_id and year(dp.create_time) = #{year}
    WHERE
	d.del_flag = '0'
	and d.parent_id = 124
    GROUP BY
	d.dept_id
    ORDER BY
	targetArrivalAmount DESC,
	totAmt DESC
    </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  year(d.create_time) = #{year}
	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
	WHERE
	sd.del_flag = '0'
    ORDER BY
	s1.payForMoney DESC
    </select>

    <select id="selectProjectUnitAmountUnit"  parameterType="string" resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	sd.dept_name AS deptName,
	ifnull( s1.payForMoney, 0 ) AS payForMoney,
	ifnull(d.target_contract_amount,0) as targetContractAmount
    FROM
	sys_dept sd
	LEFT JOIN `bm_dept_plan` d ON d.sys_dept_id = sd.dept_id
	AND d.is_del = '0' and year(d.create_time) = #{year}
	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
	WHERE
	sd.del_flag = '0'
	and sd.parent_id = 124
    ORDER BY
	s1.payForMoney DESC
    </select>

	<select id="selectProjectAreaAmountUnit"  parameterType="string" 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_id,
		sd.dept_name,
		sd.parent_id,
		a.sys_dept_id,
		(ifnull(a.project_million_num,0)+ ifnull(a.project_pro_num,0)+ ifnull(a.project_investment_num,0))as target_signing_num
	FROM
	    sys_dept sd
		LEFT JOIN `bm_dept_plan_area` a ON a.sys_dept_id = sd.dept_id and a.is_del = '0'
		where 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 in (8,9) and year(p.create_time) = #{year}
	where
	s1.parent_id = 105
    GROUP BY
	s1.dept_id
    ORDER BY
	successMission DESC,
	targetSigningNum DESC
    </select>

    <select id="selectInvestmentList" parameterType="string"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	d.dept_name AS deptName,
	(ifnull( dp.project_million_num, 0 )+ ifnull( dp.project_pro_num, 0 )+ ifnull( dp.project_investment_num, 0 )) AS targetSigningNum,
	ifnull( count(p.id), 0 ) AS successMission
    FROM
	sys_dept d
	LEFT JOIN bm_project p ON p.dept_id = d.dept_id
	and p.is_del = '0' and p.project_status &gt; 8 and year(p.create_time) = #{year}
	LEFT JOIN bm_dept_plan_area dp ON dp.sys_dept_id = d.dept_id and year(dp.create_time) = #{year}
	where d.del_flag = '0' and d.parent_id = 105
    GROUP BY
	d.dept_id
	order by successMission DESC,
	targetSigningNum DESC
    </select>

	<select id="selectInvestmentCityList" parameterType="string"  resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	sd.dept_name AS deptName,
	ifnull( s1.payForMoney, 0 ) AS payForMoney,
	ifnull( count(p.id), 0 ) AS successMission
    FROM
	sys_dept sd
	LEFT JOIN `bm_project` p ON p.dept_id = sd.dept_id and p.project_status &gt; 8
	AND p.is_del = '0' and year(p.create_time) = #{year}
	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 = p.dept_id
	WHERE
	sd.del_flag = '0'
	and sd.parent_id = 124
	GROUP BY
	sd.dept_id
    ORDER BY
	s1.payForMoney 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 p.is_del = '0' and p.project_status &gt; 8 and year(p.create_time) = #{year}
	where d.del_flag = '0'
    GROUP BY
	d.dept_id
	ORDER BY successMission
    </select>
</mapper>