<?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.hzya.frame.sysnew.comparison.dao.impl.ComparisonDaoImpl">
    <resultMap id="get-ComparisonEntity-result" type="com.hzya.frame.sysnew.comparison.entity.ComparisonEntity" >
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="sorts" column="sorts" jdbcType="INTEGER"/>
        <result property="create_user_id" column="create_user_id" jdbcType="VARCHAR"/>
        <result property="create_time" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="modify_user_id" column="modify_user_id" jdbcType="VARCHAR"/>
        <result property="modify_time" column="modify_time" jdbcType="TIMESTAMP"/>
        <result property="sts" column="sts" jdbcType="VARCHAR"/>
        <result property="org_id" column="org_id" jdbcType="VARCHAR"/>
        <result property="companyId" column="company_id" jdbcType="VARCHAR"/>
    </resultMap>

    <!--分页查询数据-->
    <select id="queryComparisonPage" resultType="java.util.Map"
            parameterType = "java.util.Map">
       select

        <if test="returnField != null and returnField.size>0">
            <trim suffixOverrides=",">
                <foreach collection="returnField" item="returnField">
                    a.${returnField} as ${returnField} ,
                </foreach>
            </trim>
        </if>
        <if test="returnField == null or returnField.size == 0">

            <foreach item="field" collection="fields"   separator=","  >
                a.${field}
            </foreach>
        </if>

       from ${dbName} a
        <trim prefix="where" prefixOverrides="and">
            <foreach collection="map.entrySet()" item="value" index="key">
                and ${key}  like concat('%',#{value},'%')
            </foreach>
           and sts='Y' and (data_status='Y' or data_status='F')
        </trim>
    </select>

    <!--根据主数据编码查询数据-->
    <select id="queryComparison" resultMap="get-ComparisonEntity-result"
            parameterType="com.hzya.frame.sysnew.comparison.entity.ComparisonEntity">
        select m.mdm_name as mdmName,m.mdm_code as mdmCode,moddb.db_name as dbName,
               moddb.db_type as dbType,filed.en_name as enName,filed.ch_name as chName,filed.filed_type as filedType,
               m.id as mId,moddb.id as dbId
        from mdm_module m
                 left join mdm_module_db moddb on m.id=moddb.mdm_id
                 left join mdm_module_db_fileds filed
                           on filed.mdm_id=m.id and filed.db_id=moddb.id
        where m.mdm_code= #{mdmCode} and (moddb.db_type='1' or moddb.db_type='2')
    </select>

    <!--根据主数据编码查询mdm_module的数据-->
    <select id="queryComparisonBymdmCode" resultMap="get-ComparisonEntity-result"
            parameterType="com.hzya.frame.sysnew.comparison.entity.ComparisonEntity">
        select mdm_name as mdmName,mdm_code as mdmCode,id as mId
        from mdm_module
        where mdm_code= #{mdmCode} and sts='Y'
    </select>

    <!--根据mdm_module的id查询mdm_module_db得数据-->
    <select id="queryComparisonBymdmId" resultMap="get-ComparisonEntity-result"
            parameterType="com.hzya.frame.sysnew.comparison.entity.ComparisonEntity">
        select db_name as dbName,db_type as dbType,id as dbId
        from mdm_module_db
        where mdm_id= #{mId} and sts='Y'
    </select>

    <!--根据mdm_module的id查询mdm_module_db_fileds得数据-->
    <select id="queryComparisonBymdmIdFileds" resultMap="get-ComparisonEntity-result"
            parameterType="com.hzya.frame.sysnew.comparison.entity.ComparisonEntity">
        select ch_name as chName,en_name as enName,filed_type as filedType,add_type as addType,
               update_type as updateType,show_type as showType, query_type as queryType,
               list_type as listType, view_type as viewType,mdm_id as mId,db_id as dbId
        from mdm_module_db_fileds
        where db_id= #{dbId} and sts='Y'
    </select>

    <!--根据mdm_module的id和mdm_module_db的id查询单据规则-->
    <select id="queryComparisonById" databaseId="oracle" resultMap="get-ComparisonEntity-result">
      select replace(WM_CONCAT(db_value),',','') as documentRule from mdm_table_code_rule
    where mdm_id=#{mId} and sts='Y'
    </select>

    <!--根据mdm_module的id和mdm_module_db的id查询单据规则-->
    <select id="queryComparisonById" databaseId="mysql" resultMap="get-ComparisonEntity-result">
          select group_concat(db_value SEPARATOR '') as documentRule from mdm_table_code_rule
            where mdm_id=#{mId} and sts='Y'
    </select>

<!--生成单据规则-->
    <select id="queryComparisonRule" resultMap="get-ComparisonEntity-result"
            parameterType="com.hzya.frame.sysnew.comparison.entity.ComparisonEntity">
        select
            concat(#{rulePrefix},'-',date_format(NOW(),#{ruleMiddle}),'-',LPAD((ifnull(max(substring_index(document_rule,'-',-1)+0),'0')+1),${ruleSuffix},'0')) as documentRule,
            substring_index(
            (concat(#{rulePrefix},'-',date_format(NOW(),#{ruleMiddle}),'-',LPAD((ifnull(max(substring_index(document_rule,'-',-1)+0),'0')+1),${ruleSuffix},'0'))),'-',-1
            )
             as documentRuleNum
        from  ${dbName} where document_rule like concat(#{rulePrefix},'-',date_format(NOW(),#{ruleMiddle}),'-','%');
    </select>

    <!--根据mdm_id查询mdm_module_db_fileds_rule中的字段是否是必填项-->
    <select id="queryComparisonBymdmIdDbIdFiledId" resultMap="get-ComparisonEntity-result"
            parameterType="com.hzya.frame.sysnew.comparison.entity.ComparisonEntity">
        select filed.ch_name as chName,filed.en_name as enName,filed.filed_type as filedType,
               rule.rule_value as ruleValue,rule.rule_code as ruleCode,db.db_name as dbName,db.db_type as dbType
        from mdm_module_db_fileds filed
                 left join mdm_module_db db on db.id=filed.db_id
                 left join mdm_module_db_fileds_rule rule on filed.id=rule.filed_id
        where filed.sts='Y' and rule.sts='Y' and db.sts='Y'
          and rule.rule_code='required' and rule.rule_value='true'
          and rule.mdm_id=#{mId}
    </select>

  <!--新增档案数据-->
    <insert id="saveComparison" parameterType = "java.util.Map">
        insert into ${dbName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <foreach collection="map.keySet()" item="column">
                ${column},
            </foreach>
        </trim>
          values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <foreach collection="map.values()" item="value">
                #{value},
            </foreach>
        </trim>
    </insert>

    <!--通过主键修改方法-->
    <update id="updateComparisonByType" parameterType = "java.util.Map" >
        update ${dbName}
        <set>
            <foreach collection="map.entrySet()" item="value" index="key" separator=",">
                ${key} =#{value}
            </foreach>
        </set>
        where id=#{id} and sts='Y' and (data_status='Y' or data_status='F')
    </update>


    <!-- 多条件逻辑删除 -->
    <update id="deleteComparison" parameterType = "java.util.Map" >
        update
        ${dbName}
        set data_status= 'N' ,modify_time = now(),modify_user_id = #{modify_user_id},delete_status='0'
        <trim prefix="where" prefixOverrides="and">
            <foreach collection="map.entrySet()" item="value" index="key">
               and ${key} =#{value}
            </foreach>
           and id=#{id} and sts='Y' and (data_status='Y' or data_status='F')
        </trim>
    </update>

</mapper>

