<?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.zcxk.process.dao.ProcessDefinitionMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.process.entity.ProcessDefinition" id="ProcessDefinitionMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="processName" column="process_name" jdbcType="VARCHAR"/>
        <result property="processKey" column="process_key" jdbcType="VARCHAR"/>
        <result property="processDesc" column="process_desc" jdbcType="VARCHAR"/>
        <result property="tenantId" column="tenant_id" jdbcType="VARCHAR"/>
        <result property="taskType" column="task_type" jdbcType="INTEGER"/>
        <result property="companyOrgId" column="company_org_id" jdbcType="INTEGER"/>
        <result property="businessType" column="business_type" jdbcType="INTEGER"/>
        <result property="actDefId" column="act_def_id" jdbcType="VARCHAR"/>
        <result property="actModelId" column="act_model_id" jdbcType="VARCHAR"/>
        <result property="actDeployId" column="act_deploy_id" jdbcType="VARCHAR"/>
        <result property="version" column="version" jdbcType="INTEGER"/>
        <result property="runningVersion" column="running_version" jdbcType="INTEGER"/>
        <result property="processStatus" column="process_status" jdbcType="VARCHAR"/>
        <result property="isMain" column="is_main" jdbcType="VARCHAR"/>
        <result property="bpmnIcon" column="bpmn_icon" jdbcType="VARCHAR"/>
        <result property="bpmnContent" column="bpmn_content" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="dateCreate" column="date_create" jdbcType="TIMESTAMP"/>
        <result property="dateUpdate" column="date_update" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        id, process_name, process_key, process_desc, tenant_id, task_type, company_org_id, business_type, act_def_id, act_model_id, act_deploy_id, version, running_version, process_status, is_main, bpmn_icon, bpmn_content, status, create_by, update_by, date_create, date_update    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="ProcessDefinitionMap">
        select
        <include refid="Base_Column_List"/>
        from sc_process_definition
        where id = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="ProcessDefinitionMap">
        select
        <include refid="Base_Column_List"/>
        from sc_process_definition
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="ProcessDefinitionMap">
        select
        <include refid="Base_Column_List"/>
        from sc_process_definition
        <where>
             status=1
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="processName != null and processName != ''">
                and process_name = #{processName}
            </if>
            <if test="processKey != null and processKey != ''">
                and process_key = #{processKey}
            </if>
            <if test="processDesc != null and processDesc != ''">
                and process_desc = #{processDesc}
            </if>
            <if test="tenantId != null and tenantId != ''">
                and tenant_id = #{tenantId}
            </if>
            <if test="taskType != null">
                and task_type = #{taskType}
            </if>
            <if test="companyOrgId != null">
                and company_org_id = #{companyOrgId}
            </if>
            <if test="businessType != null">
                and business_type = #{businessType}
            </if>
            <if test="actDefId != null and actDefId != ''">
                and act_def_id = #{actDefId}
            </if>
            <if test="actModelId != null and actModelId != ''">
                and act_model_id = #{actModelId}
            </if>
            <if test="actDeployId != null and actDeployId != ''">
                and act_deploy_id = #{actDeployId}
            </if>
            <if test="version != null">
                and version = #{version}
            </if>
            <if test="runningVersion != null">
                and running_version = #{runningVersion}
            </if>
            <if test="processStatus != null and processStatus != ''">
                and process_status = #{processStatus}
            </if>
            <if test="isMain != null and isMain != ''">
                and is_main = #{isMain}
            </if>
            <if test="bpmnIcon != null and bpmnIcon != ''">
                and bpmn_icon = #{bpmnIcon}
            </if>
            <if test="bpmnContent != null and bpmnContent != ''">
                and bpmn_content = #{bpmnContent}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by = #{createBy}
            </if>
            <if test="updateBy != null and updateBy != ''">
                and update_by = #{updateBy}
            </if>
            <if test="dateCreate != null">
                and date_create = #{dateCreate}
            </if>
            <if test="dateUpdate != null">
                and date_update = #{dateUpdate}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into sc_process_definition(id, process_name, process_key, process_desc, tenant_id, task_type,
                                          company_org_id, business_type, act_def_id, act_model_id, act_deploy_id,
                                          version, running_version, process_status, is_main, bpmn_icon, bpmn_content,
                                          status, create_by, update_by, date_create, date_update)
        values (#{id}, #{processName}, #{processKey}, #{processDesc}, #{tenantId}, #{taskType}, #{companyOrgId},
                #{businessType}, #{actDefId}, #{actModelId}, #{actDeployId}, #{version}, #{runningVersion},
                #{processStatus}, #{isMain}, #{bpmnIcon}, #{bpmnContent}, #{status}, #{createBy}, #{updateBy},
                #{dateCreate}, #{dateUpdate})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into sc_process_definition(id, process_name, process_key, process_desc, tenant_id, task_type,
        company_org_id, business_type, act_def_id, act_model_id, act_deploy_id, version, running_version,
        process_status, is_main, bpmn_icon, bpmn_content, status, create_by, update_by, date_create, date_update)
        values
        <foreach collection="processDefinitions" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.processName}, #{item.processKey}, #{item.processDesc}, #{item.tenantId},
            #{item.taskType}, #{item.companyOrgId}, #{item.businessType}, #{item.actDefId}, #{item.actModelId},
            #{item.actDeployId}, #{item.version}, #{item.runningVersion}, #{item.processStatus}, #{item.isMain},
            #{item.bpmnIcon}, #{item.bpmnContent}, #{item.status}, #{item.createBy}, #{item.updateBy},
            #{item.dateCreate}, #{item.dateUpdate} )
        </foreach>
    </insert>
    <insert id="insertSelective">

    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update sc_process_definition
        <set>
            <if test="processName != null  ">
                process_name = #{processName},
            </if>
            <if test="processKey != null  ">
                process_key = #{processKey},
            </if>
            <if test="processDesc != null  ">
                process_desc = #{processDesc},
            </if>
            <if test="tenantId != null and tenantId != ''">
                tenant_id = #{tenantId},
            </if>
            <if test="taskType != null">
                task_type = #{taskType},
            </if>
            <if test="companyOrgId != null">
                company_org_id = #{companyOrgId},
            </if>
            <if test="businessType != null">
                business_type = #{businessType},
            </if>
            <if test="actDefId != null and actDefId != ''">
                act_def_id = #{actDefId},
            </if>
            <if test="actModelId != null and actModelId != ''">
                act_model_id = #{actModelId},
            </if>
            <if test="actDeployId != null and actDeployId != ''">
                act_deploy_id = #{actDeployId},
            </if>
            <if test="version != null">
                version = #{version},
            </if>
            <if test="runningVersion != null">
                running_version = #{runningVersion},
            </if>
            <if test="processStatus != null and processStatus != ''">
                process_status = #{processStatus},
            </if>
            <if test="isMain != null and isMain != ''">
                is_main = #{isMain},
            </if>
            <if test="bpmnIcon != null and bpmnIcon != ''">
                bpmn_icon = #{bpmnIcon},
            </if>
            <if test="bpmnContent != null and bpmnContent != ''">
                bpmn_content = #{bpmnContent},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="createBy != null and createBy != ''">
                create_by = #{createBy},
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
            <if test="dateCreate != null">
                date_create = #{dateCreate},
            </if>
            <if test="dateUpdate != null">
                date_update = #{dateUpdate},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from sc_process_definition
        where id = #{id}
    </delete>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*)
        from sc_process_definition
    </select>
    <select id="selectPage" resultMap="ProcessDefinitionMap">
        select
        <include refid="Base_Column_List"/>
        from sc_process_definition
        <where>
            status =1
            <if test="processDefinition.programItems !=null">
               and company_org_id in
                <foreach collection="processDefinition.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="processDefinition.id != null">
                and id = #{processDefinition.id}
            </if>
            <if test="processDefinition.processName != null and processDefinition.processName != ''">
                and process_name like CONCAT('%',#{processDefinition.processName},'%')
            </if>
            <if test="processDefinition.processKey != null and processDefinition.processKey != ''">
                and process_key = #{processDefinition.processKey}
            </if>
            <if test="processDefinition.processDesc != null and processDefinition.processDesc != ''">
                and process_desc = #{processDefinition.processDesc}
            </if>
            <if test="processDefinition.tenantId != null and processDefinition.tenantId != ''">
                and tenant_id = #{processDefinition.tenantId}
            </if>
            <if test="processDefinition.taskType != null">
                and task_type = #{processDefinition.taskType}
            </if>
            <if test="processDefinition.companyOrgId != null">
                and company_org_id = #{processDefinition.companyOrgId}
            </if>
            <if test="processDefinition.businessType != null">
                and business_type = #{processDefinition.businessType}
            </if>
            <if test="processDefinition.actDefId != null and processDefinition.actDefId != ''">
                and act_def_id = #{processDefinition.actDefId}
            </if>
            <if test="processDefinition.actModelId != null and processDefinition.actModelId != ''">
                and act_model_id = #{processDefinition.actModelId}
            </if>
            <if test="processDefinition.actDeployId != null and processDefinition.actDeployId != ''">
                and act_deploy_id = #{processDefinition.actDeployId}
            </if>
            <if test="processDefinition.version != null">
                and version = #{processDefinition.version}
            </if>
            <if test="processDefinition.runningVersion != null">
                and running_version = #{processDefinition.runningVersion}
            </if>
            <if test="processDefinition.processStatus != null and processDefinition.processStatus != ''">
                and process_status = #{processDefinition.processStatus}
            </if>
            <if test="processDefinition.isMain != null and processDefinition.isMain != ''">
                and is_main = #{processDefinition.isMain}
            </if>
            <if test="processDefinition.bpmnIcon != null and processDefinition.bpmnIcon != ''">
                and bpmn_icon = #{processDefinition.bpmnIcon}
            </if>
            <if test="processDefinition.bpmnContent != null and processDefinition.bpmnContent != ''">
                and bpmn_content = #{processDefinition.bpmnContent}
            </if>
            <if test="processDefinition.status != null">
                and status = #{processDefinition.status}
            </if>
            <if test="processDefinition.createBy != null and processDefinition.createBy != ''">
                and create_by = #{processDefinition.createBy}
            </if>
            <if test="processDefinition.updateBy != null and processDefinition.updateBy != ''">
                and update_by = #{processDefinition.updateBy}
            </if>
            <if test="processDefinition.dateCreate != null">
                and date_create = #{processDefinition.dateCreate}
            </if>
            <if test="processDefinition.dateUpdate != null">
                and date_update = #{processDefinition.dateUpdate}
            </if>
        </where>
    </select>
    <select id="checkProcessUnique" resultType="java.lang.Integer">
        select count(*) from sc_process_definition
        <where>
          status=1
            <if test="tenantId != null and tenantId != ''">
                and tenant_id = #{tenantId}
            </if>
            <if test="taskType != null">
                and task_type = #{taskType}
            </if>
            <if test="companyOrgId != null">
                and company_org_id = #{companyOrgId}
            </if>
        </where>
    </select>
    <select id="getBpmnContent" resultType="java.lang.String">
        select bpmn_content from sc_process_definition where status = 1 and id = #{id}
    </select>
</mapper>