package cn.hnthyy.thmz.mapper.his.mz;

import cn.hnthyy.thmz.entity.his.mz.JcJyItemCharge;
import cn.hnthyy.thmz.entity.his.mz.JyZdItem;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface JcJyItemChargeMapper {
    /**
     * 根据单个编号查询收费编码
     *
     * @param code
     * @param reqType
     * @param tableName
     * @return
     */
    @Select("select rtrim(code) code,rtrim(charge_code) charge_code,amount,rtrim(charge_type) charge_type,rtrim(tc_no) tc_no,rtrim(zy_flag) zy_flag," +
            "rtrim(mz_flag) mz_flag,${reqType} as req_type from ${tableName} where code=#{code,jdbcType=CHAR}")
    JcJyItemCharge selectJcItemChargeByCode(@Param("code") String code, @Param("reqType") String reqType, @Param("tableName") String tableName);

    /**
     * 根据项目编码集合查询所含的收费编码集合
     *
     * @param codes
     * @param reqType
     * @param tableName
     * @return
     */
    @Select({"<script>",
            "select rtrim(code) code,rtrim(charge_code) charge_code,amount,rtrim(charge_type) charge_type,rtrim(tc_no) tc_no,rtrim(zy_flag) zy_flag,",
            "rtrim(mz_flag) mz_flag,${reqType} as req_type from ${tableName} where  charge_code <![CDATA[<>]]> '' and charge_code is not null ",
            "<when test='codes!=null'>",
            " and code in",
            "<foreach item='item' index='index' collection='codes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "</script>"})
    List<JcJyItemCharge> selectJcItemChargeByCodeList(@Param("codes") List<String> codes, @Param("reqType") String reqType, @Param("tableName") String tableName);

    /**
     * 查询诊疗项目  检验和检查
     *
     * @param commonParams
     * @return
     */
    @Select({"<script>",
            " SELECT ",
            "<when test='commonParams==null'>",
            " top 20 ",
            "</when>",
            "code,name,py_code,d_code wb_code,class ,exec_unit,type=1,rtrim(sample_code) sample_code,bw_code='' ",
            "    FROM jy_zd_item ",
            "where  isnull(del_flag,'') <![CDATA[<>]]> '1' ",
            "<when test='commonParams!=null'>",
            "and (py_code like #{commonParams} or ",
            "         d_code  like #{commonParams} or ",
            "         code    like #{commonParams} or ",
            "         name    like #{commonParams})  ",
            "</when>",
            " UNION ALL ",
            "    SELECT ",
            "<when test='commonParams==null'>",
            " top 20 ",
            "</when>",
            "code,name,py_code,d_code wb_code,class ,exec_unit,type=2,sample_code='',rtrim(bw_code) bw_code ",
            "    FROM jc_zd_item   ",
            "where  isnull(del_flag,'') <![CDATA[<>]]> '1' ",
            "<when test='commonParams!=null'>",
            "and (py_code like #{commonParams} or ",
            "         d_code  like #{commonParams} or ",
            "         code    like #{commonParams} or ",
            "         name    like #{commonParams})  ",
            "</when>",
            "</script>"})
    List<JyZdItem> selectJcJyItemByCommonParams(@Param("commonParams") String commonParams);

    /**
     * 诊疗项目查询
     * @param commonParams
     * @return
     */
    @Select({"<script>",
            " SELECT distinct ",
            "<when test='commonParams==null'>",
            " top 20 ",
            "</when>",
            "code,name,specification,charge_amount= charge_amount1,yb_comment ",
            "    FROM view_mz_zd_zl(nolock)  ",
            "where  (case when bill_code='TC' then '00' else group_no end)='00' " +
                    " and (" +
                    "(case when bill_code='TC' then exec_unit else '1040000' end)='1040000'  or (case when bill_code='TC' then op_id else '00000' end)='00000' or order_type='3'" +
                    ")  " +
                    " and code not in (select mz_code from jy_zd_item) ",
            "<when test='commonParams!=null'>",
            "and (py_code like #{commonParams} or ",
            "         d_code  like #{commonParams} or ",
            "         code    like #{commonParams} or ",
            "         name    like #{commonParams} or " +
                    "         isnull(n_code,'') like #{commonParams})  ",
            "</when>",
            "</script>"})
    List<JyZdItem> selectZlByCommonParams(@Param("commonParams") String commonParams);

    /**
     * 根据检查检验编码查询对应的明细项目
     *
     * @param code
     * @return
     */
    @Select("select rtrim(code) code,rtrim(charge_code) charge_code,amount,rtrim(charge_type) charge_type,rtrim(tc_no) tc_no,rtrim(zy_flag) zy_flag," +
            "rtrim(mz_flag) mz_flag from jc_item_charge where  charge_code <> '' and charge_code is not null and code=#{code} UNION ALL " +
            "select rtrim(code) code,rtrim(charge_code) charge_code,amount,rtrim(charge_type) charge_type,rtrim(tc_no) tc_no,rtrim(zy_flag) zy_flag," +
            "rtrim(mz_flag) mz_flag from jy_item_charge where  charge_code <> '' and charge_code is not null and code=#{code} ")
    List<JcJyItemCharge> selectJcJyItemChargeByCode(@Param("code") String code);


}
