<?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.task.mapper.TmTaskMapper">
    <resultMap type="com.bonus.material.task.domain.TmTask" id="TmTaskResult">
        <result property="taskId"    column="task_id"    />
        <result property="preTaskId"    column="pre_task_id"    />
        <result property="taskType"    column="task_type"    />
        <result property="taskStatus"    column="task_status"    />
        <result property="code"    column="code"    />
        <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="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="monthOrder"    column="month_order"    />
        <result property="splitType"    column="split_type"    />
    </resultMap>

    <sql id="selectTmTaskVo">
        select task_id, pre_task_id, task_type, task_status, code, create_by, create_time,
               update_by, update_time, remark, company_id, month_order
        from tm_task
    </sql>


    <select id="selectTmTaskList" parameterType="com.bonus.material.task.domain.TmTask" resultMap="TmTaskResult">
        <include refid="selectTmTaskVo"/>
        <where>  
            <if test="taskType != null "> and task_type = #{taskType}</if>
            <if test="taskStatus != null "> and task_status = #{taskStatus}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="monthOrder != null  and code != ''"> and month_order = #{monthOrder}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
        </where>
    </select>
    
    <select id="selectTmTaskByTaskId" parameterType="Long" resultMap="TmTaskResult">
        <include refid="selectTmTaskVo"/>
        where task_id = #{taskId}
    </select>

    <!-- 新增：批量查询任务 -->
    <select id="selectTmTaskByTaskIdsBatch" resultMap="TmTaskResult">
        <include refid="selectTmTaskVo"/>
        where task_id in
        <foreach item="taskId" collection="list" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </select>
        
    <insert id="insertTmTask" parameterType="com.bonus.material.task.domain.TmTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into tm_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="preTaskId != null">pre_task_id,</if>
            <if test="taskType != null">task_type,</if>
            <if test="taskDeptId != null">task_dept_id,</if>
            <if test="splitType != null">split_type,</if>
            <if test="taskStatus != null">task_status,</if>
            <if test="code != null">`code`,</if>
            <if test="monthOrder != null">month_order,</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="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="preTaskId != null">#{preTaskId},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="taskDeptId != null">#{taskDeptId},</if>
            <if test="splitType != null">#{splitType},</if>
            <if test="taskStatus != null">#{taskStatus},</if>
            <if test="code != null">#{code},</if>
            <if test="monthOrder != null">#{monthOrder},</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="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <update id="updateTmTask" parameterType="com.bonus.material.task.domain.TmTask">
        update tm_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="taskStatus != null">task_status = #{taskStatus},</if>
            <if test="code != null">code = #{code},</if>
            <if test="monthOrder != null">month_order = #{monthOrder},</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="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            update_time = NOW()
        </trim>
        where task_id = #{taskId}
    </update>

    <update id="updateTaskStatus">
        update tm_task
        set task_status = #{newStatus},
            update_time = NOW()
        where task_id = #{taskId}
    </update>

    <delete id="deleteTmTaskByTaskId" parameterType="Long">
        delete from tm_task where task_id = #{taskId}
    </delete>

    <delete id="deleteTmTaskByTaskIds" parameterType="String">
        delete from tm_task where task_id in 
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

    <select id="getMonthMaxOrderByDate" resultType="java.lang.Integer">
        select COALESCE(max(month_order), 0) from tm_task
        where
            month(create_time) = #{month} and year(create_time) = #{year}
            <if test="taskType != null and taskType !=''">
                and task_type = #{taskType}
            </if>
    </select>

    <delete id="deleteTmTaskByPurchaseIds" parameterType="String">
        delete from tm_task where task_id in
        (
            select task_id from purchase_check_info where id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        )
    </delete>

    <delete id="deleteTaskByIds">
        delete from tm_task where task_id in
        <foreach item="taskId" collection="taskIds" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

    <delete id="deleteAgreementId">
        delete from tm_task_agreement where task_id in
        <foreach item="taskId" collection="taskIds" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>

    <select id="selectTaskNumByMonths" resultType="java.lang.String">
        SELECT SUBSTRING(`code`, - 3) as code
        FROM tm_task
        WHERE DATE_FORMAT(create_time, '%y%m') = DATE_FORMAT(#{date}, '%y%m')
        AND task_type = #{taskType}
        ORDER BY create_time DESC LIMIT 1
    </select>

    <select id="getTaskList" resultType="com.bonus.material.task.domain.TmTask">
        select tt.task_id as taskId
        from tm_task_agreement tta
                 left join tm_task tt on tta.task_id = tt.task_id
        where tta.agreement_id = #{bean.agreementId} and tt.task_type = #{taskType}
    </select>

    <select id="getTaskIdList" resultType="com.bonus.material.task.domain.TmTask">
        select tta.task_id as taskId
        from tm_task_agreement tta
        where tta.agreement_id = #{agreementId}
    </select>

    <select id="selectTaskById" resultType="com.bonus.material.task.domain.TmTask">
        SELECT
        creator as createBy,
        create_time as createTime
        FROM
        slt_agreement_apply
        WHERE
        1 =1
        <if test="agreementId != null">
            and agreement_id = #{agreementId}
        </if>
        <if test="settlementType != null and settlementType != 0">
            and settlement_type = #{settlementType}
        </if>
    </select>

    <select id="selectTaskByIdByCl" resultType="com.bonus.material.task.domain.TmTask">
        SELECT
            creator as createBy,
            create_time as createTime
        FROM
            clz_slt_agreement_apply
        WHERE
        1 =1
        <if test="agreementId != null">
            and agreement_id = #{agreementId}
        </if>
    </select>

    <select id="selectBmUnitById" resultType="com.bonus.material.basic.domain.BmUnit">
        SELECT
            unit_id as unitId,
            type_id as typeId,
            unit_name as unitName
        FROM
            bm_unit
        WHERE
            unit_id = #{leaseUnitId}
    </select>

    <select id="selectTmTaskByPreTaskId" parameterType="Long" resultMap="TmTaskResult">
        <include refid="selectTmTaskVo"/>
        where task_type = '22' and pre_task_id = #{taskId}
    </select>

    <select id="selectBmUnitInfo" resultType="com.bonus.material.basic.domain.BmUnit">
        SELECT
            unit_id as unitId,
            type_id as typeId,
            unit_name as unitName
        FROM
            bm_unit
        WHERE
            unit_id = #{unitId}
    </select>

    <select id="selectLeaseApplyInfoByTaskNo" resultType="com.bonus.common.biz.domain.lease.LeaseApplyInfo">
        SELECT
            task_id as taskId,
            code as code
        FROM
            tm_task
        WHERE
            code = #{code}
    </select>
    <select id="selectStatusByTaskId" resultType="com.bonus.material.task.domain.TmTask">
        SELECT
            task_status as taskStatus,
            code as code
        FROM
            tm_task
        WHERE
            task_id = #{taskId}
    </select>

    <select id="selectTaskByCode" resultType="com.bonus.material.task.domain.TmTask">
        SELECT
            task_id as taskId,
            task_type as taskType,
            task_status as taskStatus,
            code as code
        FROM
            tm_task
        WHERE
            code = #{code}
    </select>

    <select id="selectBackApplyInfoById" resultType="com.bonus.material.repair.domain.RepairApplyDetails">
        SELECT
            task_id as taskId,
            back_id as backId
        FROM
            repair_apply_details
        WHERE
            back_id = #{id}
    </select>
</mapper>