<?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.ibs.task.mapper.MarketTaskListMapper">

    <insert id="batchInsertMarketTaskList">
        insert into market_task_list
            (market_task_id, cust_type, cust_id, cust_name, user_name, nick_name, dept_id, status, remark, is_reed, is_overdue, import_status, failure_reason)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.marketTaskId}, #{item.custType}, #{item.custId}, #{item.custName}, #{item.userName}, #{item.nickName}, #{item.deptId}, #{item.status}, #{item.remark}, '0', '0', #{item.importStatus}, #{item.failureReason})
        </foreach>
    </insert>

    <update id="updateReadTime">
        update market_task_list
        set is_reed = '1',
            update_by = #{username},
            update_time = sysdate()
        where id = #{id}
    </update>

    <select id="selectMarketTaskLists" resultType="MarketTaskListVO">
        select
            mtl.id,
            mtl.market_task_id,
            mtl.cust_type,
            mtl.cust_id,
            mtl.cust_name,
            mtl.user_name,
            mtl.nick_name,
            mtl.dept_id,
            mtl.status,
            mtl.remark,
            mtl.is_reed,
            mtl.is_overdue,
            mtl.import_status,
            mtl.failure_reason,
            mt.market_name as marketTaskName,
            mt.end_time as endTime,
            mt.is_feedback as isFeedback,
            d.dept_name as deptName
        from market_task_list mtl
        left join market_task mt on mtl.market_task_id = mt.id
        left join sys_dept d on d.dept_id = mtl.dept_id
        <where>
            import_status = '1'
            <if test="username != null and username != ''">
                and mtl.user_name = #{username}
            </if>
            <if test="deptId != null and deptId != ''">
                and mtl.dept_id = #{deptId}
            </if>
            <if test="dto.custInfo != null and dto.custInfo != ''">
                and (mtl.cust_id like concat('%', #{dto.custInfo}, '%')
                or mtl.cust_name like concat('%', #{dto.custInfo}, '%'))
            </if>
            <if test="dto.status != null and dto.status != ''">
                and mtl.status = #{dto.status}
            </if>
            <if test="dto.isOverdue != null and dto.isOverdue != ''">
                and mtl.is_overdue = #{dto.isOverdue}
            </if>
        </where>
    </select>

    <select id="selectAllMarketTaskLists" resultType="MarketTaskListVO">
        select
            mtl.id,
            mtl.market_task_id,
            mtl.cust_type,
            mtl.cust_id,
            mtl.cust_name,
            mtl.user_name,
            mtl.nick_name,
            mtl.dept_id,
            mtl.status,
            mtl.remark,
            mtl.is_reed,
            mtl.is_overdue,
            mtl.import_status,
            mtl.failure_reason,
            mt.market_name as marketTaskName,
            mt.end_time as endTime,
            mt.is_feedback as isFeedback,
            d.dept_name as deptName
        from market_task_list mtl
        left join sys_dept d on d.dept_id = mtl.dept_id
        left join market_task mt on mtl.market_task_id = mt.id
        <where>
            <if test="dto.marketTaskId != null and dto.marketTaskId != ''">
                and mtl.market_task_id = #{dto.marketTaskId}
            </if>
            <if test="dto.importStatus != null and dto.importStatus != ''">
                and mtl.import_status = #{dto.importStatus}
            </if>
            <if test="role != null and role == 'outlet'"> and (mtl.dept_id = #{deptId} or mtl.dept_id is null) </if>
            <if test="role != null and role == 'branch'"> and (mtl.dept_id = #{deptId} or find_in_set(#{deptId},d.ancestors) or mtl.dept_id is null) </if>
            <if test="role != null and role in {'head', 'ops', 'public', 'private'}">  and (left( d.dept_id,3) = left(#{deptId},3) or mtl.dept_id is null) </if>
            <if test="dto.custInfo != null and dto.custInfo != ''">
                and (mtl.cust_id like concat('%', #{dto.custInfo}, '%')
                or mtl.cust_name like concat('%', #{dto.custInfo}, '%'))
            </if>
            <if test="dto.userInfo != null and dto.userInfo != ''">
                and (mtl.user_name like concat('%', #{dto.userInfo}, '%')
                or mtl.nick_name like concat('%', #{dto.userInfo}, '%'))
            </if>
            <if test="dto.status != null and dto.status != ''">
                and mtl.status = #{dto.status}
            </if>
            <if test="dto.isOverdue != null and dto.isOverdue != ''">
                and mtl.is_overdue = #{dto.isOverdue}
            </if>
        </where>
    </select>

</mapper>