<?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="cn.iocoder.yudao.module.tickets.mapper.DlRepairTitemMapper">
       <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.tickets.vo.DlRepairTitemRespVO">
              <id property="id" column="id" jdbcType="VARCHAR"/>
              <result column="ticket_id" property="ticketId"/>
              <result column="item_name" property="itemName"/>
              <result column="item_count" property="itemCount"/>
              <result column="item_unit" property="itemUnit"/>
              <result column="item_price" property="itemPrice"/>
              <result column="item_discount" property="itemDiscount"/>
              <result column="item_money" property="itemMoney"/>
              <result column="project_id" property="projectId"/>
              <result column="item_status" property="itemStatus"/>
              <result column="remark" property="remark"/>
              <result column="update_time" property="updateTime"/>
              <result column="create_time" property="createTime"/>
              <collection property="recordsItemList" ofType="cn.iocoder.yudao.module.base.entity.RepairRecordsItem"
                          columnPrefix="item_">
                     <id column="id" property="id"/>
                     <result column="record_id" property="recordId"/>
                     <result column="ticket_id" property="ticketId"/>
                     <result column="repair_item_id" property="repairItemId"/>
                     <result column="remark" property="remark"/>
                     <result column="image" property="image"/>
                     <result column="is_open" property="isOpen"/>
                     <result column="create_time" property="createTime"/>
              </collection>
       </resultMap>
       <update id="updateRepairAndSale" parameterType="cn.iocoder.yudao.module.tickets.entity.DlRepairTitem">
              UPDATE dl_repair_titem
              <set>
                     <if test="repairIds != null">
                            repair_ids = #{repairIds},
                     </if>
                     <if test="repairNames != null">
                            repair_names = #{repairNames},
                     </if>
                     <if test="saleId != null">
                            sale_id = #{saleId},
                     </if>
                     <if test="saleName != null">
                            sale_name = #{saleName},
                     </if>
              </set>
              WHERE ticket_id = #{ticketId} and project_id = #{projectId}
       </update>


       <select id="getTItemPage" resultType="cn.iocoder.yudao.module.tickets.vo.DlRepairTitemRespVO">
        SELECT drti.id,
               drti.ticket_id,
               drti.item_name,
               drti.item_count,
               drti.item_unit,
               drti.item_price,
               drti.item_discount,
               drti.item_money,
               drti.repair_ids,
               drti.repair_names,
               drti.sale_id,
               drti.sale_name,
               drti.item_type,
               drti.project_id,
               drti.part_id,
               drti.other_id,
               drti.item_type_id,
               drti.item_status,
               drti.remark,
               drti.tenant_id,
               drti.deleted,
               drti.creator,
               drti.create_time,
               drti.updater,
               drti.update_time,
               drt.car_no,
               drt.user_name,
               drt.user_mobile,
               drt.ticket_no
        FROM dl_repair_titem drti
                 LEFT JOIN dl_repair_tickets drt ON drti.ticket_id = drt.id
    </select>
    <select id="selectProjList" resultMap="BaseResultMap">
           SELECT
                  drt.* ,
                  drri.id AS item_id,
                  drri.record_id AS item_record_id,
                  drri.ticket_id AS item_ticket_id,
                  drri.repair_item_id AS item_repair_item_id,
                  drri.remark AS item_remark,
                  drri.image AS item_image,
                  drri.is_open AS item_is_open,
                  drri.create_time AS item_create_time
           FROM
                  dl_repair_titem drt
                         LEFT JOIN dl_repair_records_item drri ON drt.id = drri.repair_item_id
                         AND drri.deleted = '0'
                         <if test="isOpen!=null and isOpen!=''">
                                AND drri.is_open = #{isOpen}
                         </if>
           WHERE
                  drt.deleted = 0
             AND drt.item_type = '01'
             AND drt.ticket_id = #{ticketId}
           ORDER BY
                  drt.item_status DESC,
                  drt.update_time,
                  drri.create_time
    </select>
    <select id="selectByTicketId" resultType="cn.iocoder.yudao.module.tickets.vo.DlRepairTitemReqVO">
           SELECT
                  drt.id,
                  drt.ticket_id,
                  drt.item_name ,
                  drt.item_count ,
                  drt.item_unit ,
                  drt.item_price ,
                  drt.item_discount ,
                  drt.item_money ,
                  drt.item_type ,
                  drt.part_id ,
                  drt.item_status ,
                  drt.remark ,
                  drt.tenant_id ,
                  drt.deleted ,
                  drt.creator ,
                  drt.create_time ,
                  drt.updater ,
                  drt.update_time,
                  drw.type AS item_type_id,
                  dbt.`name` AS item_type_name,
                  sdd.label AS unit_text
           FROM
                  dl_repair_titem drt
                         LEFT JOIN dl_repair_wares drw ON drt.part_id = drw.id
                         LEFT JOIN dl_base_type dbt ON drw.type = dbt.id
                         LEFT JOIN system_dict_data sdd ON drw.unit = sdd.value AND sdd.dict_type='repair_unit'
           WHERE drt.ticket_id = #{ticketId}
             AND drt.item_type = '02'
             AND drt.deleted = '0'
           ORDER BY drw.type,drt.create_time
    </select>
</mapper>
