package thyyxxk.webserver.dao.his.dictionary;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.dictionary.ZdUnitCode;

import java.util.List;

@Mapper
public interface ZdUnitCodeDao extends BaseMapper<ZdUnitCode> {

    @Select("<script>" +
            "  select d.code, d.name, d.py_code as pyCode, d.d_code as dCode from zd_unit_code as d  " +
            "   where isnull(d.del_flag, '0') = '0' " +
            "    and (isnull(d.py_code,'') like '${text}%' " +
            "     or isnull(d.d_code,'') like '${text}%' " +
            "     or isnull(d.code,'') like '${text}%' " +
            "     or isnull(d.n_code,'') like '${text}%' " +
            "     or isnull(d.name,'') like '${text}%') " +
            "</script>")
    List<ZdUnitCode> selectZdUnitCode(@Param("text") String text);

    @Select("<script>" +
            "   select d.code,   " +
            "          d.name,   " +
            "          d.class_code,   " +
            "          c.name as className, " +
            "          d.py_code,   " +
            "          d.d_code,   " +
            "          d.mz_flag,   " +
            "          d.del_flag,  " +
            "          d.n_code, " +
            "          d.yj_flag, " +
            "          d.mzrc_flag, " +
            "          d.parent_code, " +
            "          f.name as parentName, " +
            "          '0' as new_flag, " +
            "          d.gh_charge_flag, " +
            "          d.xnh_dept_code, " +
            "          d.xnh_dept_name, " +
            "          d.supply_flag, " +
            "          d.ghjz_flag, " +
            "          d.office_pos " +
            "     from zd_unit_code d with(nolock) " +
            "left join zd_unit_class c " +
            "       on d.class_code = c.code  " +
            "left join zd_unit_code f with(nolock) " +
            "       on isnull(f.del_flag,'') &lt;&gt; '1' and d.parent_code = f.code " +
            "    where isnull(d.py_code,'') like ('%' + upper(#{text}) + '%') " +
            "      or isnull(d.d_code,'') like ('%' + upper(#{text}) + '%') " +
            "      or isnull(d.code,'') like ('%' + upper(#{text}) + '%') " +
            "      or isnull(d.n_code,'') like '%${text}%' " +
            "      or isnull(d.name,'') like '%${text}%' " +
            "</script>")
    List<ZdUnitCode> selectDeptDictAll(@Param("text") String text);

    @Select(" select u.code, u.name, u.py_code, u.d_code, n_code from zd_unit_code u where isnull(del_flag,'') <> '1' ")
    List<ZdUnitCode> selectDeptDictForEffective();

    @Select(" select u.* from zd_unit_code u where u.code = #{code} ")
    ZdUnitCode selectDeptDictByCode(@Param("code") String code);

    @Update("<script>" +
            "update zd_unit_code " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"code != null\">" +
            "code = #{code}, " +
            "</if>" +
            "<if test=\"name != null \">" +
            "name = #{name}, " +
            "</if>" +
            "<if test=\"classCode != null\">" +
            "class_code = #{classCode}, " +
            "</if>" +
            "<if test=\"pyCode != null\">" +
            "py_code = #{pyCode}, " +
            "</if>" +
            "<if test=\"dCode != null\">" +
            "d_code = #{dCode}, " +
            "</if>" +
            "<if test=\"mzFlag != null\">" +
            "mz_flag = #{mzFlag}, " +
            "</if>" +
            "<if test=\"delFlag != null\">" +
            "del_flag = #{delFlag}, " +
            "</if>" +
            "<if test=\"opId != null\">" +
            "op_id = #{opId}, " +
            "</if>" +
            "<if test=\"opDate != null\">" +
            "op_date = #{opDate}, " +
            "</if>" +
            "<if test=\"nCode != null\">" +
            "n_code = #{nCode}, " +
            "</if>" +
            "<if test=\"yjFlag != null\">" +
            "yj_flag = #{yjFlag}, " +
            "</if>" +
            "<if test=\"mzrcFlag != null\">" +
            "mzrc_flag = #{mzrcFlag}, " +
            "</if>" +
            "<if test=\"parentCode != null\">" +
            "parent_code = #{parentCode}, " +
            "</if>" +
            "<if test=\"ghChargeFlag != null\">" +
            "gh_charge_flag = #{ghChargeFlag}, " +
            "</if>" +
            "<if test=\"xnhDeptCode != null\">" +
            "xnh_dept_code = #{xnhDeptCode}, " +
            "</if>" +
            "<if test=\"xnhDeptName != null\">" +
            "xnh_dept_name = #{xnhDeptName}, " +
            "</if>" +
            "<if test=\"supplyFlag != null\">" +
            "supply_flag = #{supplyFlag}, " +
            "</if>" +
            "<if test=\"ghjzFlag != null\">" +
            "ghjz_flag = #{ghjzFlag}, " +
            "</if>" +
            "<if test=\"officePos != null\">" +
            "office_pos = #{officePos}, " +
            "</if>" +
            "</trim>" +
            "where code = #{code} " +
            "</script>")
    int updateDeptDictByCode(ZdUnitCode zdUnitCode);

    @Update(" update zd_unit_code set del_flag = #{delFlag} where code = #{code} ")
    int updateDeptDictDelFlagByCode(@Param("code") String code, @Param("delFlag") String delFlag);

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

}
