<?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.bonus.material.countersign.mapper.SignProcessMapper">

    <select id="getListNew" resultType="com.bonus.material.countersign.domain.SignProcessVo">
        select
        sc.id as id,sc.id as idStr,sc.process_id as processId,sc.sign_type as signType,sc.org_id as orgIdStr,
        sd.dept_name as orgName,sdd.dict_label as processName,sdd2.dict_label as signTypeName
        from sign_config sc
        left join sys_dict_data sdd on sc.process_id = sdd.dict_value
        left join sys_dict_data sdd2 on sc.sign_type = sdd2.dict_value
        left join sys_dept sd on sc.org_id = sd.dept_id and sd.del_flag = 0
        where
        sc.del_flag = 0
        and sdd.dict_type = 'countersign_process_config_name' and sdd.status = 0
        and sdd2.dict_type = 'countersign_process_type_name' and sdd2.status = 0
        <if test="keyWord != null and keyWord != ''">
            and (sdd.dict_label like concat('%', #{keyWord}, '%') or sdd2.dict_label like concat('%', #{keyWord}, '%')
            or sd.dept_name like concat('%', #{keyWord}, '%'))
        </if>
        AND sd.dept_name NOT LIKE '%库管%'
        union
        SELECT
        sc.id AS id,
        GROUP_CONCAT(sc.id) AS idStr,
        sc.process_id AS processId,
        sc.sign_type AS signType,
        GROUP_CONCAT(sc.org_id) AS orgIdStr,
        GROUP_CONCAT(sd.dept_name) AS orgName,
        sdd.dict_label AS processName,
        sdd2.dict_label AS signTypeName
        FROM
        sign_config sc
        LEFT JOIN sys_dict_data sdd ON sc.process_id = sdd.dict_value
        LEFT JOIN sys_dict_data sdd2 ON sc.sign_type = sdd2.dict_value
        LEFT JOIN sys_dept sd ON sc.org_id = sd.dept_id AND sd.del_flag = 0
        WHERE
        sc.del_flag = 0
        AND sdd.dict_type = 'countersign_process_config_name'
        AND sdd.status = 0
        AND sdd2.dict_type = 'countersign_process_type_name'
        AND sdd2.status = 0
        <if test="keyWord != null and keyWord != ''">
            and (sdd.dict_label like concat('%', #{keyWord}, '%') or sdd2.dict_label like concat('%', #{keyWord}, '%')
            or sd.dept_name like concat('%', #{keyWord}, '%'))
        </if>
        AND sd.dept_name LIKE '%库管%'
        GROUP BY
        sc.process_id,
        sc.sign_type,
        sdd.dict_label,
        sdd2.dict_label
    </select>

    <select id="getList"  resultType="com.bonus.material.countersign.domain.SignProcessVo">
        select
        sc.id as id,sc.process_id as processId,sc.sign_type as signType,sc.org_id as orgId,
        sd.dept_name as orgName,sdd.dict_label as processName,sdd2.dict_label as signTypeName
        from sign_config sc
        left join sys_dict_data sdd on sc.process_id = sdd.dict_value
        left join sys_dict_data sdd2 on sc.sign_type = sdd2.dict_value
        left join sys_dept sd on sc.org_id = sd.dept_id and sd.del_flag = 0
        <where>
            sc.del_flag = 0
            and sdd.dict_type = 'countersign_process_config_name' and sdd.status = 0
            and sdd2.dict_type = 'countersign_process_type_name' and sdd2.status = 0
            <if test="keyWord != null and keyWord != ''">
                and (sdd.dict_label like concat('%', #{keyWord}, '%') or sdd2.dict_label like concat('%', #{keyWord}, '%')
                or sd.dept_name like concat('%', #{keyWord}, '%'))
            </if>
        </where>
    </select>

    <select id="selectProcessRepeat"  resultType="int">
        select
        count(id)
        from sign_config sc
        <where>
            sc.del_flag = 0
            <if test="processId != null">
                and sc.process_id = #{processId}
            </if>
            <if test="signType != null">
                and sc.sign_type = #{signType}
            </if>
            <if test="orgId != null">
                and sc.org_id = #{orgId}
            </if>
        </where>
    </select>

    <insert id="insertProcessInfo" parameterType="com.bonus.material.countersign.domain.SignProcessVo">
        insert into sign_config(
        <if test="processId != null">process_id,</if>
        <if test="signType != null">sign_type,</if>
        <if test="orgId != null">org_id,</if>
        del_flag
        )values(
        <if test="processId != null">#{processId},</if>
        <if test="signType != null">#{signType},</if>
        <if test="orgId != null">#{orgId},</if>
        0
        )
    </insert>

    <select id="selectProcessInfoById" resultType="com.bonus.material.countersign.domain.SignProcessVo">
        SELECT
            sc.id AS id,
            GROUP_CONCAT(sc.id) AS idStr,
            sc.process_id AS processId,
            sc.sign_type AS signType,
            GROUP_CONCAT(sc.org_id) AS orgIdStr
        FROM
            sign_config sc
        WHERE
            sc.del_flag = 0
        AND sc.id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY sc.process_id,sc.sign_type
    </select>

    <select id="selectProcessRepeatEdit"  resultType="int">
        select
        count(id)
        from sign_config sc
        <where>
            sc.del_flag = 0
            <if test="id != null">
                and sc.id != #{id}
            </if>
            <if test="processId != null">
                and sc.process_id = #{processId}
            </if>
            <if test="signType != null">
                and sc.sign_type = #{signType}
            </if>
            <if test="orgId != null">
                and sc.org_id = #{orgId}
            </if>
        </where>
    </select>

    <update id="updateProcessInfo" parameterType="com.bonus.material.countersign.domain.SignProcessVo">
        update sign_config
        <set>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="signType != null">sign_type = #{signType},</if>
            <if test="orgId != null">org_id = #{orgId}</if>
        </set>
        where id = #{id} and del_flag = 0
    </update>

    <update id="delProcess" parameterType="Long">
        update sign_config
        set del_flag = 1
        where id = #{id} and del_flag = 0
    </update>

    <select id="getPurchaseSignRecordByTask" resultType="com.bonus.material.purchase.domain.PurchaseSignRecord">
        SELECT
            par.id,
            par.task_id AS taskId,
            par.auditor AS userId,
            par.org_id AS orgId,
            par.audit_status AS auditStatus,
            par.create_time AS createTime,
            su.nick_name AS userName,
            sd.dept_name AS deptName,
            par.audit_remark AS auditRemark
        FROM
            purchase_audit_record par
            LEFT JOIN sys_user su ON par.auditor = su.user_id
            LEFT JOIN sys_dept sd ON par.org_id = sd.dept_id
        WHERE
            par.task_id = #{taskId}
    </select>

    <!-- 新增：批量查询采购任务的会签记录 -->
    <select id="getPurchaseSignRecordByTasksBatch" resultType="com.bonus.material.purchase.domain.PurchaseSignRecord">
        SELECT
            par.id,
            par.task_id AS taskId,
            par.auditor AS userId,
            par.org_id AS orgId,
            par.audit_status AS auditStatus,
            par.create_time AS createTime,
            su.nick_name AS userName,
            sd.dept_name AS deptName,
            par.audit_remark AS auditRemark
        FROM
            purchase_audit_record par
            LEFT JOIN sys_user su ON par.auditor = su.user_id
            LEFT JOIN sys_dept sd ON par.org_id = sd.dept_id
        WHERE
            par.task_id in
            <foreach item="taskId" collection="list" open="(" separator="," close=")">
                #{taskId}
            </foreach>
    </select>

    <insert id="insertPurchaseSignRecord">
        insert into purchase_audit_record(task_id, auditor, audit_time, audit_status, org_id, creator, create_time, audit_remark)
        values(#{taskId}, #{userId}, #{createTime}, #{auditStatus} ,#{orgId}, #{userId}, #{createTime}, #{auditRemark})
    </insert>

    <select id="getPurchaseSignUrlListByTaskId" resultType="com.bonus.material.purchase.domain.PurchaseSignRecord">
        SELECT par.id, par.task_id as taskId, par.org_id as orgId, par.auditor as userId,
               su.sign_url as signUrl,su.sign_type as signType
        FROM purchase_audit_record par
        LEFT JOIN sys_user su ON su.user_id = par.auditor
        WHERE par.audit_status = '1' and par.task_id = #{taskId}
    </select>
</mapper>