<?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.stockOperate.mapper.DlRepairSoMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.stockOperate.entity.DlRepairSo">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="soType" column="so_type" jdbcType="VARCHAR"/>
            <result property="purchaseType" column="purchase_type" jdbcType="VARCHAR"/>
            <result property="soNo" column="so_no" jdbcType="VARCHAR"/>
            <result property="supplierId" column="supplier_id" jdbcType="VARCHAR"/>
            <result property="supplierName" column="supplier_name" jdbcType="VARCHAR"/>
            <result property="userId" column="user_id" jdbcType="BIGINT"/>
            <result property="userName" column="user_name" jdbcType="VARCHAR"/>
            <result property="soTime" column="so_time" jdbcType="TIMESTAMP"/>
            <result property="itemCount" column="item_count" jdbcType="INTEGER"/>
            <result property="totalPrice" column="total_price" jdbcType="DECIMAL"/>
            <result property="soStatus" column="so_status" jdbcType="VARCHAR"/>
            <result property="remark" column="remark" jdbcType="VARCHAR"/>
            <result property="corpId" column="corp_id" jdbcType="VARCHAR"/>
            <result property="corpName" column="corp_name" jdbcType="VARCHAR"/>
            <result property="deptId" column="dept_id" jdbcType="BIGINT"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
            <result property="updater" column="updater" jdbcType="VARCHAR"/>
            <result property="twId" column="tw_id" />
            <result property="licenseNumber" column="license_number" />
            <result property="userIds" column="user_ids" />
    </resultMap>

    <sql id="Base_SQL">
        select so.id,
               so.so_type,
               so.purchase_type,
               so.so_no,
               so.supplier_id,
               so.supplier_name,
               so.user_id,
               so.user_name,
               so.so_time,
               so.item_count,
               so.total_price,
               so.so_status,
               so.remark,
               so.corp_id,
               so.main_id,
               so.corp_name,
               so.dept_id,
               so.create_time,
               so.update_time,
               so.updater,
               so.tw_id,
               dtw.license_number,
               so.user_ids
        from dl_repair_so so
        LEFT JOIN dl_ticket_wares dtw ON  so.tw_id = dtw.id
        where so.deleted = '0'
    </sql>

    <select id="getRepairSoPage" resultMap="BaseResultMap">
        <include refid="Base_SQL"/>
        <if test="map.soType != null and map.soType">
            and so.so_type = #{map.soType}
        </if>
        <if test="map.purchaseType != null and map.purchaseType != ''">
            and so.purchase_type = #{map.purchaseType}
        </if>
        <if test="map.soStatus != null and map.soStatus != ''">
            and so.so_status = #{map.soStatus}
        </if>
        <if test="map.soStatus == null">
            and (so.so_status != '06' or so.so_status is null)
        </if>
        <if test="map.searchTimeArray != null and map.searchTimeArray.length > 0">
            and (so.create_time between #{map.searchTimeArray[0]} and #{map.searchTimeArray[1]})
        </if>
        <if test="map.supplierId != null and map.supplierId != ''">
            and so.supplier_id = #{map.supplierId}
        </if>
        <if test="map.corpId != null and map.corpId != ''">
            and so.corp_id = #{map.corpId}
        </if>
        <if test="map.soNo != null and map.soNo != ''">
            and (so.so_no like concat('%', #{map.soNo}, '%') or so.remark like concat('%', #{map.soNo}, '%'))
        </if>
        <if test="map.userId != null and map.userId != ''">
            and (so.user_id = #{map.userId} or find_in_set(#{map.userId}, so.user_ids) > 0)
        </if>
        <if test="map.mainId != null and map.mainId != ''">
            and so.main_id = #{map.mainId}
        </if>
        <if test="map.twId != null and map.twId != ''">
            and so.tw_id = #{map.twId}
        </if>
        order by so.create_time desc
    </select>
    <select id="selectCountNum" resultType="java.lang.Integer">
        SELECT
            COUNT(id)
        FROM
            dl_repair_so
        WHERE
            user_id = #{userId}
          AND so_type = #{type}
          AND so_status = #{status}
          AND deleted = 0
    </select>
    <select id="selectByIdGroup" resultType="cn.iocoder.yudao.module.stockOperate.vo.DlRepairSoiRespVO">
        SELECT
            drs.id,
            drs.so_id,
            drs.raw_id,
            drs.soi_type,
            drs.goods_id,
            drs.goods_type,
            drs.ware_id,
            drs.goods_count,
            drs.in_count,
            drs.goods_price,
            drs.remark,
            drw.type AS type_id,
            drw.name AS goods_name,
            dbt.`name` AS type_name,
            drw.price AS sale_price,
            drw.stock AS stock,
            sdd.label AS unit_text
        FROM
            dl_repair_soi drs
                LEFT JOIN dl_repair_wares drw ON drs.goods_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
            drs.deleted = '0'
          AND drs.so_id=#{id}
    </select>
</mapper>
