package thyyxxk.webserver.dao.his.dictionary;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.dictionary.DoctorBaseZd;
import thyyxxk.webserver.entity.executeItem.ZdChargeItem;

import java.util.List;

/**
 * @Description: 医生基础字典维护
 * @Author:lihong
 * @Date: 2025/5/20
 */
@Mapper
public interface DoctorBaseZdDao {
    /**
     * @description: 查询  jy_zd_tube , ysh_zd_ss_type 字典
     * @author: lihong
     * @date: 2025/5/20 9:07
     * @param: tableName
     * @param: keyWard
     * @return: java.util.List<thyyxxk.webserver.entity.dictionary.DoctorBaseZd>
     **/
    @Select("<script>" +
            " select code,code new_code, name, py_code, d_code dCode, case when del_flag is null or del_flag ='' then '0' else del_flag end del_flag " +
            " from ${tableName} " +
            " <where>" +
            " <if test=\" keyWard !=null and keyWard !='' \">" +
            "<bind name=\"keyWardPatten\" value=\"'%'+keyWard+'%'\" />"+
            " (code like #{keyWardPatten} or name like #{keyWardPatten} or py_code like #{keyWardPatten} or d_code like #{keyWardPatten}) " +
            " </if>"+
            "<if test=\" delFlag =='0'.toString \">"+
            " and (del_flag is null or del_flag ='' or del_flag ='0') "+
            "</if>"+
            "<if test=\" delFlag =='1'.toString \">"+
            " and del_flag ='1' "+
            "</if>"+
            " </where>" +
            "</script>")
    List<DoctorBaseZd> selectDoctorBaseZd(DoctorBaseZd doctorBaseZd);
    /**
     * @description: 更新删除标识
     * @author: lihong
     * @date: 2025/5/20 9:06
     * @param: doctorBaseZd
     * @return: int
     **/
    @Update(" update ${tableName} set del_flag = #{delFlag,jdbcType = CHAR} where code = #{code,jdbcType = CHAR}")
    int updateDelFlag(DoctorBaseZd doctorBaseZd);

    @Insert(" insert into  ${tableName} (code, name, del_flag, py_code, d_code) values (#{newCode},#{name},#{delFlag},#{pyCode},#{dCode}) ")
    int insertDoctorBaseZd(DoctorBaseZd doctorBaseZd);
    @Insert(" <script>" +
            "insert into  jy_zd_tube_charge (code,charge_code,amount) values " +
            " <foreach collection='list' item='item' separator=','>" +
            " (#{item.code}, #{item.chargeCode}, #{item.amount})" +
            " </foreach>" +
            "</script>"
    )
    int batchJyZdTubeCharge(List<DoctorBaseZd> list);
    @Select(" select a.code, " +
            "       a.charge_code, " +
            "       a.amount, " +
            "       b.name, " +
            "       b.charge_amount, " +
            "       case when b.del_flag is null or b.del_flag ='' then '0' else b.del_flag end del_flag " +
            " from jy_zd_tube_charge a,zd_charge_item b " +
            " where a.charge_code = b.code and " +
            "        a.code = #{code} ")
    List<DoctorBaseZd> selectJyZdTubeChargeByCode(String code);
    @Select(" select count(*) from  ${tableName} where  code = #{code}")
    Integer countBycode(@Param("code") String code,@Param("tableName")String tableName);
    @Update(" update ${tableName} set name =#{name},code = #{newCode},py_code=#{pyCode},d_code = #{dCode} where code = #{code}")
    int updateDoctorBaseZd(DoctorBaseZd param);
    @Delete("<script>" +
            " delete from jy_zd_tube_charge where code =#{code}" +
            " <if test=\" chargeCode !=null and chargeCode!='' \">" +
            " and charge_code = #{chargeCode}"+
            " </if>" +
            "</script>")
    int deleteJyZdTubeCharge(@Param("code") String code, @Param("chargeCode")String chargeCode);
    @Select(" <script> " +
            "select  code,name,charge_unit,charge_amount,self_flag,separate_flag,suppress_flag  " +
            " from zd_charge_item  " +
            " where (del_flag is null  " +
            "    or del_flag = ''  " +
            "    or del_flag = '0')  " +
            "<if test=\" param.keyWard !=null and param.keyWard!='' \">"+
            " <bind name=\"keyWardPatten\" value=\"'%'+param.keyWard+'%'\" />"+
            "  and (code like #{keyWardPatten} or name like #{keyWardPatten} or py_code like #{keyWardPatten} or d_code like #{keyWardPatten})" +
            " </if>" +
            " </script> " )
    List<ZdChargeItem> selectZdItemChargePage(@Param("page")IPage<ZdChargeItem> page, @Param("param")DoctorBaseZd param);
}
