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

import cn.hnthyy.thmz.entity.his.zd.ZdChargeItem;
import cn.hnthyy.thmz.pageDto.ZdChargeItemDto;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ZdChargeItemMapper {


    /**
     * 分页查询收费项目字典列表
     *
     * @param zdChargeItemDto 收费项目查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT top ${pageSize} code,name,exec_unit,audit_code,charge_amount,effective_date,effective_amount,percentag1,percentag2,percentag3,percentag4,percentag5," +
                    "            rtrim(self_flag) self_flag,rtrim(separate_flag) separate_flag,rtrim(suppress_flag) suppress_flag,py_code,d_code wb_code,bill_item_zy,bill_item_mz," +
                    "            charge_unit,percent_group_zy,percent_group_mz,rtrim(case  when in_out_flag ='' then null else in_out_flag end) in_out_flag,n_code yn_code,class_code,s_code bz_code,discription,rtrim(del_flag) del_flag," +
                    "            rtrim(yp_flag) yp_flag,bill_item_code,rtrim(xnh_flag) xnh_flag,add_date,hnsyb_code,hnsyb_name,xnh_code,xnh_name,xnh_ratio,xnh_quotamoney,rtrim(xnh_status) xnh_status," +
                    "            yb_flag_new,yb_comment_new,yb_bl_new,charge_amount_lwg,percent_group_1  percent_group1,yb_name,yb_comment,cl_code,e_code yb_code,co,xl_code FROM (SELECT ROW_NUMBER() OVER (ORDER BY zd_charge_item_page.add_date desc) ",
            "AS RowNumber,* from dbo.zd_charge_item zd_charge_item_page where 1=1 ",
            "<when test='zdChargeItem.delFlag!=null'>",
            " and del_flag =#{zdChargeItem.delFlag}",
            "</when>",
            "<when test='zdChargeItem.name!=null'>",
            " and (name like #{zdChargeItem.name} or code like #{zdChargeItem.name} or n_code like #{zdChargeItem.name})",
            "</when>",
/*            "<when test='zdChargeItem.code!=null'>",
            " and code = #{zdChargeItem.code}",
            "</when>",*/
            "<when test='zdChargeItem.classCode!=null'>",
            " and class_code =#{zdChargeItem.classCode}",
            "</when>",
            "<when test='zdChargeItem.auditCode!=null'>",
            " and audit_code =#{zdChargeItem.auditCode}",
            "</when>",
            "<when test='zdChargeItem.billItemZy!=null'>",
            " and bill_item_zy =#{zdChargeItem.billItemZy}",
            "</when>",
            "<when test='zdChargeItem.billItemMz!=null'>",
            " and bill_item_mz =#{zdChargeItem.billItemMz}",
            "</when>",
 /*           "<when test='zdChargeItem.ynCode!=null'>",
            " and n_code =#{zdChargeItem.ynCode}",
            "</when>",*/
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"})
    List<ZdChargeItem> selectZdChargeItemWithPage(ZdChargeItemDto zdChargeItemDto);


    /**
     * 统计收费项目字典
     *
     * @param zdChargeItem 收费项目查询条件
     * @return
     */
    @Select({"<script>",
            "SELECT count(1) from dbo.zd_charge_item where 1=1 ",
            "<when test='delFlag!=null'>",
            " and del_flag =#{delFlag}",
            "</when>",
            "<when test='name!=null'>",
            " and (name like #{name} or code like #{name} or n_code like #{name})",
            "</when>",
    /*        "<when test='code!=null'>",
            " and code = #{code}",
            "</when>",*/
            "<when test='classCode!=null'>",
            " and class_code =#{classCode}",
            "</when>",
            "<when test='auditCode!=null'>",
            " and audit_code =#{auditCode}",
            "</when>",
            "<when test='billItemZy!=null'>",
            " and bill_item_zy =#{billItemZy}",
            "</when>",
            "<when test='billItemMz!=null'>",
            " and bill_item_mz =#{billItemMz}",
            "</when>",
/*            "<when test='ynCode!=null'>",
            " and n_code =#{ynCode}",
            "</when>",*/
            "</script>"})
    int countZdChargeItem(ZdChargeItem zdChargeItem);


    /**
     * 根据单个收费项目编码查询字典
     *
     * @param code
     * @return
     */
    @Select("select code,name,exec_unit,audit_code,charge_amount,effective_date,effective_amount,percentag1,percentag2,percentag3,percentag4,percentag5," +
            "rtrim(self_flag) self_flag,rtrim(separate_flag) separate_flag,rtrim(suppress_flag) suppress_flag,py_code,d_code wb_code,bill_item_zy,bill_item_mz," +
            "charge_unit,percent_group_zy,percent_group_mz,rtrim(case  when in_out_flag ='' then null else in_out_flag end) in_out_flag,n_code yn_code,class_code,s_code bz_code,discription,rtrim(del_flag) del_flag, " +
            "rtrim(yp_flag) yp_flag,bill_item_code,rtrim(xnh_flag) xnh_flag,add_date,hnsyb_code,hnsyb_name,xnh_code,xnh_name,xnh_ratio,xnh_quotamoney,rtrim(xnh_status) xnh_status," +
            "yb_flag_new,yb_comment_new,yb_bl_new,e_code yb_code,cl_code,co,xl_code from zd_charge_item where  code = #{code,jdbcType=VARCHAR} ")
    //     " and (del_flag != 1 or del_flag is null) " )
    ZdChargeItem selectZdChargeItemByCode(@Param("code") String code);


    /**
     * 根据收费项目编码集合查询所包含的收费项目
     *
     * @param codes
     * @return
     */
    @Select({"<script>",
            "select code,name,exec_unit,audit_code,charge_amount,effective_date,effective_amount,percentag1,percentag2,percentag3,percentag4,percentag5,",
            "rtrim(self_flag) self_flag,rtrim(separate_flag) separate_flag,rtrim(suppress_flag) suppress_flag,py_code,d_code wb_code,bill_item_zy,bill_item_mz,",
            "charge_unit,percent_group_zy,percent_group_mz,rtrim(case  when in_out_flag ='' then null else in_out_flag end) in_out_flag,n_code yn_code,class_code,s_code bz_code,discription,rtrim(del_flag) del_flag, ",
            "rtrim(yp_flag) yp_flag,bill_item_code,rtrim(xnh_flag) xnh_flag,add_date,hnsyb_code,hnsyb_name,xnh_code,xnh_name,xnh_ratio,xnh_quotamoney,rtrim(xnh_status) xnh_status,",
            // "yb_flag_new,yb_comment_new,yb_bl_new from zd_charge_item where (del_flag != 1 or del_flag is null) ",
            "yb_flag_new,yb_comment_new,yb_bl_new,e_code yb_code,cl_code,co,xl_code from zd_charge_item  ",
            "<when test='codes!=null'>",
            " where code in",
            "<foreach item='item' index='index' collection='codes' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</when>",
            "</script>"})
    List<ZdChargeItem> selectZdChargeItemByCodeList(@Param("codes") List<String> codes);


    /**
     * 查询所有的核算码名称
     *
     * @return
     */
    @Select("select distinct bb.name from zd_charge_item aa,zy_zd_audit_item bb where aa.audit_code = bb.code union select distinct dd.name from yp_zd_dict cc, yp_zd_drug_kind dd where cc.drug_kind = dd.code ")
    List<String> selectAllHsmName();


    /**
     * 新增收费项目
     *
     * @param zdChargeItem
     * @return
     */
    @Insert("INSERT INTO zd_charge_item ( code, name, charge_amount, percentag1, percentag2, percentag3, percentag4, percentag5, self_flag, separate_flag," +
            " suppress_flag, py_code, d_code, del_flag, charge_unit, percent_group_mz, class_code, e_code, s_code, add_date, xnh_code, xnh_name,audit_code," +
            "bill_item_mz,bill_item_zy,exec_unit,discription,cl_code,co,oper_id,xl_code ) VALUES (" +
            " #{code}, #{name}, #{chargeAmount}, #{percentag1}, #{percentag2}, #{percentag3}, #{percentag4}, #{percentag5}, #{selfFlag}, #{separateFlag}, " +
            "#{suppressFlag}, #{pyCode}, #{wbCode}, #{delFlag}, #{chargeUnit}, #{percentGroupMz}, #{classCode}, #{ybCode}, #{bzCode}, #{addDate}, #{xnhCode}, #{xnhName}" +
            ",#{auditCode},#{billItemMz},#{billItemZy},#{execUnit} ,#{discription}, #{clCode},#{co},#{operId},#{xlCode}  )")
    int insertZdChargeItem(ZdChargeItem zdChargeItem);


    /**
     * 修改收费项目
     *
     * @param zdChargeItem
     * @return
     */
    @Update({"<script>",
            "update zd_charge_item",
            "<trim prefix='set' prefixOverrides=',' suffix=' where code = #{code} '>",
            "<when test='classCode!=null'>",
            "class_code =#{classCode}",
            "</when>",
            "<when test='ybCode!=null'>",
            ",e_code =#{ybCode}",
            "</when>",
            "<when test='xnhCode!=null'>",
            ",xnh_code =#{xnhCode}",
            "</when>",
            "<when test='xnhName!=null'>",
            ",xnh_name =#{xnhName}",
            "</when>",
            "<when test='name!=null'>",
            ",name =#{name}",
            "</when>",
            "<when test='chargeUnit!=null'>",
            ",charge_unit =#{chargeUnit}",
            "</when>",
            "<when test='chargeAmount!=null'>",
            ",charge_amount =#{chargeAmount}",
            "</when>",
            "<when test='percentag1!=null'>",
            ",percentag1 =#{percentag1}",
            "</when>",
            "<when test='percentag2!=null'>",
            ",percentag2 =#{percentag2}",
            "</when>",
            "<when test='percentag3!=null'>",
            ",percentag3 =#{percentag3}",
            "</when>",
            "<when test='percentag4!=null'>",
            ",percentag4 =#{percentag4}",
            "</when>",
            "<when test='percentag5!=null'>",
            ",percentag5 =#{percentag5}",
            "</when>",
            "<when test='effectiveDate!=null'>",
            ",effective_date =#{effectiveDate}",
            "</when>",
            "<when test='auditCode!=null'>",
            ",audit_code =#{auditCode}",
            "</when>",
            "<when test='inOutFlag!=null'>",
            ",in_out_flag =#{inOutFlag}",
            "</when>",
            "<when test='billItemMz!=null'>",
            ",bill_item_mz =#{billItemMz}",
            "</when>",
            "<when test='billItemZy!=null'>",
            ",bill_item_zy =#{billItemZy}",
            "</when>",
            "<when test='execUnit!=null'>",
            ",exec_unit =#{execUnit}",
            "</when>",
            "<when test='bzCode!=null'>",
            ",s_code =#{bzCode}",
            "</when>",
            "<when test='discription!=null'>",
            ",discription =#{discription}",
            "</when>",
            "<when test='clCode!=null'>",
            ",cl_code =#{clCode}",
            "</when>",
            "<when test='delFlag!=null'>",
            ",del_flag =#{delFlag}",
            "</when>",
            "<when test='co!=null'>",
            ",co =#{co}",
            "</when>",
            "<when test='xlCode!=null'>",
            ",xl_code =#{xlCode}",
            "</when>",
            "<when test='operId!=null'>",
            ",oper_id =#{operId}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateZdChargeItem(ZdChargeItem zdChargeItem);


    /**
     * 查询所有生效的收费项目字典列表
     *
     * @return
     */
    @Select({"<script>",
            "SELECT  code,name,exec_unit,audit_code,charge_amount,effective_date,effective_amount,percentag1,percentag2,percentag3,percentag4,percentag5," +
                    "            rtrim(self_flag) self_flag,rtrim(separate_flag) separate_flag,rtrim(suppress_flag) suppress_flag,py_code,d_code wb_code,bill_item_zy,bill_item_mz," +
                    "            charge_unit,percent_group_zy,percent_group_mz,rtrim(case  when in_out_flag ='' then null else in_out_flag end) in_out_flag,n_code yn_code,class_code,s_code bz_code,discription,rtrim(del_flag) del_flag," +
                    "            rtrim(yp_flag) yp_flag,bill_item_code,rtrim(xnh_flag) xnh_flag,add_date,hnsyb_code,hnsyb_name,xnh_code,xnh_name,xnh_ratio,xnh_quotamoney,rtrim(xnh_status) xnh_status," +
                    "            yb_flag_new,yb_comment_new,yb_bl_new,charge_amount_lwg,percent_group_1  percent_group1,yb_name,yb_comment,cl_code,e_code yb_code,co,xl_code FROM zd_charge_item ",
            " where del_flag =0 ",
            "</script>"})
    List<ZdChargeItem> selectAllInUsedZdCharge();


}
