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

    <resultMap type="BmProject" id="BmProjectResult">
        <result property="id" column="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="name" column="name"/>
        <result property="industryCategory" column="industry_category"/>
        <result property="industry" column="industry"/>
        <result property="isBackbussiness" column="is_backbussiness"/>
        <result property="projectStatus" column="project_status"/>
        <result property="deptId" column="dept_id"/>
        <result property="totAmt" column="tot_amt"/>
        <result property="isDel" column="is_del"/>
        <result property="orderBeginTime" column="order_begin_time"/>
        <result property="orderEndTime" column="order_end_time"/>
        <result property="remark" column="remark"/>
        <result property="deptName" column="dept_name"/>
        <result property="dictLabel" column="dict_label"/>
        <result property="num" column="num"/>
    </resultMap>

    <sql id="selectBmProjectVo">
        select id, create_by, create_time, update_by, update_time, name, industry_category, industry, is_backbussiness, project_status, dept_id, tot_amt, is_del, order_begin_time, order_end_time, remark from bm_project
    </sql>

    <select id="selectBmProjectList" parameterType="BmProject" resultMap="BmProjectResult">
        SELECT
        p.id,
        p.create_by,
        p.create_time,
        p.update_by,
        p.update_time,
        p.NAME,
        p.industry_category,
        p.industry,
        p.is_backbussiness,
        p.project_status,
        p.dept_id,
        p.tot_amt,
        p.is_del,
        p.order_begin_time,
        p.order_end_time,
        p.remark ,
        d.dept_name,
        sdd.dict_label
        FROM
        bm_project p
        left join sys_dept d on p.dept_id = d.dept_id and p.is_del = '0'
        left join sys_dict_data sdd on p.project_status = sdd.dict_value and sdd.dict_type = 'project_status'
        <where>
            <if test="sysDeptId != null">and p.dept_id = #{sysDeptId}</if>
            <if test="name != null  and name != ''">and p.name like concat('%', #{name}, '%')</if>
            <if test="industryCategory != null  and industryCategory != ''">and p.industry_category =
                #{industryCategory}
            </if>
            <if test="industry != null  and industry != ''">and p.industry = #{industry}</if>
            <if test="projectStatus != null ">and p.project_status = #{projectStatus}</if>
            <if test="deptName != null and deptName != ''">and d.dept_name like concat('%', #{deptName}, '%')</if>
            <if test="totAmt != null ">and p.tot_amt = #{totAmt}</if>
            <if test="isDel != null ">and p.is_del = #{isDel}</if>
            <if test="beginTime != null ">and p.create_time &gt;= #{beginTime}</if>
            <if test="endTime != null ">and p.create_time &lt;= #{endTime}</if>
            <if test="deptList != null  and deptList.size() > 0">
                and p.dept_id in
                <foreach item="deptId" collection="deptList" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
        order by p.create_time DESC
    </select>

    <select id="selectToBeReviewedList" parameterType="BmProject" resultMap="BmProjectResult">
        SELECT
        p.id,
        p.create_by,
        p.create_time,
        p.update_by,
        p.update_time,
        p.NAME,
        p.industry_category,
        p.industry,
        p.is_backbussiness,
        p.project_status,
        p.dept_id,
        p.tot_amt,
        p.is_del,
        p.order_begin_time,
        p.order_end_time,
        p.remark ,
        sdd.dict_label
        FROM
        bm_project p
        left join sys_dict_data sdd on p.project_status = sdd.dict_value and sdd.dict_type = 'project_status'
        <where>
            p.is_del = '0' and
            sdd.dict_value=(sdd.dict_value<![CDATA[>>]]>1)<![CDATA[<<]]>1
            <if test="deptId != null">and p.dept_id = #{deptId}</if>
        </where>
        order by p.create_time DESC
    </select>

    <select id="selectBmProjectById" parameterType="Long" resultMap="BmProjectResult">
        SELECT
        p.id,
        p.create_by,
        p.create_time,
        p.update_by,
        p.update_time,
        p.NAME,
        p.industry_category,
        p.industry,
        p.is_backbussiness,
        p.project_status,
        p.dept_id,
        p.tot_amt,
        p.is_del,
        p.order_begin_time,
        p.order_end_time,
        p.remark ,
        d.dept_name,
        sdd.dict_label
        FROM
        bm_project p
        left join sys_dept d on p.dept_id = d.dept_id
        left join sys_dict_data sdd on p.project_status = sdd.dict_value and sdd.dict_type = 'project_status'
        where p.id = #{id} and p.is_del = '0'
    </select>

    <insert id="insertBmProject" parameterType="BmProject" useGeneratedKeys="true" keyProperty="id">
        insert into bm_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <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="name != null">name,</if>
            <if test="industryCategory != null">industry_category,</if>
            <if test="industry != null">industry,</if>
            <if test="isBackbussiness != null">is_backbussiness,</if>
            <if test="projectStatus != null">project_status,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="totAmt != null">tot_amt,</if>
            <if test="isDel != null">is_del,</if>
            <if test="orderBeginTime != null">order_begin_time,</if>
            <if test="orderEndTime != null">order_end_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <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="name != null">#{name},</if>
            <if test="industryCategory != null">#{industryCategory},</if>
            <if test="industry != null">#{industry},</if>
            <if test="isBackbussiness != null">#{isBackbussiness},</if>
            <if test="projectStatus != null">#{projectStatus},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="totAmt != null">#{totAmt},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="orderBeginTime != null">#{orderBeginTime},</if>
            <if test="orderEndTime != null">#{orderEndTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateBmProject" parameterType="BmProject">
        update bm_project
        <trim prefix="SET" suffixOverrides=",">
            <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="name != null">name = #{name},</if>
            <if test="industryCategory != null">industry_category = #{industryCategory},</if>
            <if test="industry != null">industry = #{industry},</if>
            <if test="isBackbussiness != null">is_backbussiness = #{isBackbussiness},</if>
            <if test="projectStatus != null">project_status = #{projectStatus},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="totAmt != null">tot_amt = #{totAmt},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="orderBeginTime != null">order_begin_time = #{orderBeginTime},</if>
            <if test="orderEndTime != null">order_end_time = #{orderEndTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <!--    <delete id="deleteBmProjectById" parameterType="Long">
            delete from bm_project where id = #{id}
        </delete>-->

    <update id="deleteBmProjectById" parameterType="Long">
        update  bm_project set is_del = '1' where id = #{id}
    </update>

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

    <update id="deleteBmProjectByIds" parameterType="String">
        update bm_project set is_del = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


    <select id="selectStatisticsList" resultMap="BmProjectResult">
        <include refid="selectBmProjectVo"></include>
        <where>is_del = '0'
            <if test="deptId != -1">and dept_id = #{deptId}</if>
            and project_status in
            <foreach item="id" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
        </where>
        order by create_time DESC
    </select>

    <select id="selectProjectEharts" 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 d.dept_id = p.dept_id
	and d.status = '0' and p.project_status in (1,2,3,4) and p.is_del = '0'
	AND YEAR (p.create_time ) = #{year}
	where d.del_flag = '0'
    GROUP BY
	d.dept_id
	order by successMission DESC
    </select>


    <select id="selectProjectContractEharts" parameterType="string"
            resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
    SELECT
	d.dept_name AS deptName,
	ifnull( sum( p.tot_amt ), 0 ) AS totAmt,
	ifnull( count( p.id ), 0 ) AS successMission
    FROM
	sys_dept d
	LEFT JOIN bm_project p ON d.dept_id = p.dept_id
	AND p.project_status &gt; 4
	AND p.is_del = '0'
	AND YEAR ( p.create_time ) = #{year}
	where d.del_flag = '0'
    GROUP BY
	d.dept_id
	order by successMission DESC,
	totAmt DESC
    </select>


    <select id="selectBmProjectInProjectStatus" resultMap="BmProjectResult">
        <include refid="selectBmProjectVo"></include>
        where project_status = 1 and is_del = 0 group by dept_id
    </select>

    <select id="indexInfo" resultMap="BmProjectResult">
        select count(id) as num,project_status from bm_project
        <where>
            is_del = '0'
            <if test="deptId != null and deptId != -1">and dept_id = #{deptId}</if>
        </where>
        group by project_status
    </select>
</mapper>