<?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.yanzhu.jh.project.mapper.SurProjectStandardMapper">

    <resultMap type="SurProjectStandard" id="SurProjectStandardResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="imageFile"    column="image_file"    />
        <result property="standardDesc"    column="standard_desc"    />
        <result property="ord"    column="ord"    />
        <result property="standardType"    column="standard_type"    />
        <result property="standardTypeName"    column="standard_type_name"    />
        <result property="standardTypeRemark"    column="standard_type_remark"    />
        <result property="isDel"    column="is_del"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSurProjectStandardVo">
        select sps.id, sps.project_id, sps.dept_id, sd.dept_name, sps.image_file, sps.standard_desc, sps.ord, sps.standard_type, sdd.dict_label as standard_type_name, sdd.remark as standard_type_remark, sps.is_del, sps.create_by, sps.create_time, sps.update_by, sps.update_time from sur_project_standard sps
        left join sys_dict_data sdd on sps.standard_type = sdd.dict_value and sdd.dict_type='prj_standard_type'
        left join sys_dept sd on sd.dept_id = sps.dept_id
    </sql>

    <select id="selectSurProjectStandardList" parameterType="SurProjectStandard" resultMap="SurProjectStandardResult">
        <include refid="selectSurProjectStandardVo"/>
        <where>
            <if test="projectId != null "> and sps.project_id = #{projectId}</if>
            <if test="deptId != null "> and sps.dept_id = #{deptId}</if>
            <if test="imageFile != null  and imageFile != ''"> and sps.image_file = #{imageFile}</if>
            <if test="standardDesc != null  and standardDesc != ''"> and sps.standard_desc = #{standardDesc}</if>
            <if test="ord != null "> and sps.ord = #{ord}</if>
            <if test="standardType != null  and standardType != ''"> and sps.standard_type = #{standardType}</if>
            <if test="isDel != null "> and sps.is_del = #{isDel}</if>
        </where>
        order by sps.standard_type,sps.ord
    </select>

    <select id="selectSurProjectStandardGroupByType" parameterType="SurProjectStandard" resultType="map">
        select sps.standard_type as standardType, count(1) as total from sur_project_standard sps
        <where>
            <if test="projectId != null "> and sps.project_id = #{projectId}</if>
            <if test="deptId != null "> and sps.dept_id = #{deptId}</if>
            <if test="imageFile != null  and imageFile != ''"> and sps.image_file = #{imageFile}</if>
            <if test="standardDesc != null  and standardDesc != ''"> and sps.standard_desc = #{standardDesc}</if>
            <if test="ord != null "> and sps.ord = #{ord}</if>
            <if test="isDel != null "> and sps.is_del = #{isDel}</if>
        </where>
        group by sps.standard_type
    </select>

    <select id="selectSurProjectStandardById" parameterType="Long" resultMap="SurProjectStandardResult">
        <include refid="selectSurProjectStandardVo"/>
        where sps.id = #{id}
    </select>

    <insert id="insertSurProjectStandard" parameterType="SurProjectStandard" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_standard
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="imageFile != null">image_file,</if>
            <if test="standardDesc != null">standard_desc,</if>
            <if test="ord != null">ord,</if>
            <if test="standardType != null">standard_type,</if>
            <if test="isDel != null">is_del,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="imageFile != null">#{imageFile},</if>
            <if test="standardDesc != null">#{standardDesc},</if>
            <if test="ord != null">#{ord},</if>
            <if test="standardType != null">#{standardType},</if>
            <if test="isDel != null">#{isDel},</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>
        </trim>
    </insert>

    <update id="updateSurProjectStandard" parameterType="SurProjectStandard">
        update sur_project_standard
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="imageFile != null">image_file = #{imageFile},</if>
            <if test="standardDesc != null">standard_desc = #{standardDesc},</if>
            <if test="ord != null">ord = #{ord},</if>
            <if test="standardType != null">standard_type = #{standardType},</if>
            <if test="isDel != null">is_del = #{isDel},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurProjectStandardById" parameterType="Long">
        delete from sur_project_standard where id = #{id}
    </delete>

    <delete id="deleteSurProjectStandardByIds" parameterType="String">
        delete from sur_project_standard where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="groupByType" parameterType="SurProjectStandard" resultMap="com.ruoyi.system.mapper.SysDictDataMapper.SysDictDataResult">
        select b.dict_value,b.dict_label,a.cnt STATUS from (select * from sys_dict_data where  dict_type='prj_standard_type') b left join (
        select a.standard_type,count(1) cnt
        from sur_project_standard a
        where a.is_del =0
        <if test="projectId!=null and projectId>0"> and a.project_id=#{projectId}</if>
        <if test="deptId!=null and deptId>0">
            and   a.project_id in (SELECT id FROM sur_project WHERE deptid= #{deptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and a.project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        group by a.standard_type ) a on b.dict_value=a.standard_type
        order by b.dict_value
    </select>

    <select id="groupByProject"   resultMap="SurProjectStandardResult">
        select a.project_id,a.cnt dept_id,b.projectName standard_desc from (
           select a.project_id,count(1) cnt
           from sur_project_standard a where a.is_del=0
           group by a.project_id )a,
       sur_project b where a.project_id=b.id
        order by a.cnt desc

    </select>
    <select id="selectList"  parameterType="SurProjectStandard" resultMap="SurProjectStandardResult">
        <include refid="selectSurProjectStandardVo"/>
        where sps.is_del =0
        <if test="projectId!=null and projectId>0"> and sps.project_id=#{projectId}</if>
        <if test="deptId!=null and deptId>0">
            and  sps.project_id in (SELECT id FROM sur_project WHERE deptid= #{deptId})
        </if>
        <if test="prjIds !=null and prjIds.size()>0">
            and sps.project_id in
            <foreach collection="prjIds"  item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="standardType!=null and standardType>0"> and sps.standard_type=#{standardType}</if>
    </select>
</mapper>