<?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.sgzb.material.mapper.TaskMapper">
    <resultMap type="com.bonus.sgzb.material.domain.TmTask" id="TmTaskResult">
        <result property="taskId"    column="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"    />
    </resultMap>


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

    <select id="selectTmTaskList" parameterType="com.bonus.sgzb.material.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="companyId != null "> and company_id = #{companyId}</if>
        </where>
    </select>
    
    <select id="selectTmTaskByTaskId" parameterType="Long" resultMap="TmTaskResult">
        <include refid="selectTmTaskVo"/>
        where task_id = #{taskId}
    </select>
        
    <insert id="insertTmTask" parameterType="com.bonus.sgzb.material.domain.TmTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into tm_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskType != null">task_type,</if>
            <if test="taskStatus != null">task_status,</if>
            <if test="code != null">code,</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="taskType != null">#{taskType},</if>
            <if test="taskStatus != null">#{taskStatus},</if>
            <if test="code != null">#{code},</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>
    <insert id="insertSelective" keyColumn="task_id" keyProperty="id" parameterType="com.bonus.sgzb.material.domain.TmTask" useGeneratedKeys="true">
        insert into tm_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskType != null">
                task_type,
            </if>
            <if test="taskStatus != null">
                task_status,
            </if>
            <if test="code != null and code != ''">
                code,
            </if>
            <if test="createBy != null and createBy != ''">
                create_by,
            </if>
            create_time,
            <if test="updateBy != null and updateBy != ''">
                update_by,
            </if>
            update_time,
            <if test="remark != null and remark != ''">
                remark,
            </if>
            <if test="companyId != null">
                company_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskType != null">
                #{taskType,jdbcType=INTEGER},
            </if>
            <if test="taskStatus != null">
                #{taskStatus,jdbcType=INTEGER},
            </if>
            <if test="code != null and code != ''">
                #{code,jdbcType=VARCHAR},
            </if>
            <if test="createBy != null and createBy != ''">
                #{createBy,jdbcType=VARCHAR},
            </if>
            now(),
            <if test="updateBy != null and updateBy != ''">
                #{updateBy,jdbcType=VARCHAR},
            </if>
            now(),
            <if test="remark != null and remark != ''">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="companyId != null">
                #{companyId,jdbcType=INTEGER},
            </if>
        </trim>
    </insert>
    <insert id="insertAgreement">
        INSERT INTO tm_task_agreement ( `task_id`, `agreement_id`, `create_by`, `create_time`, `company_id` )
        VALUES(#{id},#{agreementId},#{createBy},NOW(),#{companyId})
    </insert>
    <insert id="insertTmTaskByBackInfo" keyColumn="task_id" keyProperty="taskId" parameterType="com.bonus.sgzb.base.api.domain.BackApplyInfo" useGeneratedKeys="true">
        insert into tm_task (
        <if test="taskType != null">task_type, </if>
        <if test="taskStatus != null">task_status, </if>
        <if test="code != null and code != ''">code, </if>
        <if test="createBy != null and createBy != ''">create_by, </if>
        <if test="updateBy != null and updateBy != ''">update_by, </if>
        <if test="updateTime != null">update_time, </if>
        <if test="remark != null and remark != ''">remark, </if>
        <if test="companyId != null">company_id, </if>
        create_time
        ) values (
        <if test="taskType != null">#{taskType}, </if>
        <if test="taskStatus != null">#{taskStatus}, </if>
        <if test="code != null and code != ''">#{code}, </if>
        <if test="createBy != null and createBy != ''">#{createBy}, </if>
        <if test="updateBy != null and updateBy != ''">#{updateBy}, </if>
        <if test="updateTime != null">#{updateTime}, </if>
        <if test="remark != null and remark != ''">#{remark}, </if>
        <if test="companyId != null">#{companyId}, </if>
        NOW()
        )
    </insert>
    <insert id="insertAgreementByBackInfo">
        insert into tm_task_agreement
        (
        <if test="taskId != null">
            task_id,
        </if>
        <if test="agreementId != null">
            agreement_id,
        </if>
        <if test="createBy != null and createBy != ''">
            create_by,
        </if>
        <if test="updateBy != null and updateBy != ''">
            update_by,
        </if>
        <if test="updateTime != null">
            update_time,
        </if>
        <if test="remark != null and remark != ''">
            remark,
        </if>
        <if test="companyId != null">
            company_id,
        </if>
        create_time
        ) values (
        <if test="taskId != null">
            #{taskId},
        </if>
        <if test="agreementId != null">
            #{agreementId},
        </if>
        <if test="createBy != null and createBy != ''">
            #{createBy},
        </if>
        <if test="updateBy != null and updateBy != ''">
            #{updateBy},
        </if>
        <if test="updateTime != null">
            #{updateTime},
        </if>
        <if test="remark != null and remark != ''">
            #{remark},
        </if>
        <if test="companyId != null">
            #{companyId},
        </if>
        NOW()
        )

    </insert>

    <update id="updateTmTask" parameterType="com.bonus.sgzb.material.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="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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="disposition != null">disposition = #{disposition},</if>
            <if test="fileUrl != null">file_url = #{fileUrl},</if>
            <if test="fileName != null">file_name = #{fileName},</if>
        </trim>
        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="selectTaskNumByMonth" resultType="java.lang.Integer">
        select count(*) from tm_task where DATE_FORMAT(create_time,'%y%m') = DATE_FORMAT(#{date},'%y%m') and task_type = #{taskType}
    </select>
    <select id="getMaTypeDetails" resultType="com.bonus.sgzb.material.domain.TmTask">
        SELECT
            mws2.type_id AS typeId,
            mws2.part_num AS partNum
        FROM
            ma_type mt1
                LEFT JOIN ma_whole_set mws2 ON mt1.type_id = mws2.parent_id
        WHERE
            mt1.type_id = #{typeId}
    </select>
    <select id="getMachineParts" resultType="com.bonus.sgzb.base.api.domain.MachinePart">
        SELECT
            mt.type_name AS typeModelName,
            mt.unit_name as unitName,
            m2.type_name As typeName
        FROM
            ma_type mt
                LEFT JOIN ma_type m2 ON mt.parent_id = m2.type_id
        WHERE
            mt.type_id = #{typeId}
    </select>
    <select id="selectTaskNumByMonths" resultType="java.lang.String">
        SELECT SUBSTRING(`code`, - 4) 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="selectTmTaskListByDisposition" resultType="com.bonus.sgzb.material.vo.ScrapApplyDetailsVO">

        SELECT
        tk.CODE scrapNum,
        tk.task_status taskStatus,
        dic.NAME taskStatusName,
        bui.unit_name unitName,
        bpl.lot_name projectName,
        su.nick_name createBy,
        tk.create_time createTime,
        tk.remark,
        tk1.CODE repairNum,
        sad.scrap_num as scrapNum2,
        sad.scrap_source as scrapSource,
        sad.file_url as fileUrl,
        tk.task_id taskId,
        tk.disposition disposition,
        tk.file_url dispositionFileUrl,
        tk.file_name dispositionFileName
        FROM
        tm_task tk
        LEFT JOIN tm_task_agreement tta ON tk.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bpl ON bai.project_id = bpl.lot_id
        LEFT JOIN bm_unit_info bui ON bai.unit_id = bui.unit_id
        LEFT JOIN scrap_apply_details sad ON tk.task_id = sad.task_id
        LEFT JOIN scrap_apply_details sad1 ON sad.parent_id = sad1.id
        LEFT JOIN tm_task tk1 ON sad1.task_id = tk1.task_id
        LEFT JOIN (
        SELECT
        id,
        p_id,
        NAME
        FROM
        sys_dic
        WHERE
        p_id IN ( SELECT id FROM sys_dic WHERE VALUE = 'bf_task' )) dic ON tk.task_status = dic.id
        LEFT JOIN ma_type mt ON sad.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        LEFT JOIN sys_user su ON su.user_id = tk.create_by
        WHERE
        tk.task_type = '57' and tk.task_status = '59'
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, tk.create_by) > 0
            or locate(#{keyword}, tk.CODE) > 0
            or locate(#{keyword}, bui.unit_name) > 0
            or locate(#{keyword}, bpl.lot_name) > 0
            or locate(#{keyword}, tk1.CODE) > 0)
        </if>
        <if test="backUnit != null and backUnit != ''">
            and bui.unit_id = #{backUnit}
        </if>
        <if test="repairStatus != null and repairStatus != ''">
            and tk.task_status = #{repairStatus}
        </if>
        <if test="backPro != null and backPro != ''">
            and bpl.lot_id = #{backPro}
        </if>
        <if test="type != null and type != ''">
            and mt1.type_id = #{type}
        </if>
        <if test="backCode != null and backCode != ''">
            and locate(#{backCode}, tk.code) > 0
        </if>
        <if test="params.beginTime != null and params.beginTime != '' and params.endTime != null and params.endTime != ''">
            and tk.create_time between #{params.beginTime} and #{params.endTime}
        </if>
        GROUP BY tk.CODE
        order by tk.create_time desc
    </select>
</mapper>