package thyyxxk.webserver.dao.his.medicine;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
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.CodeName;
import thyyxxk.webserver.entity.medicine.YpZdSupply;

import java.util.List;

@Mapper
public interface YpZdSupplyDao extends BaseMapper<YpZdSupply> {

    @Select(" select sy.* from yp_zd_supply as sy " +
            " where sy.code like '%${text}%' or sy.name like '%${text}%' " +
            " or sy.py_code like ('%' + upper(#{text}) + '%') or sy.d_code like ('%' + upper(#{text}) + '%') " +
            " order by sy.code ")
    List<YpZdSupply> selectYpSupply(@Param("text") String text);

    @Select(" select * from yp_zd_supply where code = #{code} ")
    YpZdSupply selectYpSupplyByCode(@Param("code") String code);

    @Update("<script>" +
            "update yp_zd_supply " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"code != null and code != '' \">" +
            "code = #{code}, " +
            "</if>" +
            "<if test=\"name != null and name != '' \">" +
            "name = #{name}, " +
            "</if>" +
            "<if test=\"pyCode != null and pyCode != '' \">" +
            "py_code = #{pyCode}, " +
            "</if>" +
            "<if test=\"dCode != null and dCode != '' \">" +
            "d_code = #{dCode}, " +
            "</if>" +
            "<if test=\"delFlag != null and delFlag != '' \">" +
            "del_flag = #{delFlag}, " +
            "</if>" +
            "<if test=\"abbrName != null and abbrName != '' \">" +
            "abbr_name = #{abbrName}, " +
            "</if>" +
            "<if test=\"address != null and address != '' \">" +
            "address = #{address}, " +
            "</if>" +
            "<if test=\"zipCode != null and zipCode != '' \">" +
            "zip_code = #{zipCode}, " +
            "</if>" +
            "<if test=\"telNo != null and telNo != '' \">" +
            "tel_no = #{telNo}, " +
            "</if>" +
            "<if test=\"relName != null and relName != '' \">" +
            "rel_name = #{relName}, " +
            "</if>" +
            "<if test=\"busiRange != null and busiRange != '' \">" +
            "busi_range = #{busiRange}, " +
            "</if>" +
            "<if test=\"comment != null and comment != '' \">" +
            "comment = #{comment}, " +
            "</if>" +
            "<if test=\"useFlag != null and useFlag != '' \">" +
            "use_flag = #{useFlag}, " +
            "</if>" +
            "<if test=\"supplyCode != null and supplyCode != '' \">" +
            "supply_code = #{supplyCode}, " +
            "</if>" +
            "</trim>" +
            "where code = #{code} " +
            "</script>")
    int updateYpSupplyByCode(YpZdSupply ypZdSupply);

    @Delete(" delete from yp_zd_supply where code = #{code} ")
    int deleteYpSupplyByCode(@Param("code") String code);

    @Select("select rtrim(code) as code, rtrim(name) as name, isnull(del_flag, '0') as statusFlag, " +
            " case when del_flag = '1' then 'true' else 'false' end as disabled " +
            "from yp_zd_supply with(nolock) order by code ")
    List<CodeName> selectYpSupplyCode();

}
