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

import cn.hnthyy.thmz.entity.his.mz.MzyZdChargeType;
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 MzyZdChargeTypeMapper {
    /**
     * 查询所有的号别
     * @return
     */
    @Select("SELECT rtrim(code) code,rtrim(name) name,rtrim(py_code) py_code,rtrim(d_code) self_code,req_fee,clinic_fee,oth_fee,del_flag,rtrim(req_type) req_type,sort_no,print_flag FROM mzy_zd_charge_type where (del_flag  <> 1  or del_flag is null )")
    List<MzyZdChargeType> selectAllMzyZdChargeType();

    /**
     * 根据号别的编号查询号别
     * @param code
     * @return
     */
    @Select("SELECT rtrim(code) code,rtrim(name) name,rtrim(py_code) py_code,rtrim(d_code) self_code,req_fee,clinic_fee,oth_fee,del_flag,rtrim(req_type) req_type,sort_no,print_flag FROM mzy_zd_charge_type where code =#{code} ")
    MzyZdChargeType selectByCode(@Param("code") String code);


    /**
     * 按照号别分类查询号别数量
     * @param reqType
     * @return
     */
    @Select("select count(1) from mzy_zd_charge_type where req_type=#{reqType} and (del_flag  <> 1  or del_flag is null )")
    int selectCountByReqType(@Param("reqType") String reqType);

//    /**
//     * 删除号别
//     * @param code
//     * @return
//     */
//    @Update("update mzy_zd_charge_type set del_flag=1 where  code=#{code}")
//    int deleteMzyZdChargeType(@Param("code") String code);

    /**
     * 查询新的编码
     * @return
     */
    @Select("select max(code)+1 from mzy_zd_charge_type ")
    String selectNewCode();


    /**
     * 更新号别
     * @param mzyZdChargeType
     * @return
     */
    @Update({"<script>",
            "update mzy_zd_charge_type " +
             "<trim prefix='set' prefixOverrides=',' suffix=' where code=#{code}' >",
            "<when test='name!=null'>",
            ",name =#{name}",
            "</when>",
            "<when test='pyCode!=null'>",
            ",py_code =#{pyCode}",
            "</when>",
            "<when test='selfCode!=null'>",
            ",d_code =#{selfCode}",
            "</when>",
            "<when test='reqFee!=null'>",
            ",req_fee =#{reqFee}",
            "</when>",
            "<when test='clinicFee!=null'>",
            ",clinic_fee =#{clinicFee}",
            "</when>",
            "<when test='othFee!=null'>",
            ",oth_fee =#{othFee}",
            "</when>",
            "<when test='reqType!=null'>",
            ",req_type =#{reqType}",
            "</when>",
            "<when test='printFlag!=null'>",
            ",print_flag =#{printFlag}",
            "</when>",
            "<when test='delFlag!=null'>",
            ",del_flag =#{delFlag}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzyZdChargeType(MzyZdChargeType mzyZdChargeType);

    /**
     * 新增号别
     * @param mzyZdChargeType
     * @return
     */
    @Insert("INSERT INTO mzy_zd_charge_type(code, name, py_code, d_code, req_fee, telreq_fee, clinic_fee, oth_fee, brochure_fee, " +
            "back_fee, del_flag, req_type, sort_no, print_flag, qm_flag) VALUES" +
            " (#{code}, #{name}, #{pyCode}, #{selfCode}, #{reqFee}, 0, #{clinicFee}, #{othFee}, 0, 0, #{delFlag}, #{reqType}, #{sortNo}, '1', NULL)")
    int insetMzyZdChargeType(MzyZdChargeType mzyZdChargeType);
}
