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

import cn.hnthyy.thmz.entity.his.zd.MzZdInstructionCode;
import cn.hnthyy.thmz.pageDto.MzZdInstructionCodePageDto;
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 MzZdInstructionCodeMapper {

    /**
     * 根据嘱托类型查询嘱托
     *
     * @param instructionType
     * @return
     */
    @Select("SELECT code,instruction_text,del_flag,py_code, d_code wb_code,instruction_type FROM mz_zd_instruction_code  where del_flag=0 and (instruction_type=2 or instruction_type=#{instructionType})")
    List<MzZdInstructionCode> selectMzZdInstructionByType(@Param("instructionType") Integer instructionType);

    /**
     * 查询嘱托
     * @param mzZdInstructionCodePageDto
     * @return
     */
    @Select({
            "<script>",
            " SELECT top ${pageSize} code,instruction_text,del_flag,py_code,wb_code,instruction_type",
            " FROM (SELECT ROW_NUMBER() OVER (ORDER BY mz_zd_instruction_code.code desc) ",
            " AS RowNumber,code,instruction_text,del_flag,py_code, d_code wb_code,instruction_type from mz_zd_instruction_code where 1=1 ",
            "<when test='mzZdInstructionCode.code!=null'>",
            " and code = #{mzZdInstructionCode.code}",
            "</when>",
            "<when test='mzZdInstructionCode.delFlag!=null'>",
            " and del_flag = #{mzZdInstructionCode.delFlag}",
            "</when>",
            "<when test='searchText!=null'>",
            " and (py_code like '${searchText}%' or d_code like '${searchText}%' or instruction_text  like '%${searchText}%')",
            "</when>",
            "<when test='mzZdInstructionCode.instructionType!=null'>",
            " and instruction_type = #{mzZdInstructionCode.instructionType}",
            "</when>",
            ") as A WHERE RowNumber >#{pageSize}*#{pageIndex} order by RowNumber asc",
            "</script>"
    })
    List<MzZdInstructionCode> selectMzZdInstructionPage(MzZdInstructionCodePageDto mzZdInstructionCodePageDto);

    /**
     * 统计嘱托数量
     *
     * @param mzZdInstructionCodePageDto 嘱托查询条件
     * @return
     */
    @Select({"<script>",
            "select count(1) from mz_zd_instruction_code where 1=1 ",
            "<when test='mzZdInstructionCode.code!=null'>",
            " and code = #{mzZdInstructionCode.code}",
            "</when>",
            "<when test='mzZdInstructionCode.delFlag!=null'>",
            " and del_flag = #{mzZdInstructionCode.delFlag}",
            "</when>",
            "<when test='mzZdInstructionCode.pyCode!=null'>",
            " and (py_code like '${searchText}%' or d_code like '${searchText}%' or instruction_text  like '%${searchText}%')",
            "</when>",
            "<when test='mzZdInstructionCode.instructionType!=null'>",
            " and instruction_type = #{mzZdInstructionCode.instructionType}",
            "</when>",
            "</script>"})
    int selectMzZdInstructionCount(MzZdInstructionCodePageDto mzZdInstructionCodePageDto);

    /**
     * 修改嘱托
     * @param mzZdInstructionCode
     * @return
     */
    @Update({"<script>",
            "update mz_zd_instruction_code",
            "<trim prefix='set' prefixOverrides=',' suffix=' where code = #{code} '>",
            "<when test='instructionText!=null'>",
            "instruction_text =#{instructionText}",
            "</when>",
            "<when test='delFlag!=null'>",
            ",del_flag =#{delFlag}",
            "</when>",
            "<when test='pyCode!=null'>",
            ",py_code =#{pyCode}",
            "</when>",
            "<when test='wbCode!=null'>",
            ",d_code =#{wbCode}",
            "</when>",
            "<when test='instructionType!=null'>",
            ",instruction_type =#{instructionType}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateMzZdInstructionCode(MzZdInstructionCode mzZdInstructionCode);

    /**
     * 根据排序码code修改停用状态
     * @param code
     * @param delFlag
     * @return
     */
    @Update("update mz_zd_instruction_code set del_flag=#{delFlag} where code=#{code}")
    int updateDelFlag(@Param("code") String code,@Param("delFlag") Integer delFlag);

    /**
     * 新增嘱托
     * @param mzZdInstructionCode
     * @return
     */
    @Insert("insert into mz_zd_instruction_code (code,instruction_text,del_flag,py_code,d_code,instruction_type) " +
            " values(#{code},#{instructionText},#{delFlag},#{pyCode},#{wbCode},#{instructionType})")
    int insertMzZdInstructionCode(MzZdInstructionCode mzZdInstructionCode);

    /**
     * 查询嘱托最大编码code
     * @return
     */
    @Select("select max(code) from mz_zd_instruction_code")
    String selectMaxCode();
}
