<?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.dk.mdm.mapper.common.CommonMapper">

    <!-- 获取显示顺序的最大值 -->
    <select id="getMaxDisplayNo" resultType="Integer">
        select max(display_no) + 1
        from ${table}
        where 1=1
        <if test="flgValid != null">
            and flg_valid = #{flgValid}
        </if>
        <if test="cpId != null">
            and cp_id = #{cpId}
        </if>
        <if test="otherCond">
            and ${otherCond}
        </if>
        <!--数据字典-->
        <if test="dictCode != null">
            and dict_code = #{dictCode}
        </if>
    </select>

    <!--获取编码-->
    <select id="getUniqueCode" resultType="java.util.Map">
        select out_id AS "outId", out_code AS "outNote"
        from dkic_b.f_create_code_id(#{cpId}, #{docName});
    </select>

    <!--获取单号-->
    <select id="getUniqueNote" resultType="java.util.Map">
        select out_id AS "outId", out_note AS "outNote"
        from dkic_b.f_create_note_id(#{cpId}, #{docName});
    </select>

    <!-- 获取用户菜单-->
    <select id="getMenuByUser" resultType="java.util.Map">
        select menu_uuid      AS "menuUuid",
               name           AS "name",
               menu_name      AS "menuName",
               parent_uuid    AS "parentUuid",
               menu_type      AS "menuType",
               flg_menu       AS "flgMenu",
               object_code    AS "objectCode",
               object_path    AS "objectPath",
               object_icon    AS "icon",
               object_id      AS "objectId",
               menu_tags      AS "menuTags",
               level_code     AS "levelCode",
               flg_same_route AS "flgSameRoute",
               flg_choose_id  AS "flgChooseId",
               flg_auto_query AS "flgAutoQuery",
               page_grid_info AS "pageGridInfo",
               grade_code     AS "gradeCode",
               grade_level    AS "gradeLevel",
               is_vip         AS "isVip",
               fun_uuid       AS "funUuid",
               #{appCode}     AS "appCode",
               menu_uuid      AS "id",
               parent_uuid    AS "parentId"
        from
            dkic_b.f_get_user_menu(#{appCode}, #{userId } ::uuid,#{cpId},#{lang})
    </select>

    <!--查用户功能权限-->
    <select id="getUserFunction" resultType="java.util.Map">
        select appm.menu_uuid                                         AS "menuUuid"
             , appm.menu_name                                         as name
             , sys.f_get_name_i18n(appm.menu_name_i18n, #{i18n}) as "menuName"
             , rr.fun_uuid                                            AS "funUuid"
             , appm.object_code                                       AS "objectCode"
             , appm.menu_type                                         AS "menuType"
        from sys.t_app_menu appm
                 inner join core.t_user_fun rr
                            on rr.fun_uuid = appm.fun_uuid
        where appm.flg_valid
          and appm.app_code = #{appCode}
          and appm.menu_type IN (1, 2)
          and rr.user_id = #{userId};
    </select>

    <!--查询用于隐藏列信息-->
    <select id="getUserTableInfo" resultType="java.util.Map">
        select tuts.code,
               tuts.table_type AS "tableType",
               tuts.table_ref  AS "tableRef",
               tuts.columns
        from pset.t_user_table_set tuts
        where tuts.flg_valid
          and tuts.user_id = #{userId};
    </select>

    <!--查导航菜单(自定义报表用)-->
    <select id="getMenuNavigation" resultType="java.util.Map">
        select appm.menu_uuid AS "menuUuid"
        ,sys.f_get_name_i18n(appm.menu_name_i18n,#{i18n}) as "menuName"
        ,appm.parent_uuid AS "parentUuid"
        from sys.t_app_menu appm
        where appm.flg_valid
        <if test="parentUuid">
            and appm.parent_uuid = #{parentUuid} ::uuid
        </if>
        and appm.app_code = 'WEB-MAIN'
        and appm.menu_type = 0
    </select>


    <!-- 获取应用-->
    <select id="getApplication" resultType="java.util.Map">
        select
            app_code as "appCode",
            app_name as "appName"
        from sys.t_application
    </select>

    <!--获取单据-->
    <select id="getDoc" resultType="java.util.Map">
        SELECT
        tsd.doc_name AS "docName"
        FROM
        sys.t_doc tsd
        <where>
            <if test="flgApproval!=null">
                and tsd.flg_approval = #{flgApproval}
            </if>
            <if test="flgPrint!=null">
                and tsd.flg_print = #{flgPrint}
            </if>
        </where>
    </select>

    <!-- 获取数据种类 -->
    <select id="getDataKind" resultType="java.util.Map">
        select kind_code AS "kindCode",
        sys.f_get_name_i18n(kind_name_i18n,#{i18n}) as "kindName",
        sys.f_get_name_i18n(kind_desc,#{i18n}) as "gradeKindDesc",
        kind_type AS "kindType",
        kind_tags AS "kindTags",
        display_no AS "displayNo",
        kind_code AS "id",
        remarks
        from sys.t_data_kind
        where flg_valid
        <if test="kindType != null">
            AND kind_type = #{kindType}
        </if>
        <if test="usedFlag != null">
            AND kind_tags -> 'used' ?? '标签'
        </if>
        <if test="kindTag != null">
            AND kind_tags -> 'used' ?? #{kindTag}
        </if>
        <if test="kindCodeList != null and kindCodeList.size()>0">
            AND kind_code =any(#{kindCodeList,typeHandler=StringListTypeHandler})
        </if>
        order by display_no
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>


    <!-- 获取组织部门 -->
    <select id="getOrg" resultType="java.util.Map">
        select org_Id     AS "orgId",
        org_code   AS "orgCode",
        org_name   AS "orgName",
        org_Id     AS "id",
        parent_id  AS "parentId",
        top_Id     AS "topId",
        display_no AS "displayNo",
        level_code AS "levelCode",
        level_no    AS "levelNo",
        remarks
        ,flg_valid as "flgValid"
        ,flg_business as "flgBusiness"
        from dkic_b.t_mst_org
        where flg_valid
        and cp_id = #{cpId}
        <if test="topId != null">
            AND top_id = #{topId}
        </if>
        <if test="orgId != null">
            AND org_id != #{orgId}
        </if>
        <if test="orgCode != null">
            AND org_code LIKE concat('%',my_ex.likequery(#{orgCode}),'%')
        </if>
        <if test="orgName != null">
            AND org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
        <if test="flgBusiness != null">
            AND flg_business  = #{flgBusiness}
        </if>
        <if test="searchText !=null">
            AND ( org_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
            or org_code LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
        </if>
        <if test="ids != null and ids.size() > 0 ">
            order by case org_id
            <foreach collection="ids" index="index" item="item">
                when #{item}::uuid then #{item}::uuid
            </foreach>
            end
        </if>
        <if test="ids == null">
            order by display_no
        </if>

    </select>
    <!-- 只获取二级部门 按照level_code 排序 -->
    <select id="getProcureOrg" resultType="java.util.Map">
        select org_Id     AS "orgId",
        org_code   AS "orgCode",
        org_name   AS "orgName",
        org_Id     AS "id",
        parent_id  AS "parentId",
        top_Id     AS "topId",
        display_no AS "displayNo",
        remarks
        from dkic_b.t_mst_org
        where flg_valid
        and cp_id = #{cpId}
        and parent_id is NOT null
        <if test="topId != null">
            AND top_id = #{topId}
        </if>
        <if test="orgId != null">
            AND org_id != #{orgId}
        </if>
        <if test="orgCode != null">
            AND org_code LIKE concat('%',my_ex.likequery(#{orgCode}),'%')
        </if>
        <if test="orgName != null">
            AND org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
        order by level_code
    </select>

    <!-- t_mst_org，（条件查询）个数 -->
    <select id="getOrgCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_org
        where flg_valid
        and cp_id = #{cpId}
        <if test="topId != null">
            AND top_id = #{topId}
        </if>
        <if test="orgId != null">
            AND org_id != #{orgId}
        </if>
        <if test="orgCode != null">
            AND org_code LIKE concat('%',my_ex.likequery(#{orgCode}),'%')
        </if>
        <if test="orgName != null">
            AND org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
    </select>

    <!-- 获取角色 -->
    <select id="getRole" resultType="java.util.Map">
        select role_Id    AS "roleId",
               role_code  AS "roleCode",
               role_name  AS "roleName",
               display_no AS "displayNo",
               remarks
        from dkic_b.t_mst_role
        where cp_id = #{cpId}
          and flg_valid
        <if test="searchText !=null">
            AND   role_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
        </if>
        order by display_no
    </select>

    <!-- 获取员工 -->
    <select id="getStaffByPage" resultType="java.util.Map">
        select staff.staff_id                                      AS "staffId",
        staff.staff_code                                    AS "staffCode",
        staff.staff_name                                    AS "staffName",
        staff.staff_phone                                    AS "staffPhone",
        sys.f_code_name(staff.staff_code, staff.staff_name) AS "staffCodeName",
        staff.remarks
        ,tmg.org_id as "orgId"
        ,tmg.org_code as "orgCode"
        ,tmg.org_name as "orgName"
        ,staff.flg_valid as "flgValid"
        from dkic_b.t_mst_staff AS staff
        left join  dkic_b.t_mst_org tmg on tmg.org_id = staff.org_id
        where staff.flg_valid
        and staff.cp_id =#{cpId}
        and staff.flg_ghost = false
        <if test="staffCode != null and staffCode != ''">
            And position (#{staffCode} in staff.staff_code)>0
        </if>
        <if test="staffName != null and staffName != ''">
            And position (#{staffName} in staff.staff_name)>0
        </if>
        <if test="searchText !=null">
            AND ( staff.staff_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
            or staff.staff_code LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
        </if>
        <if test="ids != null and ids.size() > 0 ">
            order by case staff.staff_id
            <foreach collection="ids" index="index" item="item">
                when #{item}::uuid then #{item}::uuid
            </foreach>
            end
        </if>
        <if test="ids == null ">
            order by staff.staff_code
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_user，（条件查询）个数 -->
    <select id="getStaffCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_staff AS staff
        where staff.flg_valid
        and staff.cp_id =#{cpId}
        and staff.flg_ghost = false
        <if test="staffCode != null and staffCode != ''">
            And position (#{staffCode} in staff.staff_code)>0
        </if>
        <if test="staffName != null and staffName != ''">
            And position (#{staffName} in staff.staff_name)>0
        </if>
    </select>

    <!--    函数查询单个系统参数-->
    <select id="getSettingValue" resultType="String">
        select dkic_b.f_get_setting_value(#{code}, #{cpId})
    </select>

    <!--    函数查询所有系统参数-->
    <select id="getAllSettingValue" resultType="Map">
        select f_get_all_setting_value AS "setting" from dkic_b.f_get_all_setting_value(#{cpId})
    </select>


    <!--    函数查询多个系统参数-->
    <select id="getSettingValues" resultType="Map">
        select core.f_get_setting_values(
                       #{codes, typeHandler=StringListTypeHandler}, #{cpId})
    </select>

    <!--  查询系统参数分类  -->
    <select id="getSettingKind" resultType="java.util.Map">
        select sk.kind_code                                         as "classCode",
               sys.f_get_name_i18n_lang(sk.kind_name_i18n, #{i18n}) as "className",
               sk.display_no                                        as "displayNo"
        from sys.t_setting_kind as sk
        where sk.flg_valid
        order by sk.display_no
    </select>

    <!-- 获取数据字典 -->
    <select id="getDictionaryData" resultType="java.util.Map">
        select dd.data_id     as "dataId",
               dd.data_id     as "id",
               dd.dict_code   as "dictCode",
               dd.data_code   as "dataCode",
               dd.data_value  as "dataValue",
               dd.flg_default as "flgDefault"
        from dkic_b.t_mst_dictionary_data as dd
        where dd.flg_valid
          and dd.cp_id = #{cpId}
          and dd.dict_code = #{dictCode}
        <if test="searchText !=null">
            AND   dd.data_value LIKE concat('%',my_ex.likequery(#{searchText}), '%')
        </if>
        order by dd.display_no
    </select>

    <!-- 获取数据字典项目 -->
    <select id="getDictionaryItemData" resultType="java.util.Map">
        SELECT stdi.dict_code                                         AS "dictCode",
               sys.f_get_name_i18n_lang(stdi.dict_name_i18n,  #{i18n}) AS "dictName"
        FROM sys.t_dictionary_item stdi
        WHERE stdi.flg_valid
          AND stdi.level_no NOT IN ('1')
    </select>

    <!-- 获取数据类型 -->
    <select id="getValueKind" resultType="java.util.Map">
        select tvk.kind_code AS "kindCode",
        tvk.kind_name as "kindName",
        sys.f_get_name_i18n_lang(tvk.kind_name_i18n,#{i18n}) AS "kindLabel"
        from sys.t_value_kind as tvk
        where tvk.flg_valid
        <if test="used != null and used != ''">
            and  #{used} = ANY (tvk.used)
        </if>
        order by tvk.display_no
    </select>


    <!-- 获取仓库 -->
    <select id="getWarehouse" resultType="java.util.Map">
        SELECT t.wh_id       as "whId"
             , t.wh_id       AS "id"
             , t.wh_name     as "whName"
             , t.wh_code     as "whCode"
             , t.flg_default as "flgDefault"
        FROM dkic_b.t_mst_warehouse t
        WHERE t.cp_id = #{cpId}
          and t.flg_valid
        <if test="whCode!=null and whCode!=''">
            and position(#{whCode} in t.wh_code) > 0
        </if>
        <if test="whName!=null and whName!=''">
            and position(#{whName} in t.wh_name) > 0
        </if>
        <!-- 部门上有仓库，只能选部门上的仓库；部门上没有仓库，可选所有仓库 -->
        <if test="orgId != null">
                and (select count(1)
                     from dkic_b.t_mst_org_wh as tmow
                     where tmow.org_id = #{orgId}::uuid) = 0
               or (exists(select 1
                          from dkic_b.t_mst_org_wh as tmow
                          where tmow.org_id = #{orgId}::uuid
                            and tmow.wh_id = t.wh_id))
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 获取商品 -->
    <select id="getGoods" resultType="java.util.Map">
        select tmgs.sku_id            as "skuId",
        tmgs.sku_code          as "skuCode",
        tmgs.sku_name          as "skuName",
        tmgs.sku_model         as "skuModel",
        tmgs.brand_id          as "brandId",
        tmgb.brand_name        as "brandName",
        tmgs.category_id       as "categoryId",
        tmgc.cat_name          as "categoryName",
        tmgs.unit_id           as "unitId",
        tmgu.unit_name         as "unitName",
        tmgs.sub_unit_id       as "subUnitId",
        tmgs.conversion_factor as "conversionFactor",
        tmgs.series_id         as "seriesId",
        tmgseries.series_name  as "seriesName",
        tmgs.sku_spec          as "skuSpec",
        tmgs.price_purchase    as "pricePurchase",
        tmgs.price_standard    as "priceStandard",
        tmgs.price_wholesale   as "priceWholesale",
        tmgs.price_limited     as "priceLimited",
        tmgs.sku_images        as "skuImages",
        tmgs.remarks
        from dkic_b.t_mst_goods_sku as tmgs
        left join dkic_b.t_mst_goods_brand as tmgb on tmgs.brand_id = tmgb.brand_id
        left join dkic_b.t_mst_goods_category as tmgc on tmgs.category_id = tmgc.cat_id
        left join dkic_b.t_mst_goods_series as tmgseries on tmgs.series_id = tmgseries.series_id
        left join dkic_b.t_mst_unit as tmgu on tmgs.unit_id = tmgu.unit_id
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="brandId != null">
            AND tmgs.brand_id  = #{brandId} ::uuid
        </if>
    </select>

    <!-- 获取商品个数 -->
    <select id="getGoodsCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_goods_sku as tmgs
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
    </select>

    <!-- 获取商品 -->
    <select id="getGoodsForOrder" resultType="java.util.Map">
        select tmgs.sku_id            as "skuId",
        tmgs.sku_code          as "skuCode",
        tmgs.sku_name          as "skuName",
        tmgs.sku_model         as "skuModel",
        tmgs.brand_id          as "brandId",
        tmgb.brand_name        as "brandName",
        tmgb.short_name        as "shortName",
        tmgs.category_id       as "categoryId",
        tmgc.cat_name          as "categoryName",
        tmgs.unit_id           as "unitId",
        tmgu.unit_name         as "unitName",
        tmgs.sub_unit_id       as "subUnitId",
        tmgus.unit_name         as "subUnitName",
        tmgs.flg_sub_unit       as "flgSubUnit",
        tmgs.conversion_factor as "conversionFactor",
        tmgs.series_id         as "seriesId",
        tmgseries.series_name  as "seriesName",
        tmgs.sku_spec          as "skuSpec",
        tmgs.price_purchase    as "pricePurchase",
        tmgs.price_standard    as "priceStandard",
        tmgs.price_wholesale   as "priceWholesale",
        tmgs.price_other   as "priceOther",
        tmgs.price_limited     as "priceLimited",
        tmgs.sku_images        as "skuImages",
        tmgs.remarks,
        tmgs.pack_box AS "packBox"
        ,tmgu.decimal_places    as "decimalPlaces"
        ,tmgs.flg_price as "flgPrice"
        from dkic_b.t_mst_goods_sku as tmgs
        left join dkic_b.t_mst_goods_brand as tmgb on tmgs.brand_id = tmgb.brand_id
        left join dkic_b.t_mst_goods_category as tmgc on tmgs.category_id = tmgc.cat_id
        left join dkic_b.t_mst_goods_series as tmgseries on tmgs.series_id = tmgseries.series_id
        left join dkic_b.t_mst_unit as tmgu on tmgs.unit_id = tmgu.unit_id
        left join dkic_b.t_mst_unit as tmgus on tmgs.sub_unit_id = tmgus.unit_id
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="brandId != null">
            AND tmgb.brand_id = #{brandId}::uuid
        </if>
        <if test="categoryId != null">
            AND tmgs.category_id = #{categoryId}::uuid
        </if>
        <if  test="searchText != null">
            AND (tmgs.sku_model  like concat('%', my_ex.likequery(#{searchText}) , '%')
            OR  tmgs.sku_name like concat('%', my_ex.likequery(#{searchText}) , '%')
            )
        </if>
    </select>

    <!-- 获取商品个数 -->
    <select id="getGoodsForOrderCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_goods_sku as tmgs
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if  test="searchText != null">
            AND (tmgs.sku_model  like concat('%', my_ex.likequery(#{searchText}) , '%')
            OR  tmgs.sku_name like concat('%', my_ex.likequery(#{searchText}) , '%')
            )
        </if>
    </select>

    <!-- 获取商品(采购) -->
    <select id="getGoodsForPurchase" resultType="java.util.Map">
        select tmgs.sku_id            as "skuId",
        tmgs.sku_code          as "skuCode",
        tmgs.sku_name          as "skuName",
        tmgs.sku_model         as "skuModel",
        tmgs.brand_id          as "brandId",
        tmgb.brand_name        as "brandName",
        tmgs.category_id       as "categoryId",
        tmgc.cat_name          as "categoryName",
        tmgs.unit_id           as "unitId",
        tmgu.unit_name         as "unitName",
        tmgu.decimal_places    as "decimalPlaces",
        tmgs.sub_unit_id       as "subUnitId",
        tmgus.unit_name        as "subUnitName",
        tmgs.conversion_factor as "conversionFactor",
        tmgs.series_id         as "seriesId",
        tmgseries.series_name  as "seriesName",
        tmgs.sku_spec          as "skuSpec",
        tmgs.price_purchase    as "pricePurchase",
        tmgs.price_standard    as "priceStandard",
        tmgs.price_wholesale   as "priceWholesale",
        tmgs.price_limited     as "priceLimited",
        tmgs.sku_images        as "skuImages",
        tmgs.pack_box          as "packBox",
        tmgs.remarks
        from dkic_b.t_mst_goods_sku as tmgs
        left join dkic_b.t_mst_goods_brand as tmgb on tmgs.brand_id = tmgb.brand_id
        left join dkic_b.t_mst_goods_category as tmgc on tmgs.category_id = tmgc.cat_id
        left join dkic_b.t_mst_goods_series as tmgseries on tmgs.series_id = tmgseries.series_id
        left join dkic_b.t_mst_unit as tmgu on tmgs.unit_id = tmgu.unit_id
        left join dkic_b.t_mst_unit as tmgus on tmgs.sub_unit_id = tmgus.unit_id
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="skuModel != null">
            AND tmgs.sku_model LIKE concat('%',my_ex.likequery(#{skuModel}),'%')
        </if>
    </select>

    <!-- 获取商品个数(采购) -->
    <select id="getGoodsForPurchaseCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_goods_sku as tmgs
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
    </select>

    <!-- 获取客户 -->
    <select id="getCustomer" resultType="java.util.Map">
        select tmc.cus_id             as "cusId",
        tmc.cus_code           as "cusCode",
        tmc.cus_name           as "cusName",
        tmc.cus_phone          as "cusPhone",
        tmc.address_area       as "addressArea",
        tmc.address_name       as "addressName",
        tmc.address_no         as "addressNo",
        tmc.address_gcj02      as "addressGcj02",
        tmc.address_full       as "addressFull",
        tmc.contact_name       as "contactName",
        tmc.contact_phone      as "contactPhone",
        tmc.cus_from           as "cusFrom",
        tmdd.data_value        as "cusFromName",
        tmc.channel_id         as "channelId",
        tmsc.channel_name      as "channelName",
        tmc.org_id             as "orgId",
        tmo.org_name           as "orgName",
        tmc.staff_id           as "staffId",
        tms.staff_name         as "staffName",
        tmc.report_staff       as "reportStaff",
        reportstaff.staff_name as "reportStaffName",
        tmc.report_time        as "reportTime",
        tmc.sale_status        as "saleStatus",
        tmc.remarks            as "remarks",
        tmc.follow_staffs      as "followStaffs",
        tmc.last_follow_staff  as "lastFollowStaff",
        tmc.last_follow_id     as "lastFollowId",
        tmc.last_follow_status as "lastFollowStatus",
        tmc.last_follow_time   as "lastFollowTime"
        from dkic_b.t_mst_customer as tmc
        left join dkic_b.t_mst_org tmo on tmc.org_id = tmo.org_id
        left join dkic_b.t_mst_staff tms on tmc.staff_id = tms.staff_id
        left join dkic_b.t_mst_staff as reportstaff on tmc.report_staff = reportstaff.staff_id
        left join dkic_b.t_mst_sale_channel tmsc on tmc.channel_id = tmsc.channel_id
        left join dkic_b.t_mst_dictionary_data tmdd on tmc.cus_from = tmdd.data_id
        where tmc.flg_valid
        AND tmc.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmc.cus_code = #{cusCode}
        </if>
        <if test="skuName != null">
            AND tmc.cus_name = #{cusName}
        </if>
    </select>

    <!-- 获取客户个数 -->
    <select id="getCustomerCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_customer as tmc
        where tmc.flg_valid
        and tmc.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmc.cus_code = #{cusCode}
        </if>
        <if test="skuName != null">
            AND tmc.cus_name = #{cusName}
        </if>
    </select>

    <!--    获取单据Id-->
    <select id="getUuidSeq" resultType="object">
        select sys.f_uuid_seq()
    </select>

    <!--    获取单据单号-->
    <select id="getDocNo" resultType="java.lang.String">
        select dkic_b.f_create_note(#{cpId}, #{purId}::uuid, #{docCode})
    </select>

    <!--获取供应商-->
    <select id="getSupplier" resultType="java.util.Map">
        select tms.sup_code       as "supplierCode",
        tms.sup_name              as "supplierName",
        tms.sup_id                as "supplierId",
        sys.f_get_name_i18n(tdk.kind_name_i18n, #{i18n}) AS  "supplierType",
        tms.contact_name          as "contactName",
        tms.contact_phone         as "contactPhone",
        tms.flg_valid             as "flgValid"
        from dkic_b.t_mst_supplier tms
        LEFT JOIN sys.t_data_kind tdk
        ON tdk.kind_code = tms.sup_type
        where tms.cp_id = #{cpId}
        <if test="supplierName!=null and supplierName!=''">
            AND tms.sup_name LIKE concat('%',my_ex.likequery(#{supplierName}),'%')
        </if>
        <if test="supplierCode!=null and supplierCode!=''">
            AND tms.sup_code LIKE concat('%',my_ex.likequery(#{supplierCode}),'%')
        </if>
        <if test="supplierType!=null and supplierType!=''">
            AND tms.sup_type = #{supplierType}
        </if>
        order by tms.sup_code
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
        <if test="limit != null and limit != 0 ">
            limit #{limit}
        </if>
    </select>

    <!--获取供应商数量-->
    <select id="countSupplier" resultType="java.lang.Long">
        select count(1)
        from dkic_b.t_mst_supplier tms
        LEFT JOIN sys.t_data_kind tdk
        ON tdk.kind_code = tms.sup_type
        where tms.cp_id = #{cpId}
        <if test="supplierName!=null and supplierName!=''">
            AND tms.sup_name LIKE concat('%',my_ex.likequery(#{supplierName}),'%')
        </if>
        <if test="supplierCode!=null and supplierCode!=''">
            AND tms.sup_code LIKE concat('%',my_ex.likequery(#{supplierCode}),'%')
        </if>
        <if test="supplierType!=null and supplierType!=''">
            AND tms.sup_type LIKE concat('%',my_ex.likequery(#{supplierType}),'%')
        </if>
    </select>

    <!--获取渠道 -->
    <select id="getChannel" resultType="java.util.Map">
        select c.channel_id   as "channelId",
        c.channel_code as "channelCode",
        c.channel_name as "channelName",
        c.sys_code     as "sysCode",
        c.display_no   as "displayNo"
        from dkic_b.t_mst_sale_channel as c
        where c.flg_valid
        and c.cp_id = #{cpId}
        <if test="channel_code!=null and channel_code!=''">
            AND c.channel_code LIKE concat('%', #{channelCode}, '%')
        </if>
        <if test="channel_name!=null and channel_name!=''">
            AND c.channel_name LIKE concat('%', #{channelName}, '%')
        </if>
        order by c.display_no
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
        <if test="limit != null and limit != 0 ">
            limit #{limit}
        </if>
    </select>

    <!-- 查询商品档案-->
    <select id="getGoodsBrand" resultType="java.util.Map">
        SELECT
        tmgb.flg_valid AS "flgValid",
        tmgb.brand_id AS "brandId", tmgb.brand_code AS "brandCode", tmgb.brand_name  AS "brandName",
        (SELECT  COALESCE(array_to_string(array_agg(tsm.sup_name), ','),'')
        FROM dkic_b.t_mst_supplier_brand tsmb
        left join dkic_b.t_mst_supplier tsm on tsm.sup_id = tsmb.sup_id
        WHERE tsmb.brand_id  =  tmgb.brand_id) AS "supplierName"
        FROM dkic_b.t_mst_goods_brand tmgb
        <where>
            <if test="searchText !=null">
                AND   tmgb.brand_name LIKE concat('%',my_ex.likequery(#{searchText}), '%')
            </if>
            <if test="cpId != null">
                AND tmgb.cp_id = #{cpId}
            </if>
            <if test="flgValid != null">
                AND tmgb.flg_valid = #{flgValid}
            </if>
        </where>
        order by tmgb.display_no asc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!--    查询商品种类-->
    <select id="getGoodsCategoryTree" resultType="java.util.Map">
        SELECT
        T.cat_id AS "catId",
        T.cat_code AS "catCode",
        T.cat_name AS "catName",
        T.parent_id AS "parentId",
        T.flg_valid AS "flgValid"
        FROM
        (--当前数据
        SELECT
        tmgc.cat_id,
        tmgc.cat_code,
        tmgc.cat_name,
        tmgc.parent_id,
        tmgc.flg_valid
        FROM
        dkic_b.t_mst_goods_category tmgc
        <where>
            <if test="searchText !=null">
                AND ( tmgc.cat_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or tmgc.remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND tmgc.cp_id = #{cpId}
            </if>
            AND  tmgc.flg_valid
        </where>
        UNION ALL--父级
        SELECT
        tmgcp.cat_id,
        tmgcp.cat_code,
        tmgcp.cat_name,
        tmgcp.parent_id,
        tmgcp.flg_valid
        FROM
        dkic_b.t_mst_goods_category tmgc
        LEFT JOIN dkic_b.t_mst_goods_category tmgcp ON tmgc.cat_id = ANY ( tmgcp.level_lower )
        <where>
            <if test="searchText !=null">
                AND ( tmgc.cat_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or tmgc.remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND tmgc.cp_id = #{cpId}
            </if>
            AND  tmgc.flg_valid
        </where>
        ) T
        WHERE T.cat_id IS NOT NULL
        GROUP BY
        T.cat_id,
        T.cat_code,
        T.cat_name,
        T.parent_id,
        T.flg_valid

        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>

    </select>

    <!-- 查询表t_mst_goods_brand，（条件查询）个数 -->
    <select id="countGoodsBrand" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_mst_goods_brand tmgb
        <where>
            <if test="searchText !=null">
                AND   tmgb.brand_name LIKE concat('%',my_ex.likequery(#{searchText}), '%')
            </if>
            <if test="cpId != null">
                AND tmgb.cp_id = #{cpId}
            </if>
            <if test="flgValid != null">
                AND tmgb.flg_valid = #{flgValid}
            </if>
        </where>
    </select>
    <!--    查询商品种类-->
    <select id="getGoodsCategory" resultType="java.util.Map">
        SELECT
        cat_id AS "catId",
        cat_code AS "catCode",
        cat_name AS "catName",
        parent_id AS "parentId",
        flg_valid AS "flgValid"
        FROM dkic_b.t_mst_goods_category
        <where>
            <if test="searchText !=null">
                AND ( cat_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or   remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
        ORDER BY display_no
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>

    </select>
    <!--    查询商品种类的个数-->
    <select id="countGoodsCategory" resultType="java.lang.Long">
        SELECT
        count(1)
        FROM dkic_b.t_mst_goods_category
        <where>
            <if test="searchText !=null">
                AND ( cat_name LIKE concat('%', #{searchText}, '%')
                or   remarks LIKE concat('%', #{searchText}, '%'))
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
    </select>
    <!--    查询商品系列-->
    <select id="getGoodsSeries" resultType="java.util.Map">
        SELECT
        series_id AS "seriesId",
        series_code AS "seriesCode",
        series_name AS "seriesName",
        flg_valid AS "flgValid"
        FROM dkic_b.t_mst_goods_series
        <where>
            <if test="searchText !=null">
                AND ( series_name LIKE concat('%',my_ex.likequery(#{searchText}), '%')
                or   remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            AND flg_valid
        </where>
        order by display_no asc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!--    商品系列个数-->
    <select id="countGoodsSeries" resultType="java.lang.Long">
        SELECT
        count(1)
        FROM dkic_b.t_mst_goods_series
        <where>
            <if test="searchText !=null">
                AND ( series_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or   remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
    </select>

    <!--    获取计量单位-->
    <select id="getUnit" resultType="java.util.Map">
        SELECT
        unit_id AS "unitId",
        unit_code AS "unitCode",
        unit_name AS "unitName",
        decimal_places AS "decimalPlaces",
        flg_valid AS "flgValid"
        FROM  dkic_b.t_mst_unit
        <where>
            <if test="searchText !=null">
                AND ( unit_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or   remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            AND flg_valid
        </where>
        order by display_no asc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!--    获取计量单位个数-->
    <select id="countUnit" resultType="java.lang.Long">
        SELECT
        count(1)
        FROM  dkic_b.t_mst_unit
        <where>
            <if test="searchText !=null">
                AND ( unit_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or   remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
    </select>

    <!--获取仓库档案-->
    <select id="getWarehouseByPage" resultType="java.util.Map">
        SELECT t.wh_id         AS "whId",
               t.wh_code       AS "whCode",
               t.wh_name       AS "whName",
               t.supervisor    AS "supervisor",
               t.contact_phone AS "contactPhone",
               t.flg_valid     AS "flgValid"
        FROM dkic_b.t_mst_warehouse as t
        <where>
            <if test="searchText !=null">
                AND ( t.wh_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or  t.remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND t.cp_id = #{cpId}
            </if>
            <if test="flgValid != null">
                AND t.flg_valid = #{flgValid}
            </if>
            <!-- 部门上有仓库，只能选部门上的仓库；部门上没有仓库，可选所有仓库 -->
            <if test="orgId != null">
                and (select count(1)
                     from dkic_b.t_mst_org_wh as tmow
                     where tmow.org_id = #{orgId}::uuid) = 0
               or (exists(select 1
                          from dkic_b.t_mst_org_wh as tmow
                          where tmow.org_id = #{orgId}::uuid
                            and tmow.wh_id = t.wh_id))
            </if>
        </where>
        order by display_no asc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!--获取仓库档案个数-->
    <select id="countWarehouseByPage" resultType="Long">
        SELECT count(1)
        FROM  dkic_b.t_mst_warehouse as t
        <where>
            <if test="searchText !=null">
                AND ( t.wh_name LIKE concat('%', my_ex.likequery(#{searchText}), '%')
                or  t.remarks LIKE concat('%', my_ex.likequery(#{searchText}), '%'))
            </if>
            <if test="cpId != null">
                AND t.cp_id = #{cpId}
            </if>
            <!-- 部门上有仓库，只能选部门上的仓库；部门上没有仓库，可选所有仓库 -->
            <if test="orgId != null">
                and (select count(1)
                     from dkic_b.t_mst_org_wh as tmow
                     where tmow.org_id = #{orgId}::uuid) = 0
               or (exists(select 1
                          from dkic_b.t_mst_org_wh as tmow
                          where tmow.org_id = #{orgId}::uuid
                            and tmow.wh_id = t.wh_id))
            </if>
        </where>
    </select>

    <!-- 查询库存 -->
    <select id="getInventory" resultType="java.util.Map">
        select tpi.inv_id             as "invId",
               tpi.sku_id             as "skuId",
               tmgs.sku_code          as "skuCode",
               tmgs.sku_name          as "skuName",
               tmgs.sku_model         as "skuModel",
               tmgs.brand_id          as "brandId",
               tmgb.brand_name        as "brandName",
               tmgs.category_id       as "categoryId",
               tmgc.cat_name          as "categoryName",
               tmgs.unit_id           as "unitId",
               tmgs.sub_unit_id       as "subUnitId",
               tmgs.conversion_factor as "conversionFactor",
               tmgs.series_id         as "seriesId",
               tmgseries.series_name  as "seriesName",
               tmgs.sku_spec          as "skuSpec",
               tmgs.price_purchase    as "pricePurchase",
               tmgs.price_standard    as "priceStandard",
               tmgs.price_wholesale   as "priceWholesale",
               tmgs.price_limited     as "priceLimited",
               tmgs.sku_images        as "skuImages",
               tpi.non_std_code       as "nonStdCode",
               tpi.inv_qty            as "invQty",
               tpi.outing_qty         as "outingQty",
               tpi.freeze_qty         as "freezeQty",
               tpi.usable_qty         as "usableQty",
               tpi.wh_id              as "whId",
               tmw.wh_code            as "whCode",
               tmw.wh_name            as "whName",
               tmgs.pack_box          as "packBox",
               tmgs.unit_id           as "unitId",
               tmgs.sub_unit_id       as "subUnitId",
               ul.unit_name           as "unitName",
               u2.unit_name           as "subUnitName",
               dkic_b.f_get_last_ivt_price(tpi.inv_id,null,null,null) as "priceInto",
               tpi.remarks
        from dkic_b.t_psi_inventory as tpi
                 inner join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id
                 inner join dkic_b.t_mst_goods_sku as tmgs on tmgs.sku_id = tpi.sku_id
                 left join dkic_b.t_mst_goods_brand as tmgb on tmgs.brand_id = tmgb.brand_id
                 left join dkic_b.t_mst_goods_category as tmgc on tmgs.category_id = tmgc.cat_id
                 left join dkic_b.t_mst_goods_series as tmgseries on tmgs.series_id = tmgseries.series_id
                 left join dkic_b.t_mst_unit as ul on tmgs.unit_id = ul.unit_id
                 left join dkic_b.t_mst_unit as u2 on tmgs.sub_unit_id = u2.unit_id
        where tpi.flg_valid
          AND tpi.cp_id = #{cpId}
        <if test="skuId != null">
            AND tpi.sku_id = #{skuId} ::uuid
        </if>
        <if test="nonStdCode != null">
            AND tpi.non_std_code = #{nonStdCode}
        </if>
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="whName != null">
            AND tmw.wh_name LIKE concat('%',my_ex.likequery(#{whName}),'%')
        </if>
        <if test="whId != null">
            AND tpi.wh_id = #{whId} ::uuid
        </if>
        <if test="inqtyFlag != null">
            AND tpi.inv_qty != 0
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询库存个数 -->
    <select id="getInventoryCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_psi_inventory as tpi
        inner join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id
        inner join dkic_b.t_mst_goods_sku as tmgs on tmgs.sku_id = tpi.sku_id
        where tpi.flg_valid
        AND tpi.cp_id = #{cpId}
        <if test="skuId != null">
            AND tpi.sku_id = #{skuId} ::uuid
        </if>
        <if test="nonStdCode != null">
            AND tpi.non_std_code = #{nonStdCode}
        </if>
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="whName != null">
            AND tmgs.wh_name LIKE concat('%',my_ex.likequery(#{whName}),'%')
        </if>
    </select>


    <!--    获取销售渠道-->
    <select id="getSaleChannel" resultType="java.util.Map">
        SELECT
        channel_id AS "channelId",
        channel_id AS "id",
        channel_code AS "channelCode",
        channel_name AS "channelName"
        FROM dkic_b.t_mst_sale_channel
        <where>
            flg_valid
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
            order by display_no
        </where>
    </select>

    <!--获取结算方式数据-->
    <select id="getSettlement" resultType="java.util.Map">
        SELECT T.mac_id id,
               T.mac_code AS "macCode",
               T.mac_name AS "macName",
               T.mac_Type,
               dd.data_value AS "macTypeName",
               T.balance,
               T.display_no,
               T.mac_receipt_code,
               T.acc_date,
               T.flg_negative,
               T.remarks,
               T.flg_valid,
               T.cp_id,
               COALESCE ( tmti.amt_trf, 0 ) AS "initBalance"
        FROM
            dkic_b.t_mst_money_account
                AS T LEFT JOIN dkic_b.t_mst_dictionary_data AS dd ON T.mac_type = dd.data_id
                     LEFT JOIN (
                SELECT
                    tmti.trf_in_mac,
                    tmti.amt_trf
                FROM
                    dkic_b.t_mac_transfer AS tmt
                        INNER JOIN dkic_b.t_mac_transfer_item AS tmti ON tmti.trf_id = tmt.trf_id
                        AND tmt.flg_valid
                        AND tmti.flg_valid
                        AND tmt.trf_type = 0
            ) tmti ON tmti.trf_in_mac = T.mac_id
        WHERE
            T.cp_id = #{cpId}
          and t.flg_valid
        ORDER BY
            T.display_no
    </select>

    <!--    获取销售渠道-->
    <select id="getSaleChannelPurviewType" resultType="java.util.Map">
        SELECT
        sc.channel_id AS "channelId",
        sc.channel_id AS "id",
        sc.channel_code AS "channelCode",
        sc.channel_name AS "channelName",
        COALESCE ( ( SELECT flg_nolimit FROM dkic_b.t_mst_staff_purview WHERE staff_id = #{staffId} :: UUID and purview_code = #{purviewCode}), FALSE ) "flgNolimit",
        CASE WHEN  scsc.channel_id IS NULL THEN FALSE ELSE TRUE END AS "checked"
        FROM dkic_b.t_mst_sale_channel sc
        left JOIN dkic_b.t_mst_sale_channel scsc ON scsc.channel_id = sc.channel_id and sc.channel_id::uuid =  any ( (
        select  data_ids from dkic_b.t_mst_staff_purview where staff_id = #{staffId}::uuid and purview_code = #{purviewCode})::uuid[])
        <where>
            sc.flg_valid
            <if test="cpId != null">
                AND sc.cp_id = #{cpId}
            </if>
            <if test="channelCode !=null and channelCode !=''">
                AND sc.channel_code LIKE concat('%',my_ex.likequery(#{channelCode}),'%')
            </if>
            <if test="channelName !=null and channelName !=''">
                AND sc.channel_name LIKE concat('%',my_ex.likequery(#{channelName}),'%')
            </if>
            order by sc.display_no
        </where>
    </select>
    <!--获取仓库档案-->



    <!--获取供应商和客户数据-->
    <select id="getSupplierAndCustomer" resultType="java.util.Map">
        select tms.sup_id as "objId",
        tms.sup_code as "objCode",
        tms.sup_name as "objName",
        '供应商' as "objTypeName",
        1 as "objType",
        tms.flg_valid as "flgValid"
        null as "phone",
        null as "addressFull"
        from dkic_b.t_mst_supplier tms
        <where>
        <if test="flgValid">
            AND tms.flg_valid
        </if>
        <if test="objCode != null and objCode != ''">
            AND tms.sup_code LIKE concat('%',my_ex.likequery(#{objCode}),'%')
        </if>
        <if test="objName != null and objName != ''">
            AND tms.sup_name LIKE concat('%',my_ex.likequery(#{objCode}),'%')
        </if>
        </where>
        union all
        select cus.cus_id as "objId",
        cus.cus_code as "objCode",
        cus.cus_name as "objName",
        '客户' as "objTypeName",
        2 as "objType",
        cus.flg_valid as "flgValid",
        cus.cus_phone as "phone",
        cus.address_full as "addressFull"
        from dkic_b.t_mst_customer cus
        <where>
            <if test="flgValid">
                AND cus.flg_valid
            </if>
            <if test="objCode != null and objCode != ''">
                AND cus.cus_code LIKE concat('%',my_ex.likequery(#{objCode}),'%')
            </if>
            <if test="objName != null and objName != ''">
                AND cus.cus_name LIKE concat('%',my_ex.likequery(#{objName}),'%')
            </if>
        </where>
    </select>
    <!--获取供应商和客户数据分页-->
    <select id="getSupplierAndCustomerByPage" resultType="java.util.Map">
        select * from (
        select tms.sup_id as "objId",
        tms.sup_code as "objCode",
        tms.sup_name as "objName",
        '供应商' as "objTypeName",
        1 as "objType",
        tms.flg_valid as "flgValid",
        tms.contact_phone as "phone",
        tms.return_address as "addressFull"
        from dkic_b.t_mst_supplier tms
        <where >
             tms.sup_type = '供应商类别-采购'
            and tms.cp_id = #{cpId}
            <if test="flgValid">
                AND tms.flg_valid
            </if>

            <if test="objCode != null and objCode != ''">
                AND tms.sup_code LIKE concat('%',my_ex.likequery(#{objCode}),'%')
            </if>
            <if test="objName != null and objName != ''">
                AND tms.sup_name LIKE concat('%',my_ex.likequery(#{objCode}),'%')
            </if>
            <if test="searchText != null and searchText != ''">
                AND (
                tms.sup_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or tms.contact_phone LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or tms.return_address LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>
        union all
        select cus.cus_id as "objId",
        cus.cus_code as "objCode",
        cus.cus_name as "objName",
        '客户' as "objTypeName",
        2 as "objType",
        cus.flg_valid as "flgValid",
        cus.cus_phone as "phone",
        cus.address_full as "addressFull"
        from dkic_b.t_mst_customer cus
        <where>
              cus.cp_id = #{cpId}
            <if test="flgValid">
                AND cus.flg_valid
            </if>
            <if test="objCode != null and objCode != ''">
                AND cus.cus_code LIKE concat('%',my_ex.likequery(#{objCode}),'%')
            </if>
            <if test="objName != null and objName != ''">
                AND cus.cus_name LIKE concat('%',my_ex.likequery(#{objName}),'%')
            </if>
            <if test="searchText != null and searchText != ''">
                AND ( cus.cus_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or cus.cus_phone LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or cus.address_full LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>
        ) tt
        <where>
            <if test="objType != null and objType != ''">
                AND tt."objType" = #{objType}
            </if>
        </where>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>
    <!--获取供应商和客户数据分页-->
    <select id="getSupplierAndCustomerByPageCount" resultType="Long">
        select count(1) from(
        select tms.sup_id as "objId",
        tms.sup_code as "objCode",
        tms.sup_name as "objName",
        '供应商' as "objTypeName",
        1 as "objType",
        tms.flg_valid as "flgValid",
        null as "phone",
        null as "addressFull"
        from dkic_b.t_mst_supplier tms
        <where>
            tms.sup_type = '供应商类别-采购'
            and tms.cp_id = #{cpId}
            <if test="flgValid">
                AND tms.flg_valid
            </if>
            <if test="objCode != null and objCode != ''">
                AND tms.sup_code LIKE concat('%',my_ex.likequery(#{objCode}),'%')
            </if>
            <if test="objName != null and objName != ''">
                AND tms.sup_name LIKE concat('%',my_ex.likequery(#{objCode}),'%')
            </if>
            <if test="searchText != null and searchText != ''">
                AND (
                tms.sup_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or tms.contact_phone LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or tms.return_address LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>
        union all
        select cus.cus_id as "objId",
        cus.cus_code as "objCode",
        cus.cus_name as "objName",
        '客户' as "objTypeName",
        2 as "objType",
        cus.flg_valid as "flgValid",
        cus.cus_phone as "phone",
        cus.address_full as "addressFull"
        from dkic_b.t_mst_customer cus
        <where>
            and cus.cp_id = #{cpId}
            <if test="flgValid">
                AND cus.flg_valid
            </if>
            <if test="objCode != null and objCode != ''">
                AND cus.cus_code LIKE concat('%',my_ex.likequery(#{objCode}),'%')
            </if>
            <if test="objName != null and objName != ''">
                AND cus.cus_name LIKE concat('%',my_ex.likequery(#{objName}),'%')
            </if>
            <if test="searchText != null and searchText != ''">
                AND ( cus.cus_name LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or cus.cus_phone LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                or cus.address_full LIKE concat('%',my_ex.likequery(#{searchText}),'%')
                )
            </if>
        </where>
        ) tt
        <where>
            <if test="objType != null and objType != ''">
                AND tt."objType" = #{objType}
            </if>
        </where>
    </select>


    <!-- 获取用户 -->
    <select id="getStaff" resultType="java.util.Map">
        select
          s.staff_name as "staffName"
        , s.staff_code as "staffCode"
        , sys.f_code_name(s.staff_code, s.staff_name) AS "staffCodeName"
        , s.staff_id as "staffId"
        , s.staff_id AS "id"
        , s.org_id as "orgId"
        , o.org_name as "orgName"
        , c.cp_name as "cpName"
        FROM dkic_b.t_mst_staff s
        left join dkic_b.t_mst_org o on o.org_id = s.org_id
        left join dkic_a.t_a_company c on c.cp_id = s.cp_id
        where
        s.flg_valid
        and s.hr_status = 1
        and s.cp_id = #{cpId}
        and s.flg_ghost = false
        <if test="staffCode !=null and staffCode !=''">
            AND s.staff_code LIKE concat('%',my_ex.likequery(#{staffCode}),'%')
        </if>
        <if test="staffName !=null and staffName !=''">
            AND s.staff_name LIKE concat('%',my_ex.likequery(#{staffName}),'%')
        </if>
        <if test="orgId !=null">
            AND s.org_id = #{orgId}
        </if>
        <if test="orgName !=null and orgName !=''">
            AND o.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
    </select>

    <!-- 获取用户 -->
    <select id="getStaffPurviewType" resultType="java.util.Map">
        select
        s.staff_name as "staffName"
        , s.staff_code as "staffCode"
        , s.staff_id as "staffId",
        COALESCE ( ( SELECT flg_nolimit FROM dkic_b.t_mst_staff_purview WHERE staff_id = #{staffId} :: UUID and purview_code = #{purviewCode}), FALSE ) "flgNolimit",
        CASE WHEN  ss.staff_id IS NULL THEN FALSE ELSE TRUE END AS "checked"
        FROM dkic_b.t_mst_staff s
        left JOIN dkic_b.t_mst_staff ss ON ss.staff_id = s.staff_id and s.staff_id::uuid =  any ( (
            select  data_ids from dkic_b.t_mst_staff_purview where staff_id = #{staffId}::uuid and purview_code = #{purviewCode})::uuid[])
        where
        s.flg_valid
        and s.hr_status = 1
        and s.cp_id = #{cpId}
        and s.flg_ghost = false
        <if test="staffCode !=null and staffCode !=''">
            AND s.staff_code LIKE concat('%',my_ex.likequery(#{staffCode}),'%')
        </if>
        <if test="staffName !=null and staffName !=''">
            AND s.staff_name LIKE concat('%',my_ex.likequery(#{staffName}),'%')
        </if>

    </select>


    <!-- 获取组织机构 -->
    <select id="getOrganization" resultType="java.util.Map">
        select o.org_id as "orgId"
        , o.org_code as "orgCode"
        , o.org_name as "orgName"
        , sys.f_code_name(o.org_code, o.org_name) AS "orgCodeName"
        from dkic_b.t_mst_org o
        where
        o.flg_valid
        and o.cp_id = #{cpId}
        <if test="orgCode !=null and orgCode !=''">
            AND o.org_code LIKE concat('%',my_ex.likequery(#{orgCode}),'%')
        </if>
        <if test="orgName !=null and orgName !=''">
            AND o.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
        order by o.level_code
    </select>
    <!-- 获取组织机构分页 -->
    <select id="getOrganizationByPage" resultType="java.util.Map">
        select o.org_id as "orgId"
        , o.org_code as "orgCode"
        , o.org_name as "orgName"
        , sys.f_code_name(o.org_code, o.org_name) AS "orgCodeName"
        from dkic_b.t_mst_org o
        where
        o.flg_valid
        and o.cp_id = #{cpId}
        <if test="orgCode !=null and orgCode !=''">
            AND o.org_code LIKE concat('%',my_ex.likequery(#{orgCode}),'%')
        </if>
        <if test="orgName !=null and orgName !=''">
            AND o.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
        order by o.level_code
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>
    <!-- 获取组织机构分个数-->
    <select id="getOrganizationCount" resultType="java.lang.Long">
        SELECT count(1)
        from dkic_b.t_mst_org o
        where
        o.flg_valid
        and o.cp_id = #{cpId}
        <if test="orgCode !=null and orgCode !=''">
            AND o.org_code LIKE concat('%',my_ex.likequery(#{orgCode}),'%')
        </if>
        <if test="orgName !=null and orgName !=''">
            AND o.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
    </select>

    <!-- 获取资金账户（下拉）-->
    <select id="getMac" resultType="java.util.Map">
        select tmma.mac_id           as "macId",
               tmma.mac_code         as "macCode",
               tmma.mac_name         as "macName",
               tmma.mac_type         as "macType",
               tmma.balance,
               tmma.display_no       as "displayNo",
               tmma.flg_default      as "flgDefault",
               tmma.mac_receipt_code as "macReceiptCode",
               tmma.remarks
        from dkic_b.t_mst_money_account tmma
        where tmma.flg_valid
          and tmma.cp_id = #{cpId}
        <if test="macCode !=null and macCode !=''">
            AND tmma.mac_code LIKE concat('%',my_ex.likequery(#{macCode}),'%')
        </if>
        <if test="macName !=null and macName !=''">
            AND tmma.mac_name LIKE concat('%',my_ex.likequery(#{macName}),'%')
        </if>
        order by tmma.display_no
    </select>

    <!-- 获取资金账户（放大镜） -->
    <select id="getMoneyAccount" resultType="java.util.Map">
        select tmma.mac_id           as "macId",
               tmma.mac_code         as "macCode",
               tmma.mac_name         as "macName",
               tmma.mac_type         as "macType",
               tmma.balance,
               tmma.display_no       as "displayNo",
               tmma.flg_default      as "flgDefault",
               tmma.mac_receipt_code as "macReceiptCode",
               tmma.remarks
        from dkic_b.t_mst_money_account tmma
        where tmma.flg_valid
          and tmma.cp_id = #{cpId}
        <if test="macCode !=null and macCode !=''">
            AND tmma.mac_code LIKE concat('%',my_ex.likequery(#{macCode}),'%')
        </if>
        <if test="macName !=null and macName !=''">
            AND tmma.mac_name LIKE concat('%',my_ex.likequery(#{macName}),'%')
        </if>
        order by tmma.display_no
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 获取资金账户（放大镜）个数 -->
    <select id="getMoneyAccountCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_money_account tmma
        where tmma.flg_valid
          and tmma.cp_id = #{cpId}
        <if test="macCode !=null and macCode !=''">
            AND tmma.mac_code LIKE concat('%',my_ex.likequery(#{macCode}),'%')
        </if>
        <if test="macName !=null and macName !=''">
            AND tmma.mac_name LIKE concat('%',my_ex.likequery(#{macName}),'%')
        </if>
    </select>

    <!-- 查询表core.t_m_organization，查询业务部门多业务归属 -->
    <select id="selectMultiOrg" resultType="java.util.Map">
        select t.org_id                                                                    as "orgId",
               t.org_code                                                                  as "orgCode",
               t.org_name                                                                  as "orgName",
               case when mo.allocation_ratio is null then 100 else mo.allocation_ratio end as "allocationRatio",
               case when mo.multi_id is null then 0 else 1 end                             as "selectFlag",
               case when mo.owner_type = '归属类型-主部门' then true else false end           as "ownerFlag"
        from dkic_b.t_mst_org t
                 left join (SELECT tpmo.owner_id, tpmo.allocation_ratio, tpmo.multi_id, tpmo.owner_type
                            from dkic_b.t_psi_order tpo
                                     inner join dkic_b.t_psi_multi_owner tpmo
                                                on tpo.order_id = tpmo.order_id
                            where tpmo.flg_valid
                              and tpmo.owner_type = any (array ['归属类型-主部门' , '归属类型-从部门'])
                              and tpo.order_id = #{orderId} ::uuid) mo
                           on mo.owner_id = t.org_id
        where t.flg_valid

        <if test="orgCode !=null and orgCode !=''">
            AND t.org_code LIKE concat('%',my_ex.likequery(#{orgCode}),'%')
        </if>
        <if test="orgName !=null and orgName !=''">
            AND t.org_name LIKE concat('%',my_ex.likequery(#{orgName}),'%')
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表core.t_m_user，查询员工多业务归属 -->
    <select id="selectMultiStaff" resultType="java.util.Map">
        select t.staff_id                                                                  as "staffId",
               t.staff_code                                                                as "staffCode",
               t.staff_name                                                                as "staffName",
               tmo.org_name                                                                as "orgName",
               case when mo.allocation_ratio is null then 100 else mo.allocation_ratio end as "allocationRatio",
               case when mo.multi_id is null then 0 else 1 end                             as "selectFlag",
               case when mo.owner_type = '归属类型-主员工' then true else false end               as "ownerFlag"
        from dkic_b.t_mst_staff t
                 left join dkic_b.t_mst_org as tmo on tmo.org_id = t.org_id
                 left join (SELECT tpmo.owner_id, tpmo.allocation_ratio, tpmo.multi_id, tpmo.owner_type
                            from dkic_b.t_psi_order tpo
                                     inner join dkic_b.t_psi_multi_owner tpmo
                                                on tpo.order_id = tpmo.order_id
                            where tpmo.flg_valid
                              and tpmo.owner_type = any (array ['归属类型-主员工' , '归属类型-从员工'])
                              and tpo.order_id = #{orderId} ::uuid
                     ) mo
                           on mo.owner_id = t.staff_id
        where t.flg_valid
        and t.flg_ghost = false
        <if test="staffCode!=null and staffCode!='' ">
            and t.staff_code LIKE concat('%',my_ex.likequery(#{staffCode}),'%')
        </if>
        <if test="staffName!=null and staffName!='' ">
            and t.staff_name LIKE concat('%',my_ex.likequery(#{staffName}),'%')
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表core.t_m_org_wh，查询组织仓库 -->
    <select id="selectMultiWarehouse" resultType="java.util.Map">
        select t.wh_id                                                         as "whId",
        t.wh_code                                                              as "whCode",
        t.wh_name                                                              as "whName",
        case when mo.wh_id is null then 0 else 1 end                           as "selectFlag",
        case when mo.wh_id is null then false else mo.flg_default end          as "ownerFlag"
        from dkic_b.t_mst_warehouse t
        left join (SELECT orgwh.flg_default,  orgwh.wh_id
        from dkic_b.t_mst_org org
        inner join dkic_b.t_mst_org_wh orgwh
        on org.org_id = orgwh.org_id
        where org.flg_valid
        and org.org_id = #{orgId} ::uuid) mo
        on mo.wh_id = t.wh_id
        where t.flg_valid
        <if test="whCode !=null and whCode !=''">
            AND t.wh_code LIKE concat('%',my_ex.likequery(#{whCode}),'%')
        </if>
        <if test="whName !=null and whName !=''">
            AND t.wh_name LIKE concat('%',my_ex.likequery(#{whName}),'%')
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表core.t_m_org_mac，查询组织资金账户 -->
    <select id="selectMultiMoneyAccount" resultType="java.util.Map">
        select t.mac_id                                                         as "macId",
        t.mac_code                                                              as "macCode",
        t.mac_name                                                              as "macName",
        case when mo.mac_id is null then 0 else 1 end                           as "selectFlag",
        case when mo.mac_id is null then false else mo.flg_default end          as "ownerFlag"
        from dkic_b.t_mst_money_account t
        left join (SELECT orgmac.flg_default,orgmac.mac_id
        from dkic_b.t_mst_org org
        inner join dkic_b.t_mst_org_mac orgmac
        on org.org_id = orgmac.org_id
        where org.flg_valid
        and org.org_id = #{orgId} ::uuid) mo
        on mo.mac_id = t.mac_id
        where t.flg_valid
        <if test="macCode !=null and macCode !=''">
            AND t.mac_code LIKE concat('%',my_ex.likequery(#{macCode}),'%')
        </if>
        <if test="macName !=null and macName !=''">
            AND t.mac_name LIKE concat('%',my_ex.likequery(#{macName}),'%')
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询库存 -->
    <select id="getGoodsForPurchaseReturn" resultType="java.util.Map">
        select tpi.inv_id             as "invId",
        tpi.sku_id             as "skuId",
        tmgs.sku_code          as "skuCode",
        tmgs.sku_name          as "skuName",
        tmgs.sku_model         as "skuModel",
        tmgs.brand_id          as "brandId",
        tmgb.brand_name        as "brandName",
        tmgs.category_id       as "categoryId",
        tmgc.cat_name          as "categoryName",
        tmgs.unit_id           as "unitId",
        tmgu.unit_name         as "unitName",
        tmgs.sub_unit_id       as "subUnitId",
        tmgs.conversion_factor as "conversionFactor",
        tmgs.series_id         as "seriesId",
        tmgseries.series_name  as "seriesName",
        tmgs.sku_spec          as "skuSpec",
        tmgs.price_purchase    as "pricePurchase",
        tmgs.price_standard    as "priceStandard",
        tmgs.price_wholesale   as "priceWholesale",
        tmgs.price_limited     as "priceLimited",
        tmgs.sku_images        as "skuImages",
        tpi.non_std_code       as "nonStdCode",
        tpi.inv_qty            as "invQty",
        tpi.outing_qty         as "outingQty",
        tpi.usable_qty         as "usableQty",
        tpi.wh_id              as "whId",
        tmw.wh_code            as "whCode",
        tmw.wh_name            as "whName",
        tpi.cost_price         as "costPrice",
        tpi.cost_amt           as "costAmt",
        tpi.remarks
        from dkic_b.t_psi_inventory as tpi
        inner join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id
        inner join dkic_b.t_mst_goods_sku as tmgs on tmgs.sku_id = tpi.sku_id
        left join dkic_b.t_mst_goods_brand as tmgb on tmgs.brand_id = tmgb.brand_id
        left join dkic_b.t_mst_goods_category as tmgc on tmgs.category_id = tmgc.cat_id
        left join dkic_b.t_mst_goods_series as tmgseries on tmgs.series_id = tmgseries.series_id
        left join dkic_b.t_mst_unit as tmgu on tmgs.unit_id = tmgu.unit_id
        where tpi.flg_valid
        AND tpi.cp_id = #{cpId}
        <if test="skuId != null">
            AND tpi.sku_id = #{skuId} ::uuid
        </if>
        <if test="nonStdCode != null">
            AND tpi.non_std_code = #{nonStdCode}
        </if>
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="whName != null">
            AND tmgs.wh_name LIKE concat('%',my_ex.likequery(#{whName}),'%')
        </if>
        <if test="whId != null">
            AND tpi.wh_id = #{whId} ::uuid
        </if>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询库存个数 -->
    <select id="getGoodsForPurchaseReturnByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_psi_inventory as tpi
        inner join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id
        inner join dkic_b.t_mst_goods_sku as tmgs on tmgs.sku_id = tpi.sku_id
        where tpi.flg_valid
        AND tpi.cp_id = #{cpId}
        <if test="skuId != null">
            AND tpi.sku_id = #{skuId} ::uuid
        </if>
        <if test="nonStdCode != null">
            AND tpi.non_std_code = #{nonStdCode}
        </if>
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="whName != null">
            AND tmgs.wh_name LIKE concat('%',my_ex.likequery(#{whName}),'%')
        </if>
    </select>

    <!--范围权限类型-->
    <select id="getPurviewType" resultType="java.util.Map">
        select tpt.purview_name_i18n as purviewName,
               tpt.purview_code as "code",
               "row_number"() OVER (ORDER BY tpt.display_no) as "id",
               sys.f_get_name_i18n(tpt.purview_name_i18n, #{i18n}) AS name
        from sys.t_purview_type tpt
        where tpt.flg_valid
        order by tpt.display_no
    </select>


    <select id="getInventoryByList" resultType="java.util.Map">
        select tpi.inv_id             as "invId",
        tpi.sku_id             as "skuId",
        tpi.non_std_code       as "nonStdCode",
        tpi.inv_qty            as "invQty",
        tpi.outing_qty         as "outingQty",
        tpi.usable_qty         as "usableQty",
        tpi.wh_id              as "whId",
        tmw.wh_name            as "whName",
        tpi.remarks
        from dkic_b.t_psi_inventory as tpi
        inner join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = tpi.wh_id

        <where>
            <foreach collection="skuIdNonStdCodeWhIdList" item="condition" separator=" OR ">
                <if test="condition.skuId != null">
                    tpi.sku_id = #{condition.skuId} ::uuid
                </if>
                <if test="condition.nonStdCode != null">
                    AND tpi.non_std_code = #{condition.nonStdCode}
                </if>
                <if test="condition.whId != null">
                    AND tpi.wh_id = #{condition.whId} ::uuid
                </if>
                AND tpi.flg_valid
                AND tpi.cp_id = #{cpId}
            </foreach>
        </where>

    </select>
    <!--  查询标签打印项目  -->
    <select id="getLabelPrintItem" resultType="java.util.Map">
        select
        item_code as "itemCode",
        sys.f_get_name_i18n(item_name_i18n,#{i18n}) as "itemName",
        doc_name as "docName",
        item_type as "itemType",
        item_style as "itemStyle",
        item_sample as "itemSample",
        data_key as "dataKey",
        remarks as "remarks"
        from sys.t_label_print_item
        where flg_valid
        <if test="docName">
            and doc_name = #{docName}
        </if>
        order by display_no
    </select>

    <!--获取组织仓库-->
    <select id="getOrgWh" resultType="java.util.Map">
        select t.wh_id       as "whId",
               tmw.wh_name   as "whName",
               t.flg_default as "flgDefault"
        from dkic_b.t_mst_org_wh t
                 inner join dkic_b.t_mst_warehouse as tmw on tmw.wh_id = t.wh_id
        where t.cp_id = #{cpId}
          and t.org_id = #{orgId}::uuid
        order by t.display_no
    </select>

    <!-- 获取商品（其他入库开单用） -->
    <select id="getGoodsForInto" resultType="java.util.Map">
        select tmgs.sku_id            as "skuId",
        tmgs.sku_code          as "skuCode",
        tmgs.sku_name          as "skuName",
        tmgs.sku_model         as "skuModel",
        tmgs.brand_id          as "brandId",
        tmgb.brand_name        as "brandName",
        tmgs.category_id       as "categoryId",
        tmgc.cat_name          as "categoryName",
        tmgs.conversion_factor as "conversionFactor",
        tmgs.series_id         as "seriesId",
        tmgseries.series_name  as "seriesName",
        tmgs.sku_spec          as "skuSpec",
        tmgs.price_purchase    as "priceInto",
        tmgs.price_standard    as "priceStandard",
        tmgs.price_wholesale   as "priceWholesale",
        tmgs.price_limited     as "priceLimited",
        tmgs.sku_images        as "skuImages",
        tmgs.pack_box          as "packBox",
        tmgs.unit_id           as "unitId",
        tmgs.sub_unit_id       as "subUnitId",
        ul.unit_name           as "unitName",
        u2.unit_name           as "subUnitName",
        tmgs.remarks
        from dkic_b.t_mst_goods_sku as tmgs
        left join dkic_b.t_mst_goods_brand as tmgb on tmgs.brand_id = tmgb.brand_id
        left join dkic_b.t_mst_goods_category as tmgc on tmgs.category_id = tmgc.cat_id
        left join dkic_b.t_mst_goods_series as tmgseries on tmgs.series_id = tmgseries.series_id
        left join dkic_b.t_mst_unit as ul on tmgs.unit_id = ul.unit_id
        left join dkic_b.t_mst_unit as u2 on tmgs.sub_unit_id = u2.unit_id
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
        <if test="categoryId != null">
            AND tmgs.category_id = #{categoryId}::uuid
        </if>
    </select>

    <!-- 获取商品（其他入库开单用）（数量） -->
    <select id="getGoodsForIntoCountByPage" resultType="Long">
        SELECT
        count(1)
        from dkic_b.t_mst_goods_sku as tmgs
        where tmgs.flg_valid
        AND tmgs.cp_id = #{cpId}
        <if test="skuCode != null">
            AND tmgs.sku_code LIKE concat('%',my_ex.likequery(#{skuCode}),'%')
        </if>
        <if test="skuName != null">
            AND tmgs.sku_name LIKE concat('%',my_ex.likequery(#{skuName}),'%')
        </if>
    </select>
    <select id="selectDictionaryData" resultType="java.util.Map">
        select dd.data_id     as "dataId",
               dd.data_id     as "id",
               dd.dict_code   as "dictCode",
               dd.data_code   as "dataCode",
               dd.data_value  as "dataValue",
               dd.flg_default as "flgDefault"
        from dkic_b.t_mst_dictionary_data as dd
        where dd.flg_valid
          and dd.cp_id = #{cpId}
          and dd.dict_code = #{dictCode}
          and dd.data_value = #{dataValue}
    </select>

    <!-- 销售分析-我的 门店助手的 销售金额 收款金额 库存成本 -->
    <select id="getHomeOrderRecCost" resultType="java.util.Map">
        select dkic_b.f_get_home_order_rec_cost(#{cpId},#{dateBegin}::date,#{dateEnd}::date,#{staffId}::uuid)
    </select>

    <!--查询用户授权数-->
    <select id="selectUserCount" resultType="java.util.Map">
        SELECT
            (
                SELECT COUNT
                           ( 1 ) "webNum"
                FROM
                    dkic_b.t_mst_staff s
                WHERE
                    s.flg_valid
                  AND s.flg_can_login
                  AND s.login_type IN ( 1, 3 )
                  AND s.hr_status = 1
                  AND s.cp_id = #{cpId}
            ),
            (
                SELECT COUNT
                           ( 1 ) "wxNum"
                FROM
                    dkic_b.t_mst_staff s
                WHERE
                    s.flg_valid
                  AND s.flg_can_login
                  AND s.hr_status = 1
                  AND s.login_type IN ( 2, 3 )
                  AND s.cp_id = #{cpId}
            ),
            CURRENT_DATE as "currentDate"
    </select>

    <!--查询公司-->
    <select id="getCompany" resultType="java.util.Map">
        select t.cp_id         as "cpId",
               t.cp_code       as "cpCode",
               t.cp_name       as "cpName",
               t.svc_code      as "svcCode",
               t.opening_date  as "openingDate",
               t.grade_code    as "gradeCode",
               t.end_date      as "endDate",
               t.max_staff_num as "maxStaffNum",
               t.flg_valid     as "flgValid",
               t.remarks,
               t.owner
        from dkic_a.t_a_company as t
        where t.cp_id = #{cpId}
    </select>

    <!--查询首页实时数据1-->
    <select id="getRealTimeData1" resultType="java.util.Map">
        select count(1) as "orderQty", coalesce(sum(t.sum_amount), 0) as "orderAmt"
        from dkic_b.t_psi_order as t
        where t.flg_valid
          and t.cp_id = #{cpId}
          and t.op_create_time <![CDATA[ >= ]]> #{createTimeStart}
          and t.op_create_time <![CDATA[ < ]]> #{createTimeEnd}
    </select>

    <!--查询首页实时数据2-->
    <select id="getRealTimeData2" resultType="java.util.Map">
        select count(1) as "outQty", coalesce(sum(t.out_amt), 0) as "outAmt"
        from dkic_b.t_psi_outbound as t
        where t.flg_valid
          and t.out_type = '出库类型-销售出库'
          and t.cp_id = #{cpId}
          and t.op_create_time <![CDATA[ >= ]]> #{createTimeStart}
          and t.op_create_time <![CDATA[ < ]]> #{createTimeEnd}
    </select>

    <!--查询首页实时数据3-->
    <select id="getRealTimeData3" resultType="java.util.Map">
        select count(1) as "returnQty", -coalesce(sum(t.out_amt), 0) as "returnAmt"
        from dkic_b.t_psi_outbound as t
        where t.flg_valid
          and t.out_type = '出库类型-销退回库'
          and t.cp_id = #{cpId}
          and t.op_create_time <![CDATA[ >= ]]> #{createTimeStart}
          and t.op_create_time <![CDATA[ < ]]> #{createTimeEnd}
    </select>

    <!--查询首页实时数据4-->
    <select id="getRealTimeData4" resultType="java.util.Map">
        select count(1) as "cusQty", coalesce(sum(t.sum_amount), 0) as "cusAmt"
        from (select sum(t.sum_amount) as sum_amount
              from dkic_b.t_psi_order as t
              where t.flg_valid
                and t.cp_id = #{cpId}
                and t.op_create_time <![CDATA[ >= ]]> #{createTimeStart}
                and t.op_create_time <![CDATA[ < ]]> #{createTimeEnd}
              group by t.cus_id) as t
    </select>
</mapper>
