package thyyxxk.webserver.dao.his.dictionary;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
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.OpLevelManagement;
import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.ZdIcd9Cm3;

import java.util.List;

/**
 * ${comments}
 *
 * @author lh
 * @email lihong@gmail.com
 * @date 2023-06-08 10:57:38
 */
@Mapper
public interface OpLevelManagementDao extends BaseMapper<OpLevelManagement> {
    @Select(" select rtrim(code) code ,name,op_scale from zd_icd9_cm3  ${ew.customSqlSegment}   ")
    IPage<ZdIcd9Cm3> queryZdIcd9Cm3Page(IPage<ZdIcd9Cm3> page,@Param(Constants.WRAPPER) QueryWrapper queryWrapper);
    @Update(" update  zd_icd9_cm3 set op_scale = #{opScale} where code =#{codes} ")
    void updateZdIcd9Cm3OpScaleByCode(String codes, Integer opScale);
    @Select(" select  code,code_doctor , name_doctor  from op_level_management  ${ew.customSqlSegment} group by code,code_doctor,name_doctor ")
    IPage<OpLevelManagement> selectDoctorPage(IPage<OpLevelManagement> page, @Param(Constants.WRAPPER)QueryWrapper queryWrapper);
    @Insert( " <script> " +
            " insert into op_level_management ( code_doctor,name_doctor,code_op,code_op_name,input_time,input_user,code) values " +
            "   <foreach collection='list' item='item'  separator=',' > " +
            " ( #{item.codeDoctor},#{item.nameDoctor},#{item.codeOp},#{item.codeOpName},getdate(),#{item.inputUser},#{item.code} ) " +
            "   </foreach> " +
            " </script> ")
    void batchSaveOpLevelManagement(@Param("list") List<OpLevelManagement> list);
    @Select(" select rtrim(code_rs) code_doctor, code from a_employee_mi where code_rs in(${codes}) and isnull(del_flag,'')<>'1' ")
    List<OpLevelManagement> selectEmpCode(@Param("codes") String codes);
    @Delete(" delete from op_level_management where id in( " +
            " select t1.id from op_level_management t1 join " +
            " (select code,code_op from op_level_management group by code,code_op having count(*) > 1) t2 " +
            " on t1.code=t2.code and t1.code_op =t2.code_op " +
            " where  t1.id not in (select max(id) from op_level_management group by code,code_op having count(*) > 1) )"
           )
    void distinctOpLevelManagement();
}
